[AWS Lab] DynamoDB – Basic Operations via CLI

In this lab, we will learn how to work with DynamoDB tables using AWS CLI.

  • Overview
    • DynamoDB
      • Create a table
      • List tables
      • Get the table information
      • Insert, Update, and delete items
      • Scan and Query items

1. Cloud9 – Setup Environment

You can use your local machine to set up AWS CLI and execute commands. Or you can use the Cloud9 environment.

  1. Click “Create environment
    • Name: “AWS Env
    • Check “New EC2 instance
    • Instance type: “t2.micro
    • Platform: “Amazon Linux 2
    • Connection: “AWS Systems Manager
  2. Click “Create
  3. Click “Open” in the “Cloud9 Env” column
  4. On the terminal, enter the following commands to verify the AWS CLI setup
aws --version

aws s3 ls
  • If the AWS CLI version is 1.x, you need to update the CLI to version 2.x
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

unzip awscliv2.zip

sudo ./aws/install --bin-dir /usr/local/bin --install-dir /usr/local/aws-cli --update

aws --version
  • You might need to exit the current terminal and open the new one.

2. DynamoDB

  • Open the DynamoDB in the new tab
    • You can check the progress.

3. Table Structure

  • Table Name: “OrderHistory
  • Partition Key (Hash): “CustomerId“, Number
  • Sort Key (Range): “OrderId“, Number
  • Other Attributes:
    • Product“, String
    • Price“, Number

4. CLI – Create a Table

aws dynamodb create-table \
--table-name OrderHistory \
--attribute-definitions AttributeName=CustomerId,AttributeType=N AttributeName=OrderId,AttributeType=N \
--key-schema AttributeName=CustomerId,KeyType=HASH AttributeName=OrderId,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1

5. CLI – List tables

aws dynamodb list-tables

6. CLI – Get the table information

aws dynamodb describe-table \
--table-name OrderHistory

7. CLI – Write items to the table

aws dynamodb put-item \
--table-name OrderHistory  \
--item \
'{"CustomerId": {"N": "10"}, "OrderId": {"N": "100"}, "Product": {"S": "Computer"}, "Price": {"N": "890.99"}}'
 
aws dynamodb put-item \
--table-name OrderHistory  \
--item \
'{"CustomerId": {"N": "10"}, "OrderId": {"N": "120"}, "Product": {"S": "Monitor"}, "Price": {"N": "120.0"}}'
 
aws dynamodb put-item \
--table-name OrderHistory  \
--item \
'{"CustomerId": {"N": "10"}, "OrderId": {"N": "150"}, "Product": {"S": "Speaker"}, "Price": {"N": "200.10"}}'
 
aws dynamodb put-item \
--table-name OrderHistory  \
--item \
'{"CustomerId": {"N": "20"}, "OrderId": {"N": "100"}, "Product": {"S": "Monitor"}, "Price": {"N": "199.99"}}'
 
aws dynamodb put-item \
--table-name OrderHistory  \
--item \
'{"CustomerId": {"N": "20"}, "OrderId": {"N": "110"}, "Product": {"S": "Keyboard"}, "Price": {"N": "79.99"}}'

8. CLI – Scan items

aws dynamodb scan \
--table-name OrderHistory

aws dynamodb scan \
--table-name OrderHistory \
--filter-expression 'Price < :p' \
--expression-attribute-values '{":p": {"N":"100"}}'

aws dynamodb scan \
--table-name OrderHistory \
--scan-filter '{
    "Price":{
        "AttributeValueList":[{"N":"100"}],
        "ComparisonOperator": "LT"
    } 
}'

aws dynamodb scan \
--table-name OrderHistory \
--projection-expression 'OrderId, Price' \
--filter-expression 'CustomerId = :cid AND Product = :name' \
--expression-attribute-values '{":cid":{"N":"10"}, ":name":{"S":"Monitor"}}'

9. CLI – Get an item using the primary key

aws dynamodb get-item \
--table-name OrderHistory \
--key '{ "CustomerId": {"N": "10"}, "OrderId": {"N": "150"}}'

10. CLI – Update an item

aws dynamodb update-item \
--table-name OrderHistory \
--key '{ "CustomerId": {"N": "10"}, "OrderId": {"N": "150"}}' \
--update-expression "SET Price = :newPrice" \
--expression-attribute-values '{":newPrice":{"N":"180.40"}}'

aws dynamodb get-item \
--table-name OrderHistory \
--key '{ "CustomerId": {"N": "10"}, "OrderId": {"N": "150"}}'

11. CLI – Query data

aws dynamodb query \
--table-name OrderHistory \
--key-condition-expression "CustomerId = :customerId AND OrderId >= :minimumOrderId" \
--expression-attribute-values  '{":customerId":{"N":"10"}, ":minimumOrderId":{"N":"120"}}'

12. CLI – Get item count and Delete the item

--table-name OrderHistory \
--select "COUNT"

aws dynamodb delete-item \
--table-name OrderHistory \
--key '{ "CustomerId": {"N": "10"}, "OrderId": {"N": "150"}}'

aws dynamodb scan \
--table-name OrderHistory \
--select "COUNT"

13. CLI – Delete the table

aws dynamodb delete-table \
--table-name OrderHistory

aws dynamodb describe-table \
--table-name OrderHistory

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s