AWS CloudFormation and its Key Components

AWS CloudFormation and its Key Components

AWS Series (Part-8)

Introduction

AWS CloudFormation is a service that enables you to specify and set up AWS infrastructure as code (IaC). You can use CloudFormation to create a template that describes your infrastructure, saving you the trouble of manually creating and configuring resources in the AWS Management Console. This template is a text file in JSON or YAML format that lists the properties and resources required for your infrastructure.

You can use a template to model and provision all the resources required for your applications across accounts and regions in an automated and secure manner. An arrangement of resources known as a stack can be created, updated, and deleted using CloudFormation.

Key Components of AWS CloudFormation

Some key components of AWS CloudFormation are as follows:

  1. Templates: A template is a text file in the JSON or YAML format that lists the properties and AWS resources required to set up and manage a stack. You can model and manage AWS resources as code by using templates, which act as the blueprint for your infrastructure. By allowing for version control, sharing, and reuse, these files offer a reliable and standardized method for updating and deploying your infrastructure.

    For example, the following YAML template can be used to create an EC2 Instance with minimum configuration

     AWSTemplateFormatVersion: "2010-09-09"
     Description: This is a sample CloudFormation Template to create an EC2 Instance
     Resources:
       MyEC2Instance:
         Type: AWS::EC2::Instance
         Properties:
           AvailabilityZone: us-east-1a
           InstanceType: t2.micro
           ImageId: ami-0fc5d935ebf8bc
    
  2. Stacks: An AWS resource collection that you can manage as a single entity is called a "stack". A stack is created based on a template that is deployed using CloudFormation. Stacks facilitate the creation, updating, and deletion of entire infrastructure sets by grouping related resources together for easier management.

  3. Change Sets: With Change Sets, you can see a preview of the changes made to your stack before deciding to apply them. A change set helps you comprehend the effects of the changes before they are applied by providing an overview of the modifications that will be made to your stack resources.

    Below is an example template to allocate an Elastic IP to the previously created EC2 Instance

     Resources:
       MyEC2Instance:
         Type: AWS::EC2::Instance
         Properties:
           AvailabilityZone: us-east-1a
           InstanceType: t2.micro
           ImageId: ami-0fc5d935ebf8bc3bc
       MyEIP:
         Type: AWS::EC2::EIP
         Properties:
           InstanceId: !Ref MyEC2Instance
    

  4. Mappings: AWS CloudFormation mappings allow you to build conditional resource configurations on the basis of key-value pairs. With their help, you can specify a range of values that can be utilized to retrieve particular configuration values under particular circumstances. When you want to modify resources according to various parameters, like regions, environments, or AMI types, mappings are frequently utilized.

     Mappings:
       RegionMap:
         us-east-1:
           AMI: ami-0230bd6060c6
         us-west-2:
           AMI: ami-06e4ca0535e9
    
     Resources:
       MyEC2Instance:
         Type: AWS::EC2::Instance
         Properties:
           InstanceType: t2.micro
           ImageId: !FindInMap [RegionMap, !Ref 'AWS::Region', AMI]
    
  5. Parameters: When creating or updating a stack in AWS CloudFormation, you can enter custom values using the parameters. They give you the ability to alter the behavior of your CloudFormation templates, increasing their adaptability and reusability in various environments. Values such as instance types, AMI IDs, key pair names, and more can be specified using parameters.

     Parameters:
       InstanceTypeParameter:
         Type: String
         Default: t2.micro
         AllowedValues:
           - t2.micro
           - t3.micro
           - t2.small
         Description: Enter the EC2 instance type
    
     Resources:
       MyEC2Instance:
         Type: AWS::EC2::Instance
         Properties:
           InstanceType: !Ref InstanceTypeParameter
           ImageId: ami-0230bd608260c
    

Overall, you can automate the infrastructure provisioning process and ensure consistency across various environments by utilizing AWS CloudFormation. It also facilitates the management and upgrade of your infrastructure in the long run.