Problem:
Execute various opertions on a DynamoDB table from AWS CLI.
Solution Summary:
We will execute various example commands on the table we already created in previous lab. We will also delete and create the table again through command line.
Prerequisites:
This lab assumes you have followed previous labs and created a user with necessary permissions, installed awscli, created and setup profile in local, and created the table.
Solution Steps:
Find all commands available
aws dynamodb ?
List tables in a region
aws dynamodb list-tables --profile dynamodbdev --region ap-south-1
Note: This will list all tables in the region in json format. You don’t have to mention region if you have configured it as default using aws configure.
Get description of the table
aws dynamodb describe-table --table-name course_data --profile dynamodbdev
Delete Table
aws dynamodb delete-table --table-name course_data --profile dynamodbdev
Create Table
aws dynamodb create-table --table-name course_data --profile dynamodbdev --attribute-definitions AttributeName=course_id,AttributeType=S AttributeName=course_date,AttributeType=S --key-schema AttributeName=course_id,KeyType=HASH AttributeName=course_date,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
Note:
-
You can also import the configuration from a json as: aws dynamodb create-table --cli-input-json <file/json> --profile dynamodbdev
-
You can generate the skeleton json as: aws dynamodb create-table --generate-cli-skeleton --profile dynamodbdev
-
create-table returns immediately and table is created asynchronously. You can ask to wait until table exist, by using wait as: aws dynamodb wait table-exists --table course_data
Update Table
aws dynamodb update-table --table-name course_data --profile dynamodbdev --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
Put Item
aws dynamodb put-item --table-name course_data --item {\"course_id\":{\"S\":\"c001\"},\"course_date\":{\"S\":\"2017/4/30\"},\"duration_in_days\":{\"N\":\"30\"}} --profile dynamodbdev
aws dynamodb put-item --table-name course_data --item {\"course_id\":{\"S\":\"c002\"},\"course_date\":{\"S\":\"2017/4/30\"},\"duration_in_days\":{\"N\":\"40\"}} --profile dynamodbdev
aws dynamodb put-item --table-name course_data --item {\"course_id\":{\"S\":\"c003\"},\"course_date\":{\"S\":\"2017/4/30\"},\"duration_in_days\":{\"N\":\"50\"}} --profile dynamodbdev
aws dynamodb put-item --table-name course_data --item {\"course_id\":{\"S\":\"c004\"},\"course_date\":{\"S\":\"2017/4/30\"},\"duration_in_days\":{\"N\":\"60\"}} --profile dynamodbdev
Notes:
-
You might have to escape double quotes and remove spaces.
-
You can get the consumed write capacity units for the command adding following option: --return-consumed-capacity TOTAL
-
You can get values of all items before updating (changed or not) adding following option: --return-values ALL_OLD
-
You can get values of all items after updating (changed or not) adding following option: --return-values ALL_NEW
-
The put command is idempotent.
Update Item
aws dynamodb update-item --table-name course_data --key {\"course_id\":{\"S\":\"c001\"},\"course_date\":{\"S\":\"2017/4/30\"}} --update-expression "SET duration_in_days=:duration_in_days" --expression-attribute-values {\":duration_in_days\":{\"N\":\"20\"}} --profile dynamodbdev
Note:
-
You might have to escape double quotes and remove spaces.
-
Update item command is more suitable for update; with put item, you will need to get the item, modify it and then put it.
-
You can get values of all items before updating (changed or not) adding following option: --return-values ALL_OLD
-
You can get values of all items after updating (changed or not) adding following option: --return-values ALL_NEW
-
You can get values of only updated items (changed or not) after updating adding following option: --return-values UPDATED_NEW
-
If you update an item that does not exist, it will be created.
Delete Item
aws dynamodb delete-item --table-name course_data --key {\"course_id\":{\"S\":\"c001\"},\"course_date\":{\"S\":\"2017/4/30\"}} --profile dynamodbdev
Note:
-
Delete item is also idempotent.
-
You can get the consumed write capacity units for the command adding following option: --return-consumed-capacity TOTAL
-
You can get values of all items before updating (changed or not) adding following option: --return-values ALL_OLD . This will only return data if data was already there.
Recent comments