How to use the command "aws dynamodb" (with examples)

How to use the command "aws dynamodb" (with examples)

The “aws dynamodb” command line interface (CLI) is used to interact with Amazon Web Services (AWS) DynamoDB, a fully managed NoSQL database service. It allows users to create tables, manage items, and perform various operations on the DynamoDB database.

Use case 1: Create a table

Code:

aws dynamodb create-table --table-name table_name --attribute-definitions AttributeName=S,AttributeType=S --key-schema AttributeName=S,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

Motivation: This use case is useful when you want to create a new table in the DynamoDB database.

Explanation:

  • --table-name: Specifies the name of the table to be created.
  • --attribute-definitions: Defines the attributes of the table with the AttributeName and AttributeType.
  • --key-schema: Defines the primary key attributes with the AttributeName and KeyType.
  • --provisioned-throughput: Specifies the provisioned throughput for the table, including ReadCapacityUnits and WriteCapacityUnits.

Example output:

{
    "TableDescription": {
        "TableName": "table_name",
        "TableStatus": "ACTIVE",
        ...
    }
}

Use case 2: List all tables in the DynamoDB

Code:

aws dynamodb list-tables

Motivation: This use case is useful for getting an overview of all the tables in the DynamoDB database.

Explanation: This command does not require any additional arguments. It lists all the tables in the DynamoDB database.

Example output:

{
    "TableNames": ["table1", "table2", "table3"],
    ...
}

Use case 3: Get details about a specific table

Code:

aws dynamodb describe-table --table-name table_name

Motivation: This use case is helpful when you need to retrieve detailed information about a specific table in the DynamoDB database.

Explanation:

  • --table-name: Specifies the name of the table to retrieve information.

Example output:

{
    "Table": {
        "TableArn": "arn:aws:dynamodb:region:1234567890:table/table_name",
        "TableStatus": "ACTIVE",
        ...
    }
}

Use case 4: Add an item to a table

Code:

aws dynamodb put-item --table-name table_name --item '{"AttributeName": {"S": "value"}}'

Motivation: This use case is useful for inserting a new item into a table in the DynamoDB database.

Explanation:

  • --table-name: Specifies the name of the table where the item will be added.
  • --item: Defines the attributes and their respective values for the new item in JSON format.

Example output: (No output is returned upon successful execution)

Use case 5: Retrieve an item from a table

Code:

aws dynamodb get-item --table-name table_name --key '{"ID": {"N": "1"}}'

Motivation: This use case is helpful when you need to retrieve a specific item from a table in the DynamoDB database.

Explanation:

  • --table-name: Specifies the name of the table to retrieve the item from.
  • --key: Defines the primary key attribute and its value for the item to be retrieved in JSON format.

Example output:

{
    "Item": {
        "ID": {"N": "1"},
        "AttributeName": {"S": "value"},
        ...
    }
}

Use case 6: Update an item in the table

Code:

aws dynamodb update-item --table-name table_name --key '{{{"ID": {"N": "1"}}}}' --update-expression "{{SET Name = :n}}" --expression-attribute-values '{{{":n": {"S": "Jane"}}}}'

Motivation: This use case is useful when you want to update the attributes of an item in a table in the DynamoDB database.

Explanation:

  • --table-name: Specifies the name of the table where the item will be updated.
  • --key: Defines the primary key attribute and its value for the item to be updated in JSON format.
  • --update-expression: Specifies the update expression to be performed on the item.
  • --expression-attribute-values: Defines the attribute values used in the update expression in JSON format.

Example output: (No output is returned upon successful execution)

Use case 7: Scan items in the table

Code:

aws dynamodb scan --table-name table_name

Motivation: This use case is useful when you want to retrieve multiple items from a table in the DynamoDB database.

Explanation:

  • --table-name: Specifies the name of the table to scan.

Example output:

{
    "Items": [
        {
            "ID": {"N": "1"},
            "AttributeName": {"S": "value"},
            ...
        },
        ...
    ],
    ...
}

Use case 8: Delete an item from the table

Code:

aws dynamodb delete-item --table-name table_name --key '{{{"ID": {"N": "1"}}}}'

Motivation: This use case is helpful when you want to remove a specific item from a table in the DynamoDB database.

Explanation:

  • --table-name: Specifies the name of the table where the item will be deleted from.
  • --key: Defines the primary key attribute and its value for the item to be deleted in JSON format.

Example output: (No output is returned upon successful execution)

Conclusion:

The “aws dynamodb” command provides a powerful interface for managing tables, items, and operations in the AWS DynamoDB database. With its extensive range of commands and options, users can easily create, retrieve, update, and delete items from DynamoDB tables.

Related Posts

Securely Deleting Files with Shred (with examples)

Securely Deleting Files with Shred (with examples)

1: Overwriting a file Code shred path/to/file Motivation When we want to securely delete a file, simply deleting it from the file system does not guarantee that the data within the file is completely erased.

Read More
How to use the command fdupes (with examples)

How to use the command fdupes (with examples)

fdupes is a command-line tool that helps in finding duplicate files within a given set of directories.

Read More
How to use the command "warp-cli" (with examples)

How to use the command "warp-cli" (with examples)

“warp-cli” is the official command-line client for Cloudflare’s WARP service. It allows users to register devices, connect to the WARP service, disconnect from it, check the connection status, and access help documentation.

Read More