Featured image of post AWS CloudFormation in a Nutshell

AWS CloudFormation in a Nutshell

AWS CloudFormation in a Nutshell

A Brief History of AWS CloudFormation

Back in 2011, AWS decided to make life easier by introducing CloudFormation, a tool that allows engineers to define AWS infrastructure in JSON (or YAML, if you have taste).

Before this, people had to:

  1. Manually create resources through the AWS Console 😡
  2. Write long, fragile bash scripts to automate deployments πŸ”§
  3. Cry when their cloud environment wasn’t repeatable 😭

AWS CloudFormation vs. Other IaC Tools

FeatureCloudFormationTerraformPulumiAnsible
AWS-Nativeβœ… Yes❌ No❌ No❌ No
Multi-Cloud Support❌ Noβœ… Yesβœ… Yesβœ… Yes
LanguageYAML/JSONHCLPython/JS/GoYAML
State ManagementAWS ManagedSelf-managedSelf-managedNo explicit state
Best ForAWS-only infraMulti-cloud infraDevs who prefer real codeConfig management

If you’re all-in on AWS, CloudFormation is a solid choice.

If you need multi-cloud support, Terraform is your best bet.

Pulumi is for devs who hate YAML, and Ansible is for managing software/configurations rather than provisioning infra.


CloudFormation Code Examples

1. Create an S3 Bucket

1
2
3
4
5
6
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyS3Bucket:
    Type: "AWS::S3::Bucket"
    Properties:
      BucketName: "my-cloudformation-bucket"

2. Provision an EC2 Instance

1
2
3
4
5
6
Resources:
  MyEC2Instance:
    Type: "AWS::EC2::Instance"
    Properties:
      InstanceType: "t2.micro"
      ImageId: "ami-12345678"

3. Set Up a VPC

1
2
3
4
5
Resources:
  MyVPC:
    Type: "AWS::EC2::VPC"
    Properties:
      CidrBlock: "10.0.0.0/16"

4. Deploy a Load Balancer

1
2
3
4
5
6
Resources:
  MyLoadBalancer:
    Type: "AWS::ElasticLoadBalancingV2::LoadBalancer"
    Properties:
      Name: "my-load-balancer"
      Type: "application"

5. Spin Up an RDS Database

1
2
3
4
5
6
7
Resources:
  MyRDS:
    Type: "AWS::RDS::DBInstance"
    Properties:
      Engine: "mysql"
      DBInstanceClass: "db.t3.micro"
      AllocatedStorage: 20

6. Create an IAM Role

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Resources:
  MyIAMRole:
    Type: "AWS::IAM::Role"
    Properties:
      RoleName: "MyCloudFormationRole"
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Principal:
              Service: "ec2.amazonaws.com"
            Action: "sts:AssumeRole"

7. Set Up an Auto Scaling Group

1
2
3
4
5
6
Resources:
  MyAutoScalingGroup:
    Type: "AWS::AutoScaling::AutoScalingGroup"
    Properties:
      MinSize: "1"
      MaxSize: "5"

8. Deploy a Lambda Function

1
2
3
4
5
6
7
8
Resources:
  MyLambda:
    Type: "AWS::Lambda::Function"
    Properties:
      Runtime: "python3.8"
      Handler: "index.lambda_handler"
      Code:
        S3Bucket: "my-lambda-bucket"

9. Create an SNS Topic

1
2
3
4
5
Resources:
  MySNSTopic:
    Type: "AWS::SNS::Topic"
    Properties:
      DisplayName: "My SNS Topic"

10. Define a CloudWatch Alarm

1
2
3
4
5
6
7
Resources:
  MyCloudWatchAlarm:
    Type: "AWS::CloudWatch::Alarm"
    Properties:
      AlarmDescription: "High CPU usage"
      ComparisonOperator: "GreaterThanThreshold"
      Threshold: 80

Key Ideas Table

ConceptExplanation
CloudFormationAWS-native IaC tool (YAML/JSON)
State ManagementAWS manages state internally
Best Use CaseDeploying AWS-only infrastructure
ComparisonCompetes with Terraform, Pulumi, and Ansible
Example UsesProvisioning VMs, databases, networks, and IAM roles