Skip to main content

Command Palette

Search for a command to run...

Creating a DynamoDB Table on AWS Using Terraform

Published
β€’3 min read
P

πŸ‘‹ Hello, and welcome to my DevOps journey! πŸš€ I am Priyanka Varshney,πŸ› οΈ As an aspiring DevOps engineer, I'm all about bridging the gap between development and operations, making software delivery seamless and efficient. πŸ’»πŸ”§ On this Hashnode blog, I'll be sharing my learnings, experiences and adventures as I dive deep into the world of continuous integration, automation, and cloud technologies. β˜οΈβš™οΈ Let's connect, learn, and grow as a vibrant DevOps community. Follow my Hashnode blog, and let's embrace the DevOps adventure together! πŸ€πŸ”—

In the previous blog, we learned how to create a local resource or file using Terraform concepts.

Now, let's take it a step further and create a DynamoDB table on AWS using Terraform.

Prerequisites

  • Launch an EC2 instance and install Terraform (refer to the previous blog if needed).

  • Ensure your EC2 instance has internet access to download dependencies.

    Step 1: Create the DynamoDB Configuration File

    Open the Vim editor to create a Terraform file:

      vim DynamoDB.tf
    

    Paste the following Terraform configuration to create a DynamoDB table:

      resource "aws_dynamodb_table" "terraform_lock" {
        name         = "terraform_state"
        billing_mode = "PAY_PER_REQUEST"
        hash_key     = "LockID"
    
        attribute {
          name = "LockID"
          type = "S"
        }
      }
    

    Define the Terraform Provider

    Create a new file to specify the AWS provider configuration:

      vim terraform.tf
    

    Add the following content:

      terraform {
        required_providers {
          aws = {
            source  = "hashicorp/aws"
            version = "5.92.0"
          }
        }
      }
    
  • Step 3: Initialize Terraform

    Run the following command to initialize your Terraform project:

      terraform init
    

    This will download the necessary provider plugins.

    Step 4: Install and Configure AWS CLI

    Install the AWS CLI to connect Terraform with your AWS account:

      curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
      sudo apt-get install unzip
      unzip awscliv2.zip
      sudo ./aws/install
    

    Now configure the CLI with your credentials:

      aws configure
    

    You'll be prompted for:

    • Access Key ID

    • Secret Access Key

    • Default region (e.g., us-east-1)

    • Output format (you can leave this as json)

Note : Need to install the Unzip in order to unzip the file sudo apt-get install unzip

Step 5: Create IAM User and Access Keys

  • Go to the IAM service and click on users from the Dashboard.

    1. Create a user named Terra-admin and give the permission AmazonS3FullAccess by clicking on attach policies directly.

      1. Click on next and then create user.

Now Terra-admin user is created successfully.

  1. To obtain the access key, navigate to the user profile, select "Security Credentials," create a security key, and then choose the "Command Line Interface (CLI)" option. After that, confirm your action and proceed by clicking "Next."

5. Now you will get two access key ID

  1. Now you can copy paste the keys in your local machine

    Step 6: Apply Terraform Configuration

    Now you're ready to create the DynamoDB table:

     terraform apply
    

    Type yes when prompted to confirm.

Step 7: Verify the Table Using AWS CLI

To list all DynamoDB tables:

    aws dynamodb list-tables

You should see your terraform_state table listed.

You can also visit the DynamoDB console on AWS to visually confirm the table was created successfully.

βœ… Congratulations! You've now provisioned a DynamoDB table on AWS using Terraform.

Happy learning :)