How to Create a DynamoDB Table

Hoov
6 min readMar 2, 2023

--

Introduction

What’s going on everyone. This project will focus on creating a DynamoDB Table along with some other Objectives along the way.

What’s Amazon DynamoDB?
Amazon DynamoDB is a database service that provides fast and predictable performance with seamless scalability. With Amazon DynamoDB you don’t have to provision a database or underlying machine. All you need to do is create a table and start writing and reading data from it. It’s production ready right from the start.

Objectives

  1. Create a DynamoDB table for something of your choosing (e.g. movies, food, games)
  2. Add 10 or more items to the table
  3. Create a t.2micro Ec2 instance
  4. Using an IAM role and the principle of least privilege, grant the EC2 instance read access to DynamoDB.
  5. Use the AWS CLI in the EC2 instance to scan the DynamoDB table
  6. Use the AWS CLI in the EC2 instance to validate you cannot write an item to the DynamoDB table

Prerequisites

AWS Account
Terminal for Mac or PowerShell for Windows

Objective #1: Create DynamoDB Table

First lets start things off by searching for “DynamoDB” in the AWS dashboard.

From there, lets click on “Create Table”.

Now lets add a Table Name, Partition Key, and Sort Key.

We’ll leave the Table Settings and Tags default. Let’s click Create Table.

After hitting Create Table you should see a message stating that the Table was created successfully.

Objective #1 is complete. Let’s move on to the next!

Objective #2: Add 10 or more items to the table

Now it’s time to add items to the table. Let’s start by selecting the table we just created and clicking “Explore Items”.

Now lets enter what we need to enter in the Partition Key and Sort Key Field. My Partition Key is Event_Type and Sort Key is Event.

Repeat the process 10 times. As you can see below, 10 items were created.

This completes Objective #2.

Objective #3: Create a t.2micro EC2 instance

In the AWS console, let's search and navigate to the EC2 dashboard. From there select “Launch Instance”.

Choose a name for your instance and select the AMI.

Now lets move on to the instance type and key pair. t2.micro was stated in the objective so we’ll use that. I’ll be using a Key Pair I previously created.

Next up is the network settings. I’m allowing SSH traffic from my IP address. For security purposes I have it blacked out. HTTPS and HTTP was automatically selected as well so I left it as is but you don’t need to have those checked for this particular project.

Configure Storage and Advanced Details will remain default. Now that we have everything configured, let’s launch the instance.

You should see a success message stating that the instance was successfully initiated. You can also go to the EC2 dashboard and click instances to see that the newly created instance is now running.

Objective #3 is now complete.

Objective #4: IAM Role & Read Access Privilege

Lets navigate to the IAM Dashboard in the search bar. From there select Roles > Create Role

For Trusted Entity Type select AWS service. For use cases select EC and the hit Next.

Now it’s time to add permissions. Search for “AmazonDynamoDB” and select the Read Only option that comes up in the search. As you can see in the description this provides READ ONLY ACCESS.

Give your role a name and a brief description. Once you’ve done that select “Create Role” at the bottom of the screen.

You should see “Role Week-8-Project Created” and the newly created Role under Roles.

Now lets navigate back to the EC2 instance dashboard, select our running instance and select Actions > Security > Modify IAM Role

Choose the IAM role we created earlier and then hit “Update IAM Role”

Objective #4 is now complete. 2 more to go!

Objective #5: Scan the DynamoDB Table in AWS CLI

Back at the EC2 dashboard lets select our running instance and connect to it.

Now let’s SSH into our instance. Be sure to change directories to where your Key File is saved.

The next step is to Scan the DynamoDB table using the command:

aws dynamodb scan — table-name <table_name> — region <region_name>

So far so good. Objective #5 is complete. On to the last one.

Objective #6: Validating Read Only Access

Now we have to ensure that the Role we created earlier is in place. Let’s see if we can write anything to the DynamoDB table we created. Keep in mind, we should have Read Only Access. Lets do that by using the command:

aws dynamodb put-item — table-name <TABLE NAME> — region <REGION> — item ‘{“<PARTITION KEY>”: {“S”: “<NAME>”}, “<SORT KEY>”: {“S”: “<NAME>”}}’

The command may look a tad bit confusing but seeing it in the terminal makes it much easier to understand. My table name I created earlier was Track_Field, the region for me is us-east-1, the partition key is Event_Type, the name for the event is Long Distance, the sort key is Event, and the name is 800m Run.

As you can see an error occured because we have Read Only Access which validates the role is in place and our permissions are set to Read Only.

As always we don’t want run up a bill by leaving our EC2 instance running so lets navigate back to the EC2 dashboard and stop the instance.

Week 8 project is complete. I’m looking forward to the next challenge ahead and I’ll definitely be posting about it. Thanks for reading!

--

--