Create DynamoDB using Terraform

Everton Araújo
2 min readJan 22, 2023

--

Create simple DB

To create an Amazon DynamoDB table using Terraform, you can use the AWS provider and the aws_dynamodb_table resource. Here is an example of how to create a basic DynamoDB table called "example" with a primary key called "id":

provider "aws" {
region = "us-west-2"
}

resource "aws_dynamodb_table" "example" {
name = "example"
hash_key = "id"
billing_mode = "PROVISIONED"
read_capacity = 5
write_capacity = 5
}

This example creates a DynamoDB table named “example” with a primary key named “id” in the US West (Oregon) region. The table is created with provisioned throughput, a read capacity of 5 and write capacity of 5.

You can run this Terraform script by running the command terraform init, terraform plan and then terraform apply in your terminal.

Please note that this is a basic example, you will need to provide credentials to connect to your AWS account and also you need to have permissions to create DynamoDB tables on your AWS account.

You can also add items to the table, define global secondary indexes, and add other properties such as stream settings, Time to Live (TTL) and Point-in-time recovery (PITR)

resource "aws_dynamodb_table" "example" {
name = "example"
hash_key = "id"
billing_mode = "PROVISIONED"
read_capacity = 5
write_capacity = 5
stream_enabled = true
stream_view_type = "NEW_AND_OLD_IMAGES"
time_to_live_specification {
attribute_name = "ttl"
enabled = true
}
}

You can also use terraform import command to import existing DynamoDB table into your Terraform state.

And finish terraform plan and next terraform apply.

--

--