AWS DynamoDB CLI

feature image

AWS DynamoDB CLI

Make sure your json file is UTF-8 without BOM.

Make sure your json file is UTF-8 without BOM.

Make sure your json file is UTF-8 without BOM.


Find current region

Run:

aws configure get region

Output:

ap-east-1

List all Dynamodb tables

Run:

aws dynamodb list-tables

Output:

{
    "TableNames": [
        "Hero",
        "Music",
        "NBA",
        "demo-Music",
        "final-favourite_games"
    ]
}

List local Dynamodb tables

Run:

aws dynamodb list-tables --endpoint-url http://localhost:8000

Output:

{
    "TableNames": [
        "Hero",
    ]
}

Describe one Dynamodb tables

We had one preset Dynamodb table “NBA”. (no “-demo”)

Run:

aws dynamodb describe-table --table-name NBA

Output:

{
    "Table": {
        "AttributeDefinitions": [
            {
                "AttributeName": "GameDate",
                "AttributeType": "S"
            },
            {
                "AttributeName": "Team",
                "AttributeType": "S"
            }
        ],
        "TableName": "NBA",
        "KeySchema": [
            {
                "AttributeName": "Team",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "GameDate",
                "KeyType": "RANGE"
            }
        ],
        "TableStatus": "ACTIVE",
        "CreationDateTime": "2022-05-19T11:23:34.141000+08:00",
        "ProvisionedThroughput": {
            "LastDecreaseDateTime": "2022-05-19T11:25:22.409000+08:00",
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        },
        "TableSizeBytes": 329,
        "ItemCount": 5,
        "TableArn": "arn:aws:dynamodb:ap-east-1:639471902531:table/NBA",
        "TableId": "048c1ee3-7803-4c02-bcae-0c9edc03fa52"
    }
}

Create table

create a table with tags

The following create-table example uses the specified attributes and key schema to create a table named “NBA-demo”. This table uses provisioned throughput. The command also applies a tag to the table, with a key of Owner and a value of blueTeam.

Run:

aws dynamodb create-table \
    --table-name NBA-demo \
    --attribute-definitions AttributeName=Team,AttributeType=S AttributeName=GameDate,AttributeType=S \
    --key-schema AttributeName=Team,KeyType=HASH AttributeName=GameDate,KeyType=RANGE \
    --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
    --tags Key=Env,Value=Demo

Output:

{
    "TableDescription": {
        "AttributeDefinitions": [
            {
                "AttributeName": "Artist",
                "AttributeType": "S"
            },
            {
                "AttributeName": "gameTitle",
                "AttributeType": "S"
            }
        ],
        "TableName": "MusicCollection",
        "KeySchema": [
            {
                "AttributeName": "Artist",
                "KeyType": "HASH"
            },
                        {
                "AttributeName": "gameTitle",
                "KeyType": "RANGE"
            }
        ],
        "TableStatus": "CREATING",
        "CreationDateTime": "2022-10-14T23:01:06.475000+08:00",
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 5,
            "WriteCapacityUnits": 5
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:ap-east-1:639471902531:table/MusicCollection",
        "TableId": "3373a6df-176a-48de-98b9-e865545be4fa"
    }
}

Generate json template

Run:

aws dynamodb create-table --generate-cli-skeleton > cli-dynamodb-create-table-template.json

Open the template.json

Output:

{
    "AttributeDefinitions": [
        {
            "AttributeName": "",
            "AttributeType": "S"
        }
    ],
    "TableName": "",
    "KeySchema": [
        {
            "AttributeName": "",
            "KeyType": "RANGE"
        }
    ],
    "LocalSecondaryIndexes": [
        {
            "IndexName": "",
            "KeySchema": [
                {
                    "AttributeName": "",
                    "KeyType": "RANGE"
                }
            ],
            "Projection": {
                "ProjectionType": "KEYS_ONLY",
                "NonKeyAttributes": [
                    ""
                ]
            }
        }
    ],
    "GlobalSecondaryIndexes": [
        {
            "IndexName": "",
            "KeySchema": [
                {
                    "AttributeName": "",
                    "KeyType": "RANGE"
                }
            ],
            "Projection": {
                "ProjectionType": "KEYS_ONLY",
                "NonKeyAttributes": [
                    ""
                ]
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 0,
                "WriteCapacityUnits": 0
            }
        }
    ],
    "BillingMode": "PROVISIONED",
    "ProvisionedThroughput": {
        "ReadCapacityUnits": 0,
        "WriteCapacityUnits": 0
    },
    "StreamSpecification": {
        "StreamEnabled": true,
        "StreamViewType": "NEW_IMAGE"
    },
    "SSESpecification": {
        "Enabled": true,
        "SSEType": "AES256",
        "KMSMasterKeyId": ""
    },
    "Tags": [
        {
            "Key": "",
            "Value": ""
        }
    ],
    "TableClass": "STANDARD_INFREQUENT_ACCESS"
}

