[Recipes] Creating And Doing Basic Operations On DynamoDB Tables from AWS CLI

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:

  1. You can also import the configuration from a json as: aws dynamodb create-table --cli-input-json <file/json> --profile dynamodbdev

  2. You can generate the skeleton json as: aws dynamodb create-table --generate-cli-skeleton --profile dynamodbdev

  3. 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:

  1. You might have to escape double quotes and remove spaces.

  2. You can get the consumed write capacity units for the command adding following option: --return-consumed-capacity TOTAL

  3. You can get values of all items before updating (changed or not) adding following option: --return-values ALL_OLD

  4. You can get values of all items after updating (changed or not) adding following option: --return-values ALL_NEW

  5. 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:

  1. You might have to escape double quotes and remove spaces.

  2. Update item command is more suitable for update; with put item, you will need to get the item, modify it and then put it.

  3. You can get values of all items before updating (changed or not) adding following option: --return-values ALL_OLD

  4. You can get values of all items after updating (changed or not) adding following option: --return-values ALL_NEW

  5. You can get values of only updated items (changed or not) after updating adding following option: --return-values UPDATED_NEW

  6. 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:

  1. Delete item is also idempotent. 

  2. You can get the consumed write capacity units for the command adding following option: --return-consumed-capacity TOTAL

  3. 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.

Recipe Tags: 

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream