How to Deploy DynamoDB with Terraform on AWS
DEV Community

How to Deploy DynamoDB with Terraform on AWS

How to Deploy DynamoDB Tables on AWS Using Terraform Infrastructure as Code allows us to define cloud infrastructure using configuration files instead of creating resources manually through the AWS Console. In this tutorial, we'll use Terraform to create and manage two Amazon DynamoDB tables: Orders Products By the end, you'll have both tables deployed to AWS and managed entirely through Terraform. ๐ŸŽฏ What We Are Building The final infrastructure will look like this: Terraform โ”‚ โ–ผ AWS Provider โ”‚ โ–ผ DynamoDB โ”œโ”€โ”€ Orders โ”‚ โ””โ”€โ”€ OrderId โ”‚ โ””โ”€โ”€ Products โ””โ”€โ”€ ProductId The Orders table will use OrderId as its partition key, while the Products table will use ProductId . Prerequisites Before starting, make sure you have: - An AWS account - Terraform installed - AWS credentials configured for Terraform - An AWS identity with permissions to create and manage DynamoDB tables You can verify Terraform is installed with: terraform version You should also make sure Terraform can authenticate with AWS before continuing. ๐Ÿ“ Step 1: Create the Terraform Files Create a directory for the Terraform configuration. Inside it, create these four files: terraform/ โ”œโ”€โ”€ providers.tf โ”œโ”€โ”€ variables.tf โ”œโ”€โ”€ terraform.tfvars โ””โ”€โ”€ dynamodb.tf Each file has a specific purpose: | File | Purpose | |---|---| providers.tf | Terraform and AWS provider configuration | variables.tf | Variable definitions | terraform.tfvars | Values for those variables | dynamodb.tf | DynamoDB table configuration | Terraform automatically loads all .tf files in the same directory, so there is no need to reference each file individually. โš™๏ธ Step 2: Configure the AWS Provider Open providers.tf and add: terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 6.0" } } required_version = ">= 1.6.0" } provider "aws" { region = var.aws_region } There are two important parts here. Terraform version required_version = ">= 1.6.0" This specifies the Terraform versions supported by the configuration. AWS provider required_providers { aws = { source = "hashicorp/aws" version = "~> 6.0" } } The AWS provider allows Terraform to communicate with AWS APIs. The provider is what allows Terraform to create and manage AWS resources such as DynamoDB tables. ๐ŸŒ Step 3: Define the Variables Open variables.tf : variable "aws_region" { type = string description = "The AWS region to deploy resources in" default = "your-aws-region" } variable "order_table_name" { type = string description = "The name of the DynamoDB order table" default = "Orders" } variable "product_table_name" { type = string description = "The name of the DynamoDB product table" default = "Products" } These variables allow us to keep configuration values separate from our infrastructure definitions. ๐Ÿ“ Step 4: Set the Variable Values Now open terraform.tfvars : aws_region = "your-aws-region" order_table_name = "Orders" product_table_name = "Products" Replace: your-aws-region with the AWS region where you want to deploy the tables. For example: aws_region = "us-east-1" Using variables makes it easier to reuse the same Terraform configuration across different environments or AWS regions. ๐Ÿ—„๏ธ Step 5: Define the DynamoDB Tables Now we can create the actual DynamoDB infrastructure. Open dynamodb.tf : module "dynamodb_order_table" { source = "terraform-aws-modules/dynamodb-table/aws" name = var.order_table_name hash_key = "OrderId" billing_mode = "PAY_PER_REQUEST" attributes = [ { name = "OrderId" type = "S" } ] } module "dynamodb_product_table" { source = "terraform-aws-modules/dynamodb-table/aws" name = var.product_table_name hash_key = "ProductId" billing_mode = "PAY_PER_REQUEST" attributes = [ { name = "ProductId" type = "S" } ] } We are using the community-maintained: terraform-aws-modules/dynamodb-table/aws Terraform module to simplify the DynamoDB configuration. ๐Ÿง  Understanding the DynamoDB Configuration Let's break down the important parts. source source = "terraform-aws-modules/dynamodb-table/aws" This tells Terraform which module to use for creating the DynamoDB table. Modules allow us to reuse existing Terraform configuration instead of implementing every resource detail ourselves. name name = var.order_table_name This determines the DynamoDB table name. Because we're using a variable, the name can be changed without modifying the resource configuration. hash_key hash_key = "OrderId" This defines the DynamoDB partition key. For the Orders table: OrderId is the partition key. For the Products table: ProductId is the partition key. billing_mode billing_mode = "PAY_PER_REQUEST" This configures DynamoDB's on-demand capacity mode. With PAY_PER_REQUEST , we don't need to manually configure provisioned read and write capacity. This is convenient for workloads where traffic can vary significantly. attributes For the Orders table: attributes = [ { name = "OrderId" type = "S" } ] S means the attribute is a string. The important thing to understand is that DynamoDB does not require us to define every attribute that an item might contain. For example, an order could look like: { "OrderId": "order-123", "CustomerId": "customer-456", "ProductId": "product-789", "Quantity": 2, "OrderStatus": "PENDING" } We don't need to declare CustomerId , ProductId , Quantity , or OrderStatus in the Terraform attributes block unless they participate in the table's key schema or indexes. ๐Ÿ” Step 6: Make Sure AWS Permissions Are Available Terraform communicates with AWS through API calls. Therefore, the AWS identity used by Terraform needs the required DynamoDB permissions. Depending on the configuration, permissions can include: dynamodb:CreateTable dynamodb:DescribeTable dynamodb:DeleteTable dynamodb:UpdateTable dynamodb:TagResource dynamodb:ListTagsOfResource The exact permissions required depend on what Terraform needs to create and manage. The important concept is: Terraform itself does not bypass AWS permissions. Every AWS operation performed by Terraform is subject to IAM authorization. ๐Ÿš€ Step 7: Initialize Terraform Now that the configuration is ready, initialize Terraform. Run: terraform init Terraform will download the required AWS provider and initialize the modules used by the configuration. You should run terraform init whenever you start working with a new Terraform configuration or when provider/module requirements change. โœ… Step 8: Validate the Configuration Before deploying anything, validate the Terraform configuration: terraform validate If the configuration is valid, Terraform will report: Success! The configuration is valid. Validation checks the configuration itself. It does not deploy anything to AWS. ๐Ÿ”Ž Step 9: Preview the Deployment Next, run: terraform plan Terraform will calculate the changes required to make the AWS environment match the configuration. You should see Terraform planning to create the two DynamoDB tables. Conceptually: Terraform Configuration โ†“ terraform plan โ†“ Changes Terraform intends to make This step is extremely important. Before allowing Terraform to modify infrastructure, review the plan. ๐Ÿš€ Step 10: Deploy the DynamoDB Tables Once you're satisfied with the plan, run: terraform apply Terraform will display the planned changes and ask for confirmation. After confirmation, Terraform will create the DynamoDB tables in AWS. The flow is: Terraform Configuration โ†“ terraform plan โ†“ Review Changes โ†“ terraform apply โ†“ AWS DynamoDB ๐Ÿ” Step 11: Verify the Terraform Resources After the deployment completes, you can check the resources Terraform is managing with: terraform state list You should see entries corresponding to the two DynamoDB tables, such as: module.dynamodb_order_table.aws_dynamodb_table.this[0] module.dynamodb_product_table.aws_dynamodb_table.this[0] You can also verify the tables directly in the AWS DynamoDB console. You should now have: Orders Partition Key: OrderId Products Partition Key: ProductId โœ… Step 12: Run Terraform Plan Again Finally, run: terraform plan If everything matches the Terraform configuration, Terraform should report that there are no changes to make. For example: No changes. Your infrastructure matches the configuration. This means Terraform has reached the desired state defined by the configuration. ๐Ÿ—๏ธ Final Result We have successfully defined and deployed two DynamoDB tables using Terraform: Terraform โ”‚ โ–ผ AWS Provider โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ–ผ โ–ผ DynamoDB DynamoDB Orders Products โ”‚ โ”‚ OrderId PK ProductId PK The complete workflow is: Create Terraform files โ†“ Configure AWS provider โ†“ Define variables โ†“ Define DynamoDB tables โ†“ terraform init โ†“ terraform validate โ†“ terraform plan โ†“ terraform apply โ†“ Verify resources โ†“ terraform plan โ†“ No changes ๐Ÿ’ก Key Takeaways The important concepts from this tutorial are: Terraform configuration describes the desired infrastructure. The AWS provider allows Terraform to communicate with AWS. Terraform modules simplify resource configuration and reuse. DynamoDB tables can be defined entirely through Terraform. terraform init prepares the Terraform working environment.terraform validate checks the configuration.terraform plan shows what Terraform intends to change.terraform apply actually creates or modifies AWS infrastructure.Terraform state tracks the infrastructure Terraform manages. Running terraform plan after deployment should show no changes when the infrastructure matches the configuration. The main idea is simple: Define your AWS infrastructure as code, review the planned changes, and let Terraform create and manage the resources for you. Top comments (0)

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.