Create table from a json file

Make sure your json file is UTF-8 without BOM.

If you use Sublime Text, then go to File -> Save with Encoding -> UTF-8.

If you use VS Code, at the bottom bar of VS Code, you’ll see the label “UTF-8 with BOM”. Click it to open the action bar and select “Save with encoding”. Choose UTF-8 and save the file.

Run:

aws dynamodb create-table --cli-input-json file://dynamodb-create-table-Notes-error.json
aws dynamodb create-table --cli-input-json file://dynamodb-create-table-Notes-work.json

Output:


Describe table

Describe table full details

Run:

aws dynamodb describe-table --table-name NBA-demo

Output:

{
    "Table": {
        "AttributeDefinitions": [
            {
                "AttributeName": "GameDate",
                "AttributeType": "S"
            },
            {
                "AttributeName": "Team",
                "AttributeType": "S"
            }
        ],
        "TableName": "NBA-demo",
        "KeySchema": [
            {
                "AttributeName": "Team",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "GameDate",
                "KeyType": "RANGE"
            }
        ],
        "TableStatus": "ACTIVE",
        "CreationDateTime": "2022-05-19T11:23:34.141000+08:00",
        "ProvisionedThroughput": {
            "LastDecreaseDateTime": "2022-05-19T11:25:22.409000+08:00",
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        },
        "TableSizeBytes": 422,
        "ItemCount": 7,
        "TableArn": "arn:aws:dynamodb:ap-east-1:639471902531:table/NBA",
        "TableId": "048c1ee3-7803-4c02-bcae-0c9edc03fa52"
    }
}

Wait

DynamoDB needs 2-3 seconds to create a table. You may wait until the table is ready before move on the next step.

Run:

aws dynamodb wait table-exists --table-name NBA

Output:

Show only TableStatus to make sure it is ACTIVE

Run:

aws dynamodb describe-table --table-name NBA --query "Table.TableStatus"

Output:

"ACTIVE"

Add record to Dynamodb

NBA table attribute: Team, GameDate, Venue, Players, Ticket

Run:

aws dynamodb put-item \
    --table-name NBA-demo \
    --item '{
      "Team": {"S": "Boston Celtics"},
      "GameDate": {"S": "2022-10-19"},
      "Venue": {"S": "TD Garden"}
    }'

Output:

Insert String Set

Run:

aws dynamodb put-item \
    --table-name NBA-demo \
    --item '{ 
      "Team": {"S": "Golden State"},
      "GameDate": {"S": "2022-10-18"},
      "Players": {
        "SS": [
            "Stephen Curry", 
            "Draymond Green",
            "Klay Thompson",
            "Andrew Wiggins",
            "James Wiseman"
        ]
      }
    }'

Output:

Output result after saved data

Run:

aws dynamodb put-item \
    --table-name NBA-demo \
    --item '{
        "Team": {"S": "Boston Celtics"},
        "GameDate": {"S": "2022-10-15"},
        "Ticket": {"N": "100"}
    }
    ' \
    --return-consumed-capacity TOTAL \
    --return-item-collection-metrics SIZE

Output:

{
    "ConsumedCapacity": {
        "TableName": "NBA",
        "CapacityUnits": 1.0
    }
}

Input record from file

Insert one simple record from JSON file.

Run:

aws dynamodb put-item \
    --table-name NBA-demo \
    --item file://dynamodb-item-NBA-1.json \
    --return-consumed-capacity TOTAL \
    --return-item-collection-metrics SIZE

Output:

{
    "ConsumedCapacity": {
        "TableName": "NBA-demo",
        "CapacityUnits": 1.0
    }
}

Insert Mapping, String Set and Number Set.

Run:

