Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

AWS DynamoDB Tutorial

What is DynamoDB?

AWS DynamoDB is a fully managed NoSQL database service provided by Amazon Web Services. It offers fast and predictable performance with seamless scalability. DynamoDB is designed to handle high-traffic applications and provides low-latency data access.

Key Features of DynamoDB

DynamoDB provides several key features:

  • Serverless: Automatically scales up or down based on your application's needs.
  • Performance: Offers single-digit millisecond response times.
  • Multi-Region Replication: Supports cross-region replication for high availability.
  • Integrated with AWS: Seamlessly integrates with other AWS services.
  • Flexible Data Models: Supports both document and key-value data structures.

Getting Started with DynamoDB

To start using DynamoDB, you need an AWS account. Once you have that, follow these steps:

  1. Log in to the AWS Management Console.
  2. Navigate to the DynamoDB service.
  3. Create a new table by specifying the table name and primary key attributes.

Creating a Table

Here's how to create a DynamoDB table using the AWS Management Console:

  1. Click on "Create table".
  2. Enter a name for your table (e.g., Users).
  3. Specify the primary key (e.g., UserId of type String).
  4. Configure any additional settings as needed.
  5. Click "Create".

Example: Creating a Table via AWS CLI

Use the following command in your terminal:

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

CRUD Operations

CRUD stands for Create, Read, Update, and Delete. Below are examples of how to perform these operations in DynamoDB:

Create an Item

To add an item to the Users table:

aws dynamodb put-item --table-name Users --item '{"UserId": {"S": "123"}, "Name": {"S": "John Doe"}, "Age": {"N": "30"}}'

Read an Item

To retrieve the item you just added:

aws dynamodb get-item --table-name Users --key '{"UserId": {"S": "123"}}'

Update an Item

To update the age of the user:

aws dynamodb update-item --table-name Users --key '{"UserId": {"S": "123"}}' --update-expression "SET Age = :val1" --expression-attribute-values '{":val1": {"N": "31"}}'

Delete an Item

To delete the item:

aws dynamodb delete-item --table-name Users --key '{"UserId": {"S": "123"}}'

Conclusion

AWS DynamoDB is a powerful NoSQL database service that can scale effortlessly with your applications. It is ideal for scenarios where you need high availability and low-latency access to data. By following the steps outlined in this tutorial, you should be able to create a table and perform basic CRUD operations.