Deploy AWS DynamoDB using Terraform

DevOps Archaeologist
2 min readApr 23, 2021

Deploying AWS DynamoDB with Terraform: Infrastructure as Code Made Easy

DynamoDB, a NoSQL database service offered by AWS, is a popular choice for applications requiring scalable and flexible data storage. Terraform, an infrastructure as code (IaC) tool, allows you to define and manage your AWS resources efficiently. This article guides you through deploying a DynamoDB table using Terraform.

What You’ll Need:

  • An AWS account with appropriate permissions.
  • Terraform installed and configured on your local machine. https://www.terraform.io/
  • Basic understanding of Terraform syntax and AWS concepts.

Step 1: Setting Up Terraform

  1. Create a Working Directory: Create a directory to store your Terraform configuration files.
  2. Initialize Terraform: Navigate to the directory and run terraform init to initialize the project and download required plugins.

Step 2: Configure Terraform for AWS

  1. Provider Configuration: Create a file named provider.tf with the following content, replacing <your_profile_name> with your AWS profile name:

Terraform

provider "aws" {
region = "us-east-1" # Update with your desired region
profile = "<your_profile_name>"
}
  • Credentials: Ensure your AWS credentials are properly configured (environment variables, shared credentials file).

Step 3: Define Your DynamoDB Table

  • Resource Definition: Create a file named dynamodb_table.tf and define your DynamoDB table configuration:

Terraform

resource "aws_dynamodb_table" "my_table" {
name = "my-table-name"
billing_mode = "PAY_PER_REQUEST"
hash_key {
name = "id"
type = "S" # String data type for the hash key
}
attribute {
name = "name"
type = "S"
}
tags = {
Environment = "dev"
}
}
  • Replace my-table-name with your desired table name.
  • Adjust attribute definitions for additional table attributes and data types (e.g., Number, String, Binary).

Step 4: Apply the Configuration

  • Preview Changes: Run terraform plan to preview the changes Terraform will make to your AWS environment.
  • Provision Resources: Once satisfied with the plan, run terraform apply to create the DynamoDB table in your AWS account.

Step 5: Verify Deployment

  • Log in to the AWS Management Console and navigate to the DynamoDB service.
  • You should see your newly created table listed.

Additional Considerations:

  • Security: Implement IAM policies to control access to your DynamoDB table.
  • Scalability: Terraform can manage provisioning additional read/write capacity units for your table as needed.
  • State Management: Configure Terraform state management (e.g., remote backend) for collaboration and disaster recovery.

Happy Terraforming!

--

--