Home

AWS CloudFormation

By Sotee Loey on Feb 22, 2024
Image post 4

What is AWS CloudFormation?

CloudFormation is one of AWS service, which allows you to set up and manage AWS resources via a template. It allows you to create and manage your infrastructure as code. It is also easy to deploy or destroy any resources, by creating a stack or deleting it.

AWS CloudFormation Concepts:

Template: An CloudFormation template is a JSON or YAML file, which uses as blueprints for building AWS resources.

Stack: CloudFormation manage resources as a single unit called a Stack.

How I create an AWS Cloudformation stack

  1. Create CloudFormation template in VS code, eventtable.yml.
  2. Login into AWS console.
  3. Go to CloudFormation then choose create stack
    • Select Template is ready.
    • Select Upload a template file.
    • Upload eventtable.yml file, which is created in step 1.
    • Click next.
  4. Enter stack name then click next.
  5. Configure stack option then click next.
  6. Review or edit your stack.
  7. Select “I acknowledge that AWS CloudFormation might create IAM resources.” and Summit.
  8. Go to Events tab – This tab lists all the processes executed during the stack creation time.

CloudFormation template, eventtable.yml

Resources:
  EventTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: Event-Table
      AttributeDefinitions:
        - AttributeName: "event"
          AttributeType: "S"
      KeySchema:
        - AttributeName: "event"
          KeyType: "HASH"
      TimeToLiveSpecification:
        AttributeName: "ExpirationTime"
        Enabled: true
      ProvisionedThroughput:
        ReadCapacityUnits: "10"
        WriteCapacityUnits: "10"
    DependsOn:
      - DynamoDBQueryPolicy

  DynamoDBQueryPolicy:
    Type: "AWS::IAM::Policy"
    Properties:
      PolicyName: DynamoDBQueryPolicy
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Action: "dynamodb:Query"
            Resource: "*"
      Roles:
        - Ref: "EventQueryRole"

  EventQueryRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Principal:
              Service:
                - "dynamodb.amazonaws.com"
            Action:
              - "sts:AssumeRole"
      Path: "/"
Subscribe to my Newsletters

Stay up-to-date with new posts