AWS DynamoDB with Terraform – Effortless Setup Guide

AWS DynamoDB with Terraform provides a clean and automated way to manage scalable NoSQL databases using Infrastructure as Code. With Terraform, you can provision tables, define attributes, and insert sample data—all in a predictable and repeatable way.

In this guide, we’ll walk through how to create a DynamoDB table, add attributes, configure billing, and insert items using Terraform—all while following best practices.

Why Use Terraform for DynamoDB?

Using AWS DynamoDB with Terraform brings several advantages:

  • Consistency: Define and version your database configuration in code.
  • Automation: Remove manual setup and errors through automated deployment.
  • Scalability: Easily reproduce and scale infrastructure across environments.

Terraform empowers DevOps teams to treat databases as code, enabling secure and repeatable deployments with minimal human error.

Creating a DynamoDB Table with Terraform

To begin, use the aws_dynamodb_table resource to create your table.

Essential Arguments

  • name: Unique table name.
  • hash_key: The primary key attribute.
  • attribute: One or more fields describing your table’s schema.

Here’s how you can define a table that stores vehicle data:

resource "aws_dynamodb_table" "cars" {
name = "cars"
hash_key = "vin"
billing_mode = "PAY_PER_REQUEST"

attribute {
name = "vin"
type = "S"
}
}

The hash_key (VIN – Vehicle Identification Number) uniquely identifies each item, and "S" signifies the attribute is a string.

Understanding Billing Modes in DynamoDB

DynamoDB offers two billing modes:

  1. Provisioned Mode: You specify read/write throughput.
  2. On-Demand Mode (PAY_PER_REQUEST): AWS handles throughput dynamically.

Using the billing_mode = "PAY_PER_REQUEST" line in your Terraform configuration opts you into on-demand billing. This is perfect for unpredictable workloads and reduces the need for manual capacity planning.

Inserting Items into the Table

To add data to the table you just created, use the aws_dynamodb_table_item resource.

Required Fields

  • table_name: Reference to the created table.
  • hash_key: The key used for lookups.
  • item: The data payload in JSON format.

Here’s a sample block:

resource "aws_dynamodb_table_item" "car_data" {
table_name = aws_dynamodb_table.cars.name
hash_key = "vin"

item = <<EOF
{
"vin": {"S": "1HGCM82633A004352"},
"make": {"S": "Honda"},
"model": {"S": "Accord"},
"year": {"N": "2020"}
}
EOF
}

Notice how each field uses a type-value pair like "S" for string and "N" for number, as required by DynamoDB.

Limitation of Item Resource in Terraform

While this method works well for basic data entry or creating demo datasets, it’s not designed for high-volume data insertion. If your use case involves inserting thousands of records, consider using AWS SDKs or data migration tools instead.

Terraform is best used for managing schema (tables, keys, policies), not for loading bulk data.

Managing Attributes and Keys

Besides the primary hash key, you can define sort keys or secondary indexes if needed.

Example for adding a sort key:

attribute {
name = "registration_date"
type = "S"
}

range_key = "registration_date"

This provides additional querying capabilities and is useful when organizing data that requires time or category-based lookups.

Secure and Reusable Configuration

As your Terraform setup grows, consider separating resource definitions into modules. Use variable files (.tfvars) to handle values like table names, key types, or environments (dev/stage/prod). This makes your setup more reusable and secure.

Avoid hardcoding secrets or sensitive values directly into your Terraform files.

Conclusion

Working with AWS DynamoDB with Terraform allows you to streamline the deployment and management of NoSQL data structures. By defining tables and inserting items through code, you reduce manual errors, improve visibility, and build infrastructure that scales efficiently.

For simple use cases like creating tables and loading sample data, Terraform provides a fast and declarative way to manage DynamoDB.

Frequently Asked Questions (FAQs)

1. Can I manage large datasets in DynamoDB using Terraform?

No. Terraform is best suited for provisioning infrastructure like tables or indexes—not for handling large-scale data operations.

2. Is on-demand billing better than provisioned mode?

It depends. On-demand is cost-effective for unpredictable workloads, while provisioned mode suits consistent traffic with fine-tuned capacity.

3. Can I define secondary indexes with Terraform?

Yes. You can add global secondary indexes (GSI) and local secondary indexes (LSI) using the respective blocks in your table configuration.

4. What format does DynamoDB expect for item data?

Item values must be JSON objects using type-specific keys like "S" for strings and "N" for numbers.

5. How do I prevent accidental deletions?

Use lifecycle rules in your resource blocks:

lifecycle { prevent_destroy = true }

This ensures Terraform won’t delete your DynamoDB table unless explicitly removed.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top