Introduction

Amazon DynamoDB is a fully managed NoSQL database service that offers fast and predictable performance with seamless scalability. Integrating DynamoDB with your AWS EC2 instances can significantly enhance your application’s efficiency. This blog post will guide you through setting up Amazon DynamoDB as a database for your AWS EC2 instances using AWS CLI. We will cover the following topics:

  1. Verifying the AWS SDK Installation
  2. Using Python’s Boto3 library as an Example
  3. Installing the AWS SDK if Missing
  4. Checking and Installing the AWS CLI
  5. Configuring AWS CLI Credentials and Region
  6. Accessing Credentials from the IAM Console
  7. Specifying the Default Region

1. Verifying the AWS SDK Installation

Before using Amazon DynamoDB, ensure the AWS SDK is installed on your system. The AWS SDK for Python (Boto3) allows Python developers to interact with AWS services.

Step-by-Step Guide:

  1. Open your terminal or command prompt.
  1. Run the following command to check if Boto3 is installed:

    pip show boto3
  1. If Boto3 is installed, you will see information about the package. If it’s not installed, proceed to the next section.

2. Using Python’s Boto3 Library as an Example

Boto3 is the Amazon Web Services (AWS) SDK for Python, which allows Python developers to write software that uses Amazon services.

Example Code:

import boto3

# Create a DynamoDB resource

dynamodb = boto3.resource(‘dynamodb’)

# Create a table

table = dynamodb.create_table(

    TableName=’ExampleTable’,

    KeySchema=[

        {

            ‘AttributeName’: ‘PrimaryKey’,

            ‘KeyType’: ‘HASH’  # Partition key

        },

    ],

    AttributeDefinitions=[

        {

            ‘AttributeName’: ‘PrimaryKey’,

            ‘AttributeType’: ‘S’

        },

    ],

    ProvisionedThroughput={

        ‘ReadCapacityUnits’: 5,

        ‘WriteCapacityUnits’: 5

    }

)

# Wait until the table exists

table.meta.client.get_waiter(‘table_exists’).wait(TableName=’ExampleTable’)

print(f’Table status: {table.table_status}’)

3. Installing the AWS SDK if Missing

If Boto3 is not installed, you can install it using pip.

Step-by-Step Guide:

  1. Open your terminal or command prompt.
  1. Run the following command to install Boto3:

    pip install boto3

4. Checking and Installing the AWS CLI

The AWS Command Line Interface (CLI) is a unified tool for managing your AWS services. Ensure it is installed on your system.

Step-by-Step Guide:

  1. Open your terminal or command prompt.
  1. Run the following command to check if AWS CLI is installed:

    aws –version
  1. If the AWS CLI is not installed, follow the installation instructions from the official AWS documentation.

5. Configuring AWS CLI Credentials and Region

After installing the AWS CLI, configure it with your AWS credentials and preferred region.

Step-by-Step Guide:

  1. Open your terminal or command prompt.
  1. Run the following command to start the configuration process:

    aws configure
  1. Enter the requested information:
  • AWS Access Key ID: Your access key ID
  • AWS Secret Access Key: Your secret access key
  • Default region name: Your preferred AWS region (e.g., us-west-2)
  • Default output format: Preferred output format (e.g., json)

6. Accessing Credentials from the IAM Console

To configure the AWS CLI, you need your AWS credentials. You can access these credentials from the IAM console.

Step-by-Step Guide:

  1. Sign in to the AWS Management Console.
  2. Navigate to the IAM service.
  3. Select Users from the left-hand menu.
  4. Choose the user you want to use.
  5. Go to the Security credentials tab.
  6. Under Access keys, create a new access key or use an existing one.

7. Specifying the Default Region

Setting the default region ensures AWS CLI commands are executed in the desired area.

Step-by-Step Guide:

  1. Open your terminal or command prompt.
  1. Run the following command:

    aws configure set region us-west-2
  2. Replace us-west-2 with your preferred region.

 

Conclusion

Following these steps, you have set up Amazon DynamoDB as a database for your AWS EC2 instances using AWS CLI. This setup allows you to leverage DynamoDB’s powerful features for your applications running on EC2 instances, ensuring scalability and performance.

References

Using the AWS CLI

Use Amazon DynamoDB with the AWS CLI