AWS CloudFormation

AWS CloudFormation is a service provided by Amazon Web Services (AWS) that enables developers and system administrators to create, manage, and provision AWS resources using Infrastructure as Code (IaC). It allows you to define your infrastructure in JSON or YAML templates, which are then used to automatically provision, configure, and update AWS services and resources.

Key Features
  1. Templates as CodeInfrastructure is defined using JSON or YAML templates, making it version-controllable and replicable across different environments.

  2. Stack ManagementA "stack" is a collection of AWS resources that you manage as a single unit. CloudFormation automates the process of creating, updating, and deleting these stacks.

  3. Drift DetectionCloudFormation can detect if the actual configuration of AWS resources in a stack has deviated from the configuration defined in the template (called "drift").

  4. Resource DependenciesCloudFormation automatically handles dependencies between resources. For example, if a database instance needs to be created before an application server, CloudFormation ensures the correct order.

  5. Update and RollbackStacks can be updated in a controlled manner, and if something goes wrong, CloudFormation supports rolling back to a previous known good state.

  6. Cross-Stack ReferencesYou can share resources across different stacks, which improves modularity and reusability.

  7. AWS Service SupportCloudFormation supports a wide range of AWS services, including EC2, S3, RDS, Lambda, and more.

Basic Concepts
  • Template: The core of CloudFormation, a JSON or YAML file that describes your resources and their configurations.
  • Stack: A collection of resources defined in a CloudFormation template. When you create a stack, CloudFormation provisions and configures the resources.
  • Change Set: A preview of the changes that CloudFormation will make when you update a stack. It allows you to review potential modifications before applying them.
Template (YAML)
AWSTemplateFormatVersion: '2010-09-09'
Resources: MyEC2Instance: Type: 'AWS::EC2::Instance' Properties: InstanceType: t2.micro ImageId: ami-0abcdef1234567890

In this example, a basic EC2 instance is created using the specified instance type and image ID.

Use Cases
  • Automating Infrastructure: Create, update, and manage infrastructure as code in a repeatable way.
  • Environment Consistency: Deploy the same infrastructure across multiple environments (e.g., development, staging, production).
  • Resource Management: Easily manage and track changes to infrastructure over time.


Example: Create a simple CloudFormation template (a YAML file) that provisions an AWS S3 bucket and an EC2 instance and then use AWS CLI to deploy this stack

Step1: Create a CloudFormation template MyInfraSetupTemplate.yaml

AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template to import an EC2 instance with existing VPC and Subnet.
Resources:
  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      InstanceType: t2.micro
      KeyName: keypair  # Ensure that this key pair exists
      SecurityGroupIds:
        - sg-073d0796e4533ade8  # <-- Replace with your existing Security Group ID
      SubnetId: subnet-01647f388348b7bbc  # <-- Replace with your existing Subnet ID
      ImageId: ami-0522ab6e1ddcc7055  # <-- Replace with the correct AMI ID for your region
    DeletionPolicy: Retain
    
  MyS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: siraj-test-bucket

Step2: Validate the CloudFormation template

aws cloudformation validate-template --template-body file://MyInfraSetupTemplate.yaml

If the template is valid, you’ll see a confirmation message. Otherwise, it will point out issues.

Step3: Create a CloudFormation stack

aws cloudformation create-stack \
    --stack-name MyStack \
    --template-body file://MyInfraSetupTemplate.yaml \
    --capabilities CAPABILITY_IAM

The --capabilities CAPABILITY_IAM flag is necessary if the template involves IAM resources, although it's not needed in this S3 bucket example. 

Step4: You can check CloudFormation stack creation progress

aws cloudformation describe-stack-events --stack-name MyStack


Step5: You can update CloudFormation stack (if needed)

aws cloudformation update-stack \
    --stack-name MyStack \
    --template-body file://MyInfraSetupTemplate.yaml

Step6: You can see the created resources a S3 bucket and an EC2 instance running.




Step7: Delete CloudFormation stack and all its resources

aws cloudformation delete-stack --stack-name MyStack