aws dynamodb put-item \
    --table-name NBA-demo \
    --item file://dynamodb-item-NBA-2.json \
    --return-consumed-capacity TOTAL \
    --return-item-collection-metrics SIZE

Output:

{
    "ConsumedCapacity": {
        "TableName": "NBA-demo",
        "CapacityUnits": 1.0
    }
}

Scan table to display some items

Run:

aws dynamodb scan --table-name NBA-demo

Output:

{
    "Items": [
        {
            "GameDate": {
                "S": "2022-05-18"
            },
            "Team": {
                "S": "Boston Celtics"
            }
        },
        {
            "Team": {
                "S": "Boston Celtics"
            },
            "GameDate": {
                "S": "2022-05-19"
            },
            "Venue": {
                "S": "TD Garden"
            }
        },
[...]

Get item

Get one item

Run:

aws dynamodb get-item \
    --table-name NBA-demo \
    --key '{"Team":{"S": "Boston Celtics"},"GameDate":{"S": "2022-10-15"}}'

Output:

{
    "Item": {
        "Team": {
            "S": "Boston Celtics"
        },
        "GameDate": {
            "S": "2022-10-15"
        },
        "Ticket": {
            "N": "100"
        }
    }
}

Get item from file

Run:

aws dynamodb get-item \
    --table-name NBA-demo \
    --key file://dynamodb-key-NBA-1.json \
    --return-consumed-capacity TOTAL

Output:

{
    "Item": {
        "Team": {
            "S": "Boston Celtics"
        },
        "GameDate": {
            "S": "2022-10-15"
        },
        "Ticket": {
            "N": "100"
        }
    },
    "ConsumedCapacity": {
        "TableName": "NBA-demo",
        "CapacityUnits": 0.5
    }
}

return specific attribute

Run:

aws dynamodb get-item \
    --table-name NBA-demo \
    --key file://dynamodb-key-NBA-1.json \
    --projection-expression "GameDate, Ticket" \
    --return-consumed-capacity TOTAL

Output:

{
    "Item": {
        "Team": {
            "S": "Boston Celtics"
        },
        "GameDate": {
            "S": "2022-10-19"
        },
        "Ticket": {
            "N": "100"
        }
    },
    "ConsumedCapacity": {
        "TableName": "NBA",
        "CapacityUnits": 0.5
    }
}

Update

Update one specific record

Add a new record to table

Run:

aws dynamodb update-item \
    --table-name NBA-demo \
    --key '{
        "Team": {"S": "Boston Celtics"},
        "GameDate": {"S": "2022-10-19"}
    }' \
    --update-expression "SET #Field = #Field - :Number" \
    --expression-attribute-names '{
        "#Field": "Ticket"
        }' \
    --expression-attribute-values '{
        ":Number": {"N": "1"}
        }' \
    --return-values ALL_NEW

Output:

{
    "Attributes": {
        "Team": {
            "S": "Boston Celtics"
        },
        "GameDate": {
            "S": "2022-10-19"
        },
        "Ticket": {
            "N": "99"
        }
    }
}

New item for update demo

Add a new record to table

Run:

aws dynamodb put-item \
    --table-name NBA-demo \
    --item '{
        "Team": {"S": "Boston Celtics"},
        "GameDate": {"S": "2022-10-16"},
        "Ticket": {"N": "-1"}
    }' \
    --return-consumed-capacity TOTAL \
    --return-item-collection-metrics SIZE

Output:

To conditionally overwrite an item in a table

Update that record based on Ticket attribute.

Run:

aws dynamodb put-item \
    --table-name NBA-demo \
    --item '{
        "Team": {"S": "Boston Celtics"},
        "GameDate": {"S": "2022-10-16"},
        "Ticket": {"N": "201"},
        "Venue": {"S": "TD Garden"}
    }' \
    --condition-expression "#SearchField = :SearchValue" \
    --expression-attribute-names '{"#SearchField": "Ticket"}' \
    --expression-attribute-values '{":SearchValue": {"N": "-1"}}' \
    --return-values ALL_OLD

Output:

{
    "Attributes": {
        "Team": {
            "S": "Boston Celtics"
        },
        "GameDate": {
            "S": "2022-10-16"
        },
        "Ticket": {
            "N": "201"
        },
        "Venue": {
            "S": "TD Garden"
        }
    }
}

