- Navigation GuideYou are on a Command (operation) page with structural examples. Use the navigation breadcrumb if you would like to return to the Client landing page.
TransactWriteItemsCommand
TransactWriteItems
is a synchronous write operation that groups up to 100 action requests. These actions can target items in different tables, but not in different Amazon Web Services accounts or Regions, and no two actions can target the same item. For example, you cannot both ConditionCheck
and Update
the same item. The aggregate size of the items in the transaction cannot exceed 4 MB.
The actions are completed atomically so that either all of them succeed, or all of them fail. They are defined by the following objects:
-
Put
— Initiates aPutItem
operation to write a new item. This structure specifies the primary key of the item to be written, the name of the table to write it in, an optional condition expression that must be satisfied for the write to succeed, a list of the item's attributes, and a field indicating whether to retrieve the item's attributes if the condition is not met. -
Update
— Initiates anUpdateItem
operation to update an existing item. This structure specifies the primary key of the item to be updated, the name of the table where it resides, an optional condition expression that must be satisfied for the update to succeed, an expression that defines one or more attributes to be updated, and a field indicating whether to retrieve the item's attributes if the condition is not met. -
Delete
— Initiates aDeleteItem
operation to delete an existing item. This structure specifies the primary key of the item to be deleted, the name of the table where it resides, an optional condition expression that must be satisfied for the deletion to succeed, and a field indicating whether to retrieve the item's attributes if the condition is not met. -
ConditionCheck
— Applies a condition to an item that is not being modified by the transaction. This structure specifies the primary key of the item to be checked, the name of the table where it resides, a condition expression that must be satisfied for the transaction to succeed, and a field indicating whether to retrieve the item's attributes if the condition is not met.
DynamoDB rejects the entire TransactWriteItems
request if any of the following is true:
-
A condition in one of the condition expressions is not met.
-
An ongoing operation is in the process of updating the same item.
-
There is insufficient provisioned capacity for the transaction to be completed.
-
An item size becomes too large (bigger than 400 KB), a local secondary index (LSI) becomes too large, or a similar validation error occurs because of changes made by the transaction.
-
The aggregate size of the items in the transaction exceeds 4 MB.
-
There is a user error, such as an invalid data format.
Example Syntax
Use a bare-bones client and the command you need to make an API call.
import { DynamoDBClient, TransactWriteItemsCommand } from "@aws-sdk/client-dynamodb"; // ES Modules import
// const { DynamoDBClient, TransactWriteItemsCommand } = require("@aws-sdk/client-dynamodb"); // CommonJS import
const client = new DynamoDBClient(config);
const input = { // TransactWriteItemsInput
TransactItems: [ // TransactWriteItemList // required
{ // TransactWriteItem
ConditionCheck: { // ConditionCheck
Key: { // Key // required
"<keys>": { // AttributeValue Union: only one key present
S: "STRING_VALUE",
N: "STRING_VALUE",
B: new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("")
SS: [ // StringSetAttributeValue
"STRING_VALUE",
],
NS: [ // NumberSetAttributeValue
"STRING_VALUE",
],
BS: [ // BinarySetAttributeValue
new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("")
],
M: { // MapAttributeValue
"<keys>": {// Union: only one key present
S: "STRING_VALUE",
N: "STRING_VALUE",
B: new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("")
SS: [
"STRING_VALUE",
],
NS: [
"STRING_VALUE",
],
BS: [
new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("")
],
M: {
"<keys>": "<AttributeValue>",
},
L: [ // ListAttributeValue
"<AttributeValue>",
],
NULL: true || false,
BOOL: true || false,
},
},
L: [
"<AttributeValue>",
],
NULL: true || false,
BOOL: true || false,
},
},
TableName: "STRING_VALUE", // required
ConditionExpression: "STRING_VALUE", // required
ExpressionAttributeNames: { // ExpressionAttributeNameMap
"<keys>": "STRING_VALUE",
},
ExpressionAttributeValues: { // ExpressionAttributeValueMap
"<keys>": "<AttributeValue>",
},
ReturnValuesOnConditionCheckFailure: "ALL_OLD" || "NONE",
},
Put: { // Put
Item: { // PutItemInputAttributeMap // required
"<keys>": "<AttributeValue>",
},
TableName: "STRING_VALUE", // required
ConditionExpression: "STRING_VALUE",
ExpressionAttributeNames: {
"<keys>": "STRING_VALUE",
},
ExpressionAttributeValues: {
"<keys>": "<AttributeValue>",
},
ReturnValuesOnConditionCheckFailure: "ALL_OLD" || "NONE",
},
Delete: { // Delete
Key: { // required
"<keys>": "<AttributeValue>",
},
TableName: "STRING_VALUE", // required
ConditionExpression: "STRING_VALUE",
ExpressionAttributeNames: {
"<keys>": "STRING_VALUE",
},
ExpressionAttributeValues: {
"<keys>": "<AttributeValue>",
},
ReturnValuesOnConditionCheckFailure: "ALL_OLD" || "NONE",
},
Update: { // Update
Key: { // required
"<keys>": "<AttributeValue>",
},
UpdateExpression: "STRING_VALUE", // required
TableName: "STRING_VALUE", // required
ConditionExpression: "STRING_VALUE",
ExpressionAttributeNames: {
"<keys>": "STRING_VALUE",
},
ExpressionAttributeValues: {
"<keys>": "<AttributeValue>",
},
ReturnValuesOnConditionCheckFailure: "ALL_OLD" || "NONE",
},
},
],
ReturnConsumedCapacity: "INDEXES" || "TOTAL" || "NONE",
ReturnItemCollectionMetrics: "SIZE" || "NONE",
ClientRequestToken: "STRING_VALUE",
};
const command = new TransactWriteItemsCommand(input);
const response = await client.send(command);
// { // TransactWriteItemsOutput
// ConsumedCapacity: [ // ConsumedCapacityMultiple
// { // ConsumedCapacity
// TableName: "STRING_VALUE",
// CapacityUnits: Number("double"),
// ReadCapacityUnits: Number("double"),
// WriteCapacityUnits: Number("double"),
// Table: { // Capacity
// ReadCapacityUnits: Number("double"),
// WriteCapacityUnits: Number("double"),
// CapacityUnits: Number("double"),
// },
// LocalSecondaryIndexes: { // SecondaryIndexesCapacityMap
// "<keys>": {
// ReadCapacityUnits: Number("double"),
// WriteCapacityUnits: Number("double"),
// CapacityUnits: Number("double"),
// },
// },
// GlobalSecondaryIndexes: {
// "<keys>": {
// ReadCapacityUnits: Number("double"),
// WriteCapacityUnits: Number("double"),
// CapacityUnits: Number("double"),
// },
// },
// },
// ],
// ItemCollectionMetrics: { // ItemCollectionMetricsPerTable
// "<keys>": [ // ItemCollectionMetricsMultiple
// { // ItemCollectionMetrics
// ItemCollectionKey: { // ItemCollectionKeyAttributeMap
// "<keys>": { // AttributeValue Union: only one key present
// S: "STRING_VALUE",
// N: "STRING_VALUE",
// B: new Uint8Array(),
// SS: [ // StringSetAttributeValue
// "STRING_VALUE",
// ],
// NS: [ // NumberSetAttributeValue
// "STRING_VALUE",
// ],
// BS: [ // BinarySetAttributeValue
// new Uint8Array(),
// ],
// M: { // MapAttributeValue
// "<keys>": {// Union: only one key present
// S: "STRING_VALUE",
// N: "STRING_VALUE",
// B: new Uint8Array(),
// SS: [
// "STRING_VALUE",
// ],
// NS: [
// "STRING_VALUE",
// ],
// BS: [
// new Uint8Array(),
// ],
// M: {
// "<keys>": "<AttributeValue>",
// },
// L: [ // ListAttributeValue
// "<AttributeValue>",
// ],
// NULL: true || false,
// BOOL: true || false,
// },
// },
// L: [
// "<AttributeValue>",
// ],
// NULL: true || false,
// BOOL: true || false,
// },
// },
// SizeEstimateRangeGB: [ // ItemCollectionSizeEstimateRange
// Number("double"),
// ],
// },
// ],
// },
// };
TransactWriteItemsCommand Input
Parameter | Type | Description |
---|
Parameter | Type | Description |
---|---|---|
TransactItems Required | TransactWriteItem[] | undefined | An ordered array of up to 100 |
ClientRequestToken | string | undefined | Providing a Although multiple identical calls using the same client request token produce the same result on the server (no side effects), the responses to the calls might not be the same. If the A client request token is valid for 10 minutes after the first request that uses it is completed. After 10 minutes, any request with the same client token is treated as a new request. Do not resubmit the same request with the same client token for more than 10 minutes, or the result might not be idempotent. If you submit a request with the same client token but a change in other parameters within the 10-minute idempotency window, DynamoDB returns an |
ReturnConsumedCapacity | ReturnConsumedCapacity | undefined | Determines the level of detail about either provisioned or on-demand throughput consumption that is returned in the response:
|
ReturnItemCollectionMetrics | ReturnItemCollectionMetrics | undefined | Determines whether item collection metrics are returned. If set to |
TransactWriteItemsCommand Output
Parameter | Type | Description |
---|
Parameter | Type | Description |
---|---|---|
$metadata Required | ResponseMetadata | Metadata pertaining to this request. |
ConsumedCapacity | ConsumedCapacity[] | undefined | The capacity units consumed by the entire |
ItemCollectionMetrics | Record<string, ItemCollectionMetrics[]> | undefined | A list of tables that were processed by |
Throws
Name | Fault | Details |
---|
Name | Fault | Details |
---|---|---|
IdempotentParameterMismatchException | client | DynamoDB rejected the request because you retried a request with a different payload but with an idempotent token that was already used. |
InternalServerError | server | An error occurred on the server side. |
InvalidEndpointException | client | |
ProvisionedThroughputExceededException | client | Your request rate is too high. The Amazon Web Services SDKs for DynamoDB automatically retry requests that receive this exception. Your request is eventually successful, unless your retry queue is too large to finish. Reduce the frequency of requests and use exponential backoff. For more information, go to Error Retries and Exponential Backoff in the Amazon DynamoDB Developer Guide. |
RequestLimitExceeded | client | Throughput exceeds the current throughput quota for your account. Please contact Amazon Web ServicesSupport to request a quota increase. |
ResourceNotFoundException | client | The operation tried to access a nonexistent table or index. The resource might not be specified correctly, or its status might not be |
TransactionCanceledException | client | The entire transaction request was canceled. DynamoDB cancels a
DynamoDB cancels a
If using Java, DynamoDB lists the cancellation reasons on the Cancellation reason codes and possible error messages:
|
TransactionInProgressException | client | The transaction with the given request token is already in progress. Recommended Settings This is a general recommendation for handling the
Assuming default retry policy , example timeout settings based on the guidelines above are as follows: Example timeline:
|
DynamoDBServiceException | Base exception class for all service exceptions from DynamoDB service. |