Hello Readers, I am here with another blog. In this blog, we will see the steps on how to create a was DynamoDB Table using Terraform. We will create a DynamoDB Table with the “PAY_PER_REQUEST” billing model.
DynamoDB
AWS DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. AWS DynamoDB lets you offload the administrative burdens of operating and scaling a distributed database so that you don’t have to worry about hardware provisioning, setup, and configuration, replication, software patching, or cluster scaling. You can follow this link.
In this blog, We will write a YAML file for the terraform to create a DynamoDB table. I have divided this blog into many steps. You can follow those steps and run them on your system.
Creating DynamoDB Table
Step 1:
First, we will create a directory to store the terraform file to create the resource which we want to create.
Step 2:
provider "aws" {
profile = "DynamoDB"
region = "us-east-2"
}
resource "aws_dynamodb_table" "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}"
}
}
Now, we will create a new file with the name main.tf. Here, We can define the region by our own choice.
Here,
- name: It is a mandatory field that is the name of the table which we are creating.
- billing_mode: This is an optional option. Controls the way, you are charged for read and write.
- hash_key: It is a mandatory field. It is used as the partition key for the table.
- attribute: It is a mandatory field that is the list of nested attribute definitions. Only required for hash_key and range_key attributes for the table.
- tags: This is an optional option. It is used to populate the table.
Step 3:
In this step, We need to set up the was credential like access key and secret key in the system. You just simply run the below command and set the keys.
aws configure --profile DynamoDB
Now, You can enter the keys and pass that name in terraform file as I mention above. Just like this:
provider "aws" {
profile = "DynamoDB"
region = "us-east-2"
}
Step 4:
In this step, We will create a variables.tf where we will define all the variables in it. You can change the value of all variables. You can follow this link to know more about terraform.
File: variables.tf
variable "table_name" {
description = "Dynamodb table name (space is not allowed)"
default = "Demo-first-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 = "demo"
}
Here, I have defined a few variables.
Step 5:
Now, We have defined all the files. According to our requirement for the DynamoDB table. We are ready to deploy terraform files to create DynamoDB.
Now, We will run the below terraform command in the working directory containing Terraform configuration files.
terraform init

Now, We are all set to run the terraform plan command. This command will create an execution plan. Now you can cross verify the change what and how many changes will happen after running this file.
terraform plan



Till now, We have initialized the terraform configuration file and check the execution plan of terraform. Now, We are ready to apply the changes required to reach the desired state.
terraform apply -auto-approve



Now, You can check the resource that is created or not. Go to the specified region and verify.



Step 6:
This is the point when we do not need the resource. So we can simply delete them. There is no need to go to the AWS console and delete it from there. You can simply run the below command and destroy all the resources after you confirm the deletion.
terraform destroy



Conclusion
In this article, we saw the steps to create a DynamoDB table using AWS access and secret keys. We also deleted the table using destroy command. You can follow this link to know more.