To conditionally overwrite an item in a table based on 2 attributes

Run:

aws dynamodb put-item \
    --table-name NBA-demo \
    --item '{
        "Team": {"S": "Boston Celtics"},
        "GameDate": {"S": "2022-10-16"},
        "Ticket": {"N": "321"},
        "Venue": {"S": "Stadium"}
    }' \
    --condition-expression "#SearchField1 = :SearchValue1 and #SearchField2 = :SearchValue2" \
    --expression-attribute-names '{"#SearchField1": "Team", "#SearchField2":"GameDate"}' \
    --expression-attribute-values '{":SearchValue1": {"S": "Boston Celtics"}, ":SearchValue2": {"S": "2022-10-16"}}' \
    --return-values ALL_OLD

Output:

{
    "Attributes": {
        "Team": {
            "S": "Boston Celtics"
        },
        "GameDate": {
            "S": "2022-10-16"
        },
        "Ticket": {
            "N": "201"
        },
        "Venue": {
            "S": "TD Garden"
        }
    }
}

If that Primary Key exist, then do not overwrite it.

The conditional request failed

Run:

aws dynamodb put-item \
    --table-name NBA-demo \
    --item '{
        "Team": {"S": "Boston Celtics"},
        "GameDate": {"S": "2022-10-20"},
        "Ticket": {"N": "456"},
        "Venue": {"S": "TD Garden"}
    }' \
    --condition-expression "attribute_not_exists(Team) AND attribute_not_exists(GameDate)" \
    --return-values ALL_OLD

Output:

An error occurred (ConditionalCheckFailedException) when calling the PutItem operation: The conditional request failed

It works!

Run:

aws dynamodb put-item \
    --table-name NBA-demo \
    --item '{
        "Team": {"S": "Boston Celtics"},
        "GameDate": {"S": "2022-10-20"},
        "Ticket": {"N": "456"},
        "Venue": {"S": "TD Garden"}
    }' \
    --condition-expression "attribute_not_exists(Team) AND attribute_not_exists(GameDate)" \
    --return-values ALL_OLD

Output:


Delete item

Run:

aws dynamodb delete-item \
    --table-name NBA-demo \
    --key '{
        "Team": {"S": "Boston Celtics"},
        "GameDate": {"S": "2022-10-20"}
    }' \
    --return-values ALL_OLD \
    --return-consumed-capacity TOTAL \
    --return-item-collection-metrics SIZE

Output:

{
    "Attributes": {
        "Team": {
            "S": "Boston Celtics"
        },
        "GameDate": {
            "S": "2022-10-20"
        },
        "Ticket": {
            "N": "456"
        },
        "Venue": {
            "S": "TD Garden"
        }
    },
    "ConsumedCapacity": {
        "TableName": "NBA-demo",
        "CapacityUnits": 1.0
    }
}

To conditionally overwrite an item in a table based on 2 attributes

Run:

aws dynamodb delete-item \
    --table-name NBA-demo \
    --key '{
        "Team": {"S": "Boston Celtics"},
        "GameDate": {"S": "2022-10-16"}
        }' \
    --condition-expression '(#SearchField1 IN (:venue1, :venue2)) and (#SearchField2 between :startdate and :enddate)' \
    --expression-attribute-names '{
        "#SearchField1":"Venue",
        "#SearchField2":"GameDate"      
        }' \
    --expression-attribute-values '{
        ":venue1":{"S":"Stadium"},
        ":venue2":{"S":"Court"},
        ":startdate":{"S":"2022-10-16"},
        ":enddate":{"S":"2022-10-20"}
        }' \
    --return-values ALL_OLD

Output:

{
    "Attributes": {
        "Team": {
            "S": "Boston Celtics"
        },
        "GameDate": {
            "S": "2022-10-16"
        },
        "Ticket": {
            "N": "321"
        },
        "Venue": {
            "S": "Stadium"
        }
    }
}

Delete table

Run:

aws dynamodb delete-table --table-name NBA-demo

Output:

{
    "TableDescription": {
        "TableName": "MusicCollection",
        "TableStatus": "DELETING",
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:ap-east-1:639471902531:table/MusicCollection",
        "TableId": "f462ebcb-c328-40e4-9787-8e3db6391fd9"
    }
}

Run:

aws dynamodb delete-table --table-name Notes
,