Setup Cloud Formation in a few Easy Steps

Setup Cloud Formation in a few Easy Steps

A simple example of an AWS CloudFormation template written in YAML. This template creates an Amazon S3 bucket:

This CloudFormation template consists of the following sections:

  • AWSTemplateFormatVersion: Specifies the CloudFormation template version.

  • Description: A brief description of the CloudFormation template.

  • Resources: The main section where you define the AWS resources you want to create.

    • myBucket: A logical name for the S3 bucket resource.

      • Type: Specifies the AWS resource type (in this case, 'AWS::S3::Bucket').

Properties: Configures the specific properties of the resource, such as the bucket name.

AWSTemplateFormatVersion: '2023-12-15'
Description: 'A simple example of an AWS CloudFormation template written in YAML. This template creates an Amazon S3 bucket.'

Resources:
  myBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: 's3-Bucket-for-CloudFormation'

Below is a simple AWS CLI command to create a stack:

aws cloudformation create-stack --stack-name myBucket --template-body file://yamlfilepath.yml

Want to Adding Stuff to your AWS Cloudformation Stack:

EC2 Instance with Key Pair and Security Group:

Resources:
  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: 'ami-12345678'
      InstanceType: 't2.micro'
      KeyName: 'my-key-pair'
      SecurityGroupIds:
        - 'sg-12345678'

Amazon RDS MySQL Instance:

Resources:
  MyDBInstance:
    Type: 'AWS::RDS::DBInstance'
    Properties:
      AllocatedStorage: '20'
      DBInstanceIdentifier: 'my-db-instance'
      Engine: 'mysql'
      MasterUsername: 'admin'
      MasterUserPassword: 'password'
      DBInstanceClass: 'db.t2.micro'

AWS Lambda Function:

Resources:
  MyLambdaFunction:
    Type: 'AWS::Lambda::Function'
    Properties:
      Handler: 'index.handler'
      Role: 'arn:aws:iam::123456789012:role/lambda-execution-role'
      FunctionName: 'my-lambda-function'
      Runtime: 'nodejs14.x'
      Code:
        S3Bucket: 'my-lambda-code-bucket'
        S3Key: 'lambda-code.zip'

use Amazon DynamoDB instead of RDS Instance

Resources:
  MyDynamoDBTable:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: 'my-dynamodb-table'
      AttributeDefinitions:
        - AttributeName: 'ID'
          AttributeType: 'N'
      KeySchema:
        - AttributeName: 'ID'
          KeyType: 'HASH'
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 5

Note: In this tutorial, I provided code for both connections SQL or NoSql

Did you find this article valuable?

Support Glitch of Code by becoming a sponsor. Any amount is appreciated!