AWS CLI - DynamoDB

Server

Language :

The DynamoDB table does not have a schema other than the default key.

You must set the default key (unique identifier) when you create the table.

There are two primary keys (link)

  • Simple primary key: only partition key (HASH) is present in the table
  • The output of the hash function determines the physical partition to store the item.
  • Composite primary key: If the table has a partition key (HASH) and an alignment key (RANGE)
  • All items with the same partition key are sorted based on the alignment key.
  • Multiple items may have the same partition key, but the sort key cannot overlap within the same partition.

Secondary Index

  • Multiple auxiliary indexes may be generated for each table.
  • A partition key must be defined when creating an index.
  • Additional data capacity is required because data is stored separately using storage space.
  • generation method
  • Global Secondary Index(GSI): When using a different partition key and alignment key than the primary table
  • Local Secondary Index(LSI): If the primary table has the same partition key but different sort keys.No additional write capacity or read capacity because it does not create separate storage space.Can only be set when creating tables and cannot be added or deleted after creation.

This article notes how to handle DynamoDB through the Amazon Command Line Interface (CLI).

AWS CLI Install

Plain Text

✗ curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
✗ sudo installer -pkg AWSCLIV2.pkg -target /
✗ which aws
/usr/local/bin/aws
✗ rm AWSCLIV2.pkg
✗ aws --version
aws-cli/2.11.3 Python/3.11.2 Darwin/21.6.0 exe/x86_64 prompt/off

Table list

Plain Text

 aws dynamodb list-tables

Plain Text

# aws dynamodb list-tables --endpoint-url http://localhost:4566
{
    "TableNames": []
}

When setting endpoint-url, you must include http, https, and port numbers.

Creating a table

Plain Text

aws dynamodb create-table \
--table-name {TableName} \
--attribute-definitions AttributeName={TableKeyAttributeName},AttributeType={S/N/B} \
--key-schema AttributeName={TableKeyAttributeName},KeyType={HASH/RANG}

attribute-defined: defines the default key attribute.

  • AttributeName: Property Name
  • AttributeType: Property Type(S:String, N:Number, B:Binary)

key-schema: Defines the default key role.

  • AttributeName: Property name to specify role
  • KeyType: Property Role
  • HASH: Partition key
  • RANGE: Sort Key

Example

Plain Text

# aws dynamodb create-table \
--endpoint-url http://localhost:4566 \
--table-name myTable \
--attribute-definitions \
 AttributeName=id,AttributeType=N \
 AttributeName=createdAt,AttributeType=S \
--key-schema \
 AttributeName=id,KeyType=HASH \
 AttributeName=createdAt,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

When using json files

Plain Text

# aws dynamodb create-table \
--endpoint-url http://localhost:4566 \
--cli-input-json file://create_table.json

JSON

{
  "TableName": "myTable",
  "AttributeDefinitions": [
    {
      "AttributeName": "id",
      "AttributeType": "S"
    }
  ],
  "KeySchema": [
    {
      "AttributeName": "id",
      "KeyType": "HASH"
    }
  ],
  "ProvisionedThroughput": {
    "ReadCapacityUnits": 10,
    "WriteCapacityUnits": 5
  }
}

Delete Table

Plain Text

aws dynamodb delete-table --table-name {TableName}

Plain Text

# aws dynamodb delete-table \
--endpoint-url http://localhost:4566 \
--table-name myTable

Check whether the table is active

Plain Text

aws dynamodb describe-table --table-name {TableName} | grep TableStatus

Plain Text

# aws dynamodb describe-table \
--endpoint-url http://localhost:4566 \
--table-name myTable | grep TableStatus

Register/renew an item

Plain Text

aws dynamodb put-item --table-name {TableName} --item '{"{AttributeName}":{"{AttributeType}":"{Value}"}}'

Plain Text

# aws dynamodb put-item \
--endpoint-url http://localhost:4566 \
--table-name myTable \
--item \
 '{"id":{"N":"1"},"createdAt":{"S":"2023-03-23 10:23:12"},"sessionKey":{"S":"A12314EHDF"}}'

When using json files

Plain Text

# aws dynamodb put-item \
--endpoint-url http://phdkim-dynamodb:8301 \
--table-name myTable \
--item file://myTable_put_item.json

JSON

{
    "id":{"N":"1"},
    "createdAt":{"S":"2023-03-23 10:23:12"},
    "sessionKey":{"S":"A12314EHDF"}
}

Delete item

Plain Text

aws dynamodb delete-item --table-name {TableName} --key '{PrimaryKey}'

Plain Text

# aws dynamodb delete-item \
--endpoint-url http://localhost:4566 \
--table-name myTable \
--key '{"id":{"N":"1"},"createdAt":{"S":"2023-03-23 10:23:12"}}'

Item inquiry

If it is a composite basic key, the partition key and the alignment key are designated together and inquired.

Returns all item properties by default.

Match Lookup

Plain Text

aws dynamodb get-item --table-name {TableName} --key '{PrimaryKey}'

Plain Text

# aws dynamodb get-item \
--endpoint-url http://localhost:4566 \
--table-name myTable \
--key '{"id":{"N":"1"},"createdAt":{"S":"2023-03-23 10:23:12"}}'

Use projection expressions to query only some item properties.

Plain Text

# aws dynamodb get-item \
--table-name myTable \
--key file://myTable_search_key.json \
--projection-expression "Description, RelatedItems"

Full Inquiry

Plain Text

aws dynamodb scan --table-name {TableName}

Plain Text

# aws dynamodb scan \
--endpoint-url http://localhost:4566 \
--table-name myTable

Conditions Inquiry

Find available condition functions here

Plain Text

aws dynamodb query --table-name {TableName}

Plain Text

# aws dynamodb query \
--endpoint-url http://localhost:4566 \
--table-name myTable \
--key-condition-expression "id = :i" \
--expression-attribute-values '{":i":{"N":"1"}}'
  • key-condition-expression: Sets the default key condition.
  • expression-attribute-values: Sets the value to replace the attribute value in the condition expression.

Plain Text

# aws dynamodb query \
--endpoint-url http://localhost:4566 \
--table-name myTable \
--key-condition-expression "id = :i" \
--filter-expression "#u >= :num" \
--expression-attribute-names '{"#u":"user_id"}' \
--expression-attribute-values '{":i":{"N":"1"},":num":{"S":"2"}}'
  • filter-expression:setting non-key conditions
  • expression-attribute-names: Sets the value to replace the attribute name in the condition expression.

Plain Text

# aws dynamodb query \
--endpoint-url http://localhost:4566 \
--table-name myTable \
--key-condition-expression 'id = :a AND createdAt BETWEEN :t1 AND :t2' \
--expression-attribute-values '{
    ":a": {"N": "1"}, 
    ":t1": {"S": "2023-04-07 01:03:12"}, 
    ":t2": {"S": "2023-04-07 10:03:12"}
}'

Reference

an official document

민갤

Back-End Developer

백엔드 개발자입니다.