Creating Amazon DynamoDB table using Cloud Formation Template

Creating Amazon DynamoDB table using Cloud Formation Template

Introduction :-

Using a #CloudFormation template, you can easily create a #DynamoDB table along with its required configuration settings. The template provides a structured way to define the table’s properties, such as the table name, attribute definitions, key schema, provisioned throughput, global secondary indexes (if needed), and other advanced options.

By utilizing CloudFormation, you can automate the creation and management of your #DynamoDB tables as part of your infrastructure deployment process. This helps ensure #consistency, reproducibility, and scalability across different environments and reduces #manual effort.

Pre-Requirements :-

  • AWS account with required permissions.

Step 1:- Create a directory named dynamodb in your system, under that dynamo db folder create a files called template.yaml and params.json as shown below.

Step 2:- Define the template format version, parameters, resources and outputs in the template.yaml file. refer the below example -

AWSTemplateFormatVersion: 2010-09-09
Description: AWS CloudFormation Template for the DynamoDB
Parameters:
  EnvironmentName:
    Description: Environment name for the application dev/staging/production
    Type: String
    AllowedValues:
      - dev
      - staging
      - production
Resources:
  DynamoDBTable:
    Type: "AWS::DynamoDB::Table"
    Properties:
      TableName: !Sub "${EnvironmentName}-dbtable"
      AttributeDefinitions:
        - AttributeName: "id"
          AttributeType: "S"
      KeySchema:
        - AttributeName: "id"
          KeyType: "HASH"
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 5

Outputs:
  DynamoDBTableName:
    Value: !Ref DynamoDBTable

Step 3:- Next define the required parameters in the params.json files as shown below -

[
  {
    "ParameterKey": "EnvironmentName",
    "ParameterValue": "dev"
  }
]
[
  {
    "ParameterKey": "EnvironmentName",
    "ParameterValue": "production"
  }
]
[
  {
    "ParameterKey": "EnvironmentName",
    "ParameterValue": "staging"
  }
]

Step 4:- save the files and login to the aws console and search for cloud formation service in the search bar.

Step 5:- once u click on cloud formation it will open cloud formation dashboard. click on create stack and upload the template.yaml file in stack

Step 6:- Click next and give a name for the stack and select the env.

Steo 7:- click next and click on submit to create a dynamo-db table using cloud formation stack.

Source-Code Link :- github.com/MahiraTechnology/Mahira-medium.git

Conclusion :-

creating an #Amazon DynamoDB table using a CloudFormation template provides a streamlined and repeatable approach for managing the lifecycle of your #database resources. #CloudFormation allows you to define the desired state of your DynamoDB table in a #declarative template, ensuring #consistency and reproducibility across environments.

By leveraging CloudFormation’s #infrastructure-as-code capabilities, you can define the table’s attributes, #primary key schema, throughput settings, and other advanced features such as global secondary indexes or DynamoDB Streams. This declarative approach simplifies the process of #provisioning and managing #DynamoDB tables, making it easier to #deploy and maintain your #database infrastructure.