Introduction

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. Integrating DynamoDB with your AWS EC2 instance allows you to leverage its high throughput and low latency for your applications. This guide will walk you through setting up DynamoDB, configuring the table, making additional adjustments, connecting from your EC2 instance, and assigning IAM roles.

Table of Contents

  1. Creating a DynamoDB Table
  2. Setting Table Configurations
  3. Making Additional Adjustments
  4. Connecting to the Table from Your EC2 Instance
  5. Assigning IAM Roles to Your Instance

Creating a DynamoDB Table

To create a DynamoDB table, follow these steps:

  1. Log in to the AWS Management Console and navigate to the DynamoDB service.
  2. Click on “Create table”.
  3. Enter the table name and primary key attributes. For instance, let’s create a table named Users with UserId as the primary key (type String).

Example CLI command:

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

Setting Table Configurations

Once the table is created, you might want to set additional configurations:

  1. Provisioned Throughput: Adjust read and write capacity units based on your application needs. You can also enable auto-scaling.
  2. Global Secondary Indexes (GSIs): Add GSIs if you need additional query capabilities.
  3. Stream Settings: Enable DynamoDB Streams if you need to capture item-level changes.

Example CLI command to update throughput:

aws dynamodb update-table \

    –table-name Users \

    –provisioned-throughput \

        ReadCapacityUnits=10,WriteCapacityUnits=10

Making Additional Adjustments

You might need to make further adjustments to your table:

  1. Enabling TTL (Time to Live): You can automatically delete items after a specified time.
  2. Encryption: Ensure that your data is encrypted at rest.
  3. Tags: Tag your DynamoDB tables for better management and cost tracking.

Example CLI command to enable TTL:

aws dynamodb update-time-to-live \

    –table-name Users \

    –time-to-live-specification “Enabled=true, AttributeName=ExpirationTime”

 

Connecting to the Table from Your EC2 Instance

To connect your EC2 instance to the DynamoDB table, follow these steps:

  1. Install AWS SDK: Ensure that the AWS SDK for your programming language (e.g., Boto3 for Python) is installed.
  2. Configure AWS Credentials: Set up AWS credentials on your EC2 instance.

Example Python code to connect to DynamoDB:

import boto3

# Create a DynamoDB client

dynamodb = boto3.resource(‘dynamodb’, region_name=’us-west-2′)

# Select the table

table = dynamodb.Table(‘Users’)

# Insert an item

table.put_item(

    Item={

        ‘UserId’: ‘123’,

        ‘Name’: ‘John Doe’,

        ‘Email’: ‘john.doe@example.com’

    }

)

# Retrieve an item

response = table.get_item(

    Key={

        ‘UserId’: ‘123’

    }

)

print(response[‘Item’])

 

Assigning IAM Roles to Your Instance

To securely access DynamoDB from your EC2 instance, you need to assign an appropriate IAM role:

  1. Create an IAM Role: Create an IAM role with policies that grant access to DynamoDB.
  2. Attach the Role to Your EC2 Instance: Attach the IAM role to your EC2 instance.

Example policy for DynamoDB access:

{

    “Version”: “2012-10-17”,

    “Statement”: [

        {

            “Effect”: “Allow”,

            “Action”: [

                “dynamodb:PutItem”,

                “dynamodb:GetItem”,

                “dynamodb:UpdateItem”,

                “dynamodb:DeleteItem”

            ],

            “Resource”: “arn:aws:dynamodb:us-west-2:123456789012:table/Users”

        }

    ]

}

Steps to attach IAM role to an EC2 instance:

  1. Navigate to the EC2 Dashboard.
  2. Select the instance you want to attach the IAM role to.
  3. Choose Actions > Instance Settings > Attach/Replace IAM Role.
  4. Select the IAM role you created and attach it.

Conclusion

Integrating DynamoDB with your AWS EC2 instance can significantly enhance your application’s performance and scalability. Following the steps outlined in this guide, you can create and configure a DynamoDB table, make necessary adjustments, connect to it from your EC2 instance, and securely manage access with IAM roles.

References

Launch an Amazon EC2 instance

 Configure an Amazon EC2 instance