DynamoDB, Movies, and Me
Welcome to CineJakia, where we have entertainment for everyone! Today we will be exploring databases with DynamoDB. But first, what is DynamoDB and why is it used?
DynamoDB is a fully managed and adaptable NoSQL database service that can provide high availability, low-latency, and scalability for applications. It is popular amongst organizations due to its ability to handle a wide range of use cases, such as simple key-value stores to complex, globally distributed systems.
So grab some popcorn, check out the newest releases, and even learn how to create a DynamoDB table for your own movie theater. It’s time to sit back and enjoy the show!
Objectives:
- Create a DynamoDB table for the latest Movie releases
- Add 10 latest movie releases to the table, including the title, genre, release date, and rating.
- Create a t.2micro Ec2 instance
- Using an IAM role and the principle of least privilege, grant the EC2 instance read access to DynamoDB.
- Use the AWS CLI in the EC2 instance to scan the DynamoDB table
- Use the AWS CLI in the EC2 instance to validate that you cannot write an item to the DynamoDB table.
Step 1: Create a DynamoDB table
Begin by accessing the DynamoDB console in AWS. Click Create table and give your table a name. Enter “Title” as the partition key and “Release date” as the sort key. To help organize your data and enable efficient querying of this data, you will need a sort key. A sort key is particularly helpful since you will be dropping several movies on the same day at your theater.
Leave the table settings as default and click Create table.
Step 2: Add items to your table
Once your table is created, click on the table’s name so that you can add the latest blockbusters.
Click on Explore items in the sidebar and then click Create item.
To organize your table, you will need a few attributes. First, click Add new attribute and select String from the dropdown menu. Since your viewers will need additional information to help them decide on a movie, you will need to add the genre and rating attributes. Click Create item.
No one will want to visit your theater if you are only showing one movie, so let’s add more to the lineup. Click Create item and repeat the last step nine more times with more movies.
You should have a list of 10 movies when you’re finished.
Step 3: Launch an EC2 Instance
Next, you will need to launch an EC2 instance so that your moviegoers can check out your latest showings.
Access the EC2 console in AWS and click Launch instance. Give your instance a name and select an AMI (Choose a free one, we want to keep more money in our pocket).
Select a t2.micro instance type and a key pair so that you can securely connect to your instance. Now click Launch instance.
Step 4: Create an IAM role
Since you want your EC2 instance to be able to read the DynamoDB table, you will need to give it some permissions.
In the IAM console, click Roles in the sidebar and then click Create role.
Select AWS service as the trusted entity type and use EC2 as the common use case. Click Next.
Search DynamoDB in the permission policies and select the box next to “AmazonDynamoDBReadOnlyAccess” and click Next.
Give your role a name. Everything else can be left as the default settings. Click Create role.
Step 5: Modify the IAM role of your EC2 instance
Now you need to attach the IAM role you just created to your EC2 instance.
Head to the EC2 console and select the box next to your instance. Click Actions and Modify IAM role.
Find and select the IAM role you just created from the dropdown menu. Click Update IAM role.
Now it’s time to connect. Select the box next to your instance and click Connect.
Click on the SSH client tab and copy the command under the example.
Step 6: Scan the DynamoDB table in the CLI
In the CLI, change directories into the file that contains your key pair, then paste the copied command into the CLI to SSH into your instance.
We’ll need to go ahead and update the server, so run the following command:
sudo yum update
The installation is complete.
Let’s scan the DynamoDB table to ensure that read access has been enabled.
aws dynamodb scan --table-name <insert table name> --region us-east-1
When you see this, you will know it’s a job well done!
Now let’s see if we can write to the table. If your permissions are enabled correctly, this command should NOT work since we enabled it as read only.
aws dynamodb put-item --table-name CineJakia --region us-east-1 --item '{"Title": {"S": "Paint"}, "Release date": {"S": "04-07-2023"}, "Genre": {"S": "Comedy"}, "Rating": {"S": "PG-13"}}' --return-consumed-capacity TOTAL
This is one of the few occasions where seeing an AccessDenied message is great.
Mission complete, now your customers are ready to enjoy their movie! It’s showtime!
Be sure to follow me for more tips as I continue my journey through the cloud!