AWS DynamoDB: Put Item into a Table
Insert or replace items in DynamoDB using aws dynamodb put-item with DynamoDB JSON format, conditional writes, and return values.
DynamoDB Operations
Detailed Explanation
Putting Items into DynamoDB
The aws dynamodb put-item command creates a new item or replaces an existing item in a DynamoDB table. Items are specified in DynamoDB's JSON format, which includes type descriptors.
Basic Put Item
aws dynamodb put-item \
--table-name Users \
--item '{
"userId": {"S": "user-001"},
"name": {"S": "Alice Johnson"},
"email": {"S": "alice@example.com"},
"age": {"N": "28"},
"active": {"BOOL": true},
"tags": {"L": [{"S": "admin"}, {"S": "developer"}]}
}'
DynamoDB JSON Type Descriptors
| Type | Key | Example |
|---|---|---|
| String | S |
{"S": "hello"} |
| Number | N |
{"N": "42"} (always a string) |
| Boolean | BOOL |
{"BOOL": true} |
| Null | NULL |
{"NULL": true} |
| List | L |
{"L": [{"S": "a"}, {"N": "1"}]} |
| Map | M |
{"M": {"key": {"S": "val"}}} |
| String Set | SS |
{"SS": ["a", "b"]} |
| Number Set | NS |
{"NS": ["1", "2"]} |
Conditional Put (prevent overwriting)
aws dynamodb put-item \
--table-name Users \
--item '{"userId": {"S": "user-001"}, "name": {"S": "Alice"}}' \
--condition-expression "attribute_not_exists(userId)"
This fails with ConditionalCheckFailedException if the item already exists.
Return Old Values
aws dynamodb put-item \
--table-name Users \
--item '{"userId": {"S": "user-001"}, "name": {"S": "Alice Updated"}}' \
--return-values ALL_OLD
Returns the previous version of the item before it was replaced.
From File
aws dynamodb put-item \
--table-name Users \
--item file://item.json
Use Case
Seeding DynamoDB tables with test data, inserting records from CLI scripts, performing one-off data corrections, or scripting data migration tasks between tables.