How to create a DynamoDB table on AWS using Terraform

In this article, we will see the steps to create a DynamoDB Table using Terraform. We will create a DynamoDB Table with the "PAY_PER_REQUEST" billing model. Before we proceed with this article, it is assumed that you are aware of AWS DynamoDB Service and know its basics, along with Terraform. 

Click here to learn more about DynamoDB arguments and properties available in Terraform.


  1. Basic understanding of Terraform.
  2. Terraform installed on your system.
  3. AWS Account (Create if you don’t have one).
  4. 'access_key' & 'secret_key' of an AWS IAM User with sufficient permissions to create DynamoDB table. (Click here to learn to create an IAM user with 'access_key' & 'secret_key' on AWS, )

What will we do?

  1. Write Terraform configuration files for DynamoDB Table.
  2. Create a DynamoDB table using the Terraform configuration files.
  3. Delete the created DynamoDB Table using Terraform.

Write Terraform configuration files for DynamoDB Table

Create a dedicated directory to write and store Terraform files to create a DynamoDB table.

Now, create a new file named "" and save the following code in it. The same code is also available on my Github repo. You can even copy the code from there. 

The following code will create a resource in "region = eu-west-3". Change the region as per your choice.

Github Link:

provider "aws" { access_key = "${var.access_key}" secret_key = "${var.secret_key}" region = "eu-west-3" } resource "aws_dynamodb_table" "my_first_table" { name = "${var.table_name}" billing_mode = "${var.table_billing_mode}" hash_key = "employee-id" attribute { name = "employee-id" type = "S" } tags = { environment = "${var.environment}" } }


  • name : (Required) The name of the table you are creating.
  • billing_mode :(Optional) Controls the way you are charged for read and write.
  • hash_key:(Required, Forces new resource) The attribute to use as the hash (partition) key for the table.
  • attribute: (Required) List of nested attribute definitions. Only required for hash_key and range_key attributes for the table.
  • tags: (Optional)  Tags to populate on the table.

The next step is to load your AWS access and secret keys in a file named as "terraform.tfvars". You should already have an AWS IAM user with secret and access keys having sufficient permissions on DynamoDB. Get those keys and store them in the file.

Github Link:
File: terraform.tfvars
access_key = "<your-aws-access-here>"
secret_key = "<your-aws-secret-here>"

The last step is to create "" and assign values to variables in it. You can change the values of table_name, table_billing_mode, and the environment variable. Click here to refer to Terraform documentation to check for the valid arguments/properties available. 

Github Link:
variable "access_key" {
        description = "Access key of AWS IAM user"
variable "secret_key" {
        description = "Secret key of AWS IAM user"

variable "table_name" {
  description = "Dynamodb table name (space is not allowed)"
  default = "my-first-test-table"

variable "table_billing_mode" {
  description = "Controls how you are charged for read and write throughput and how you manage capacity."
  default = "PAY_PER_REQUEST"

variable "environment" {
  description = "Name of environment"
  default = "test"


  • table_name: defined value = my-first-test-table. You can change this.
  • table_billing_mode: defined value = PAY_PER_REQUEST. You can either specify PROVISIONED or PAY_PER_REQUEST.
  • environment:  defined tag value = test. You can assign any value to the tag key environment.

Create a DynamoDB table using the Terraform configuration files

Once you have, terraform.tfvars, and you are set to create your DynamoDB Table using Terraform.

The following is the first command to initialize a working directory containing Terraform configuration files.

terraform init

terraform init

The next command is as follows to create an execution plan. Here, you can come to know what all changes will take place.

terraform plan

terraform plan

Now you are ready to apply the changes required to reach the desired state of the configuration using the following command. This will create a DynamoDB Table in your AWS account under the specified region.

terraform apply

terraform apply

Now, you can go to the AWS console and verify if the table is created or not.

DynamoDB Table

Delete the created DynamoDB Table using Terraform

When you no longer need the table and what to delete it, there is no need to go to the AWS console and delete it from there. Instead, you can delete it using the following command very easily. The following command will delete the table after you confirm the deletion.

terraform destroy

terraform destroy


In this article, we saw the steps to create a DynamoDB table with the "PAY_PER_REQUEST" billing model in the "region = eu-west-3" using AWS access and secret keys. We also deleted the table using destroy command. You can now extend the functionality by using different properties available and try on different use-cases.  

Share this page:

0 Comment(s)

Add comment

Please register in our forum first to comment.