Skip to content

AWS DynamoDB basics

AWS DynamoDB is a fully managed NoSQL database service, It’s designed to handle key-value and document data models, making it an ideal choice for applications that require low-latency and scalability.

DynamoDB is serverless, meaning we don’t have to manage the underlying infrastructure. It automatically scales up or down based on our workload and provides high availability through data replication.

Data in DynamoDB is stored and grouped as tables, at table contains items (rows) and attributes (columns).

Each item represents a single record in the table, an item is uniquely identified by its primary key.

Data in DynamoDB is stored physically in partitions, A Primary key determines where data is stored. we have 2 types of keys:

  • Partition Key: A single attribute used to distribute data.
  • Composite Key: A combination of partition key and sort key.

Attributes represent data elements within an item, it can be of types like string, number, boolean, list, or map.

Using DynamoDB, we pay per read and write capacity units per second, Capacity units can scale based on our setup to handle more traffic. we 2 types of capacities:

  1. Provisioned: We specify read/write capacity units.
  2. On-Demand: DynamoDB adjusts capacity automatically based on traffic.

to query data more flexibly and faster we us indexes, there 2 types of Indexes in DynamoDB:

  1. Local Secondary Index (LSI): Supports the same partition key but a different sort key.
  2. Global Secondary Index (GSI): Supports different partition and sort keys.

Make sure we have the AWS CLI installed and configured with our credentials.

Let’s create a table named Movies with a partition key MovieID (string).

Terminal window
aws dynamodb create-table \
--table-name Movies \
--attribute-definitions AttributeName=MovieID,AttributeType=S \
--key-schema AttributeName=MovieID,KeyType=HASH \
--billing-mode PAY_PER_REQUEST

In this command we have:

  • Defined MovieID as the partition key.
  • Used the Pay-Per-Request billing mode.

Let’s Add an item to the Movies table:

Terminal window
aws dynamodb put-item \
--table-name Movies \
--item '{"MovieID": {"S": "001"}, "Title": {"S": "Inception"}, "Genre": {"S": "Sci-Fi"}}'

To retrieve the item for the movie we have created:

Terminal window
aws dynamodb get-item \
--table-name Movies \
--key '{"MovieID": {"S": "001"}}'

We can Update an item in a DynamoDB table, for eg. let’s update the Genre of the Movie we have just created:

Terminal window
aws dynamodb update-item \
--table-name Movies \
--key '{"MovieID": {"S": "001"}}' \
--update-expression "SET Genre = :g" \
--expression-attribute-values '{":g": {"S": "Action"}}'

Let’s Remove the item movie with MovieID = 001:

Terminal window
aws dynamodb delete-item \
--table-name Movies \
--key '{"MovieID": {"S": "001"}}'

At the end, let’s clean up by deleting the table:

Terminal window
aws dynamodb delete-table \
--table-name Movies

In this tutorial, we understood what DynamoDB is and its main features, we Explored key concepts like tables, items, and primary keys. we performed basic operations like creating a table, inserting data, querying, updating, and deleting items and tables. We’re now equipped to handle DynamoDB’s core functionality to build applications.