Introduction
DynamoDB is a fast and flexible NoSQL database service that AWS offers. And if you don’t know, NoSQL just means there is no structure to the database. (SQL = Structured Query Language, NoSQL = Non SQL)
For example, in the table below, there are three columns, First Name, Last Name, and Location. A row is an entry. A SQL database will need all columns filled out for a particular entry.
However, a non-SQL database can have multiple entries (rows) which don’t have data in all the columns as it takes in data without any required structure. An example is seen below
For this reason, if there’s a need to use a database to store data that doesn’t have a defined structure (this is what is referred to as a SCHEMA), DynamoDB is a great tool
Use Case
I work for a media production and distribution company that creates and distributes movies, TV shows, and other content to consumers. As part of the company’s operations, there is a need to manage and track information about various movies such as ratings, release date, etc.
I will be demonstrating how to create a DynamoDB table and use it to store and manage various information.
The following items will be done:
- A DynamoDB table with 10 latest movie releases including the title, genre, release date, and rating
- An EC2 instance that has an IAM role granting ReadOnly permissions to the DynamoDB table
- Confirm the EC2 instance cannot make any changes to the table.
Pre-Requisites
- An AWS account
- An IAM user with administrative privileges (optional)
Steps
- Log into your AWS account and navigate to the DynamoDB console
- Click on create table
- Name the table “mediacatalog”
- Enter in “title” as the partition key and ensure “String” is selected.
NOTE: The partition key is part of the table’s primary key. All items entered into this table will need to have this as it is used to retrieve items.
- Leave all other settings as default and create the table
- Wait for the table status to show as “Active”
- Click on the table name, click on Actions and select Create item
- Click on Add new attribute, select string
- Fill in rating in the added attribute
- Fill out the value fields for the title and rating attributes.
- Repeat the attribute creation step twice for “genre” and “release date” and fill out values for those
- Once this is done, click on create item
- Now you have an item for your first movie which includes the title, genre, rating and release date. Repeat this for 9 more movies
2. Navigate to the EC2 Console and click on Roles
- Click on create role
- Select “AWS Service” as Trusted Entity Type and “EC2” under common use cases
NOTE: The purpose of creating this role which when attached to an EC2 instance, the instance can use the role to only read items on the DynamoDB table.
- Search for DynamoDB in the permissions policies and select the “AmazonDynamoDBReadOnlyAccess” policy
- Give the role and name
- Create the role
3. Go to the EC2 console
- Click on Launch instance. Read my article here to get more information on how to do this.
- Select any AMI and choose t2.micro as the instance type
- Select a key pair and create/choose a Security group that allows SSH from your IP
- Go to Advanced details and under IAM instance profile, choose the role created earlier
- Launch the instance
- Use the EC2 instance connect feature on the console to SSH into your instance
- Run the command below and make sure no AWS Access Key and AWS Secret Access Key are associated with this instance.
DO NOT TYPE ANYTHING ONCE THESE PROMPTS SHOW UP. ONLY HIT THE ENTER KEY
This is another check to make sure the role we use works later on
aws configure
- Run the following command to scan the DynamoDB table named “mediacatalog”
aws dynamodb scan --table-name mediacatalog
You should see the items you created. When you get to the end, the total count of items in the table is seen as well.
This shows that the IAM role created works!
- Run the following command to attempt to add an item to the existing table. You should get a permissions error if everything was done properly as the IAM role doesn’t give permissions to add items to the table.
aws dynamodb put-item --table-name mediaCatalog --item "{\"title\":{\"S\":\"The Little Mermaid\"},\"rating\":{\"S\":\"7.2\"}}"
Success!
Clean-Up
- Terminate the EC2 instance
- Delete the DynamoDB table
Advanced
All of the steps we did could be done using the AWS Command Line Interface (CLI).
NOTE: The CLI has to be configured with AWS Credentials to do this. More information can be found here.
- Run the following command to create a DynamoDB table called “mediacatalog” using the CLI
The partition key is defined under Attribute definitions and Key schema in the command below. ReadWriteCapacityUnits and WriteCapacityUnits are both set at 1 to define the maximum number of reads and writes per second.
aws dynamodb create-table --table-name mediacatalog --attribute-definitions AttributeName=title,AttributeType=S --key-schema AttributeName=title,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
2. Download the “mediacatalog.json” file in the Github repository here.
- On the CLI, navigate to the same folder where the file is saved.
This file contains a PutRequest command and a list of various items which will be added to the DynamoDB table created.
- Run the following command to update the table with the items in the file
aws dynamodb batch-write-item --request-items file://mediacatalog.json
You should something similar to the screenshot below if the command was successful
3. Run the below command to launch an EC2 instance
NOTE: This instance uses an Amazon Linux 2023 AMI, a t2.micro instance type with a specified key pair, security group and attaches the role created earlier.
In the command below, make sure you use the instance profile ARN and not the ARN otherwise, you will get an error
aws ec2 run-instances --image-id ami-0f34c5ae932e6f0e4 --instance-type t2.micro --key-name ec2_kp --security-group-ids sg-05079fb7806d944d9 --iam-instance-profile Arn=arn:aws:iam::193527533080:instance-profile/EC2Instance_DynamoDBReadAccess
- You should see something similar to the screenshot below if it’s successful. Copy the InstanceId
4. Last 2 items are to perform a scan on the DynamoDB table using the CLI in the EC2 instance and confirm the instance cannot write an item to the DynamoDB table
- Navigate to the EC2 console and copy the connect command needed to SSH into the instance
- SSH into the instance
- Run the following command on the CLI to scan the DynamoDB table named “mediacatalog”
aws dynamodb scan --table-name mediacatalog
You will get the same result as earlier when this was done.
- Run the following command to attempt to add an item to the existing table. You should get a permissions error if everything was done properly as the IAM role doesn’t give permissions to add items to the table.
aws dynamodb put-item --table-name MediaCatalog --item "{\"title\":{\"S\":\"The Little Mermaid\"},\"rating\":{\"S\":\"7.2\"}}"
You will get the same result as earlier when this was done.
That’s all for today. Remember to clean up. Instructions are kept above for that.
Please let me know if you have any questions. Happy learning!