Getting Started with AWS CloudFormation Templates (CFT)

ยท

6 min read

Getting Started with AWS CloudFormation Templates (CFT)

Introduction

In the ever-evolving world of cloud computing, provisioning and managing resources efficiently is paramount. AWS CloudFormation Templates (CFT) emerge as a powerful solution to automate and standardize the deployment of AWS resources. This article aims to provide a comprehensive guide to beginners looking to explore the world of AWS CFTs.

What is AWS CFT?

AWS CloudFormation Templates (CFT) are JSON or YAML scripts that define the infrastructure and resources required for your application in an AWS environment. These templates serve as the blueprint for creating and managing a stack, which is a collection of AWS resources that can be provisioned, updated, and deleted together.

AWS CLI vs. AWS CFT. When to use CFT?

AWS cli is mostly used to to achieve quick action like someone says Hey give me a list of S3 buckets so, at that time you simply use cli. The drawback of AWS CLI is it does not implement the IAC principle.

CFT is used to create the infrastructure in the AWS environment and it implements the IAC principle.

If you want to create large resources in AWS then at that instance you use CFT cause it supports declarative language to get the desired state.

Principle of the IAC?

  1. Version Control: Treat Infrastructure as Code just like you would treat application code. Store your infrastructure code in version control systems (such as Git) to track changes, collaborate with others, and roll back to previous configurations if needed.

  2. Declarative Language: IaC is often implemented using declarative languages like YAML or JSON. Instead of describing the sequence of steps, you define the desired state of the infrastructure. This makes it easier to understand and predict the final state of the environment.

DRIFT DETECTION

Certainly! AWS CloudFormation Drift Detection is like a safety net that helps you find out if someone made changes to your cloud resources outside of the CloudFormation templates you originally set up.

Example:

  1. You create an EC2 instance using a CloudFormation template. The template specifies that the instance should have a specific instance type and security group.

  2. Later on, someone changes the instance type directly in the AWS Management Console to something different.

  3. When you run drift detection, CloudFormation checks the current state of the EC2 instance against what the template says it should be. It detects that the instance type is not what the template specifies. This is like the LEGO inspector telling you that someone changed your LEGO structure.

  4. CloudFormation will then show you the differences it found, so you can decide whether to bring the instance back in line with your template or update the template to match the current state.

In simple terms, AWS CloudFormation Drift Detection helps you find out if your resources in the cloud have changed without you knowing, so you can keep everything in line with your original plan (template).

CFT Structure

An AWS CloudFormation template (CFT) is a text file written in JSON or YAML format that describes the infrastructure and resources you want to create in your AWS environment. It's like a blueprint for your cloud resources. Let's break down the basic structure of a CloudFormation template:

  1. Template Version and Format: At the beginning of the template, you specify the format version and other metadata.

     AWSTemplateFormatVersion: '2010-09-09'
     Description: My CloudFormation Template
    
  2. Resources: The core of the CloudFormation template is the Resources section. This is where you define the AWS resources you want to create, such as EC2 instances, S3 buckets, RDS databases, and more.

     Resources:
       MyEC2Instance:
         Type: AWS::EC2::Instance
         Properties:
           InstanceType: t2.micro
           ImageId: ami-12345678
    
  3. Parameters: Parameters allow you to customize your template when you launch a stack. They act like inputs that users provide when they deploy the CloudFormation stack.

     Parameters:
       InstanceTypeParameter:
         Type: String
         Default: t2.micro
    
  4. Mappings: Mappings are essentially lookup tables that allow you to define key-value pairs and use them in your template. They can help you choose specific values based on different conditions, regions, or environments. F

     Mappings:
       RegionToAMI:
         us-east-1:
           AMI: ami-12345678
         us-west-2:
           AMI: ami-87654321
    
  5. Conditions: Conditions allow you to control the creation of resources based on certain conditions.

     Conditions:
       CreateProdResources: !Equals [ !Ref EnvType, prod ]
    
  6. Outputs: The Outputs section allows you to define values that are returned when the stack is created or updated.

     Outputs:
       MyEC2InstanceIP:
         Description: Public IP address of the EC2 instance
         Value: !GetAtt MyEC2Instance.PublicIp
    

Creating the AWS Ec2 using AWS CFT

Here is the yaml file that creates the Ec2 resource in the aws using cft.

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      ImageId: ami-0c55b159cbfafe1f0  # Replace with your desired Amazon Machine Image (AMI) ID
      KeyName: my-key-pair  # Replace with the name of your EC2 key pair
      SecurityGroups:
        - MySecurityGroup  # Replace with the name of your security group

In this template:

  • AWSTemplateFormatVersion specifies the CloudFormation template version.

  • Resources section describes the resources you want to create. In this case, we're creating an EC2 instance with the logical ID MyEC2Instance.

  • Type specifies the resource type, which is AWS::EC2::Instance for an EC2 instance.

  • Properties define the properties of the EC2 instance. You can set the instance type, image ID, key name, and security groups as shown in the example.

Creating the S3 bucket using CFT

Here is a basic AWS CloudFormation template in YAML to create an S3 bucket:

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-unique-bucket-name

In this template:

  • AWSTemplateFormatVersion specifies the template version.

  • Resources is the section where you define the resources you want to create.

  • MyS3Bucket is the logical name for the S3 bucket resource.

  • Type specifies the resource type, which is AWS::S3::Bucket for an S3 bucket.

  • Properties contain the specific properties for the S3 bucket resource. In this case, we're setting the BucketName property, which should be a unique name for your bucket.

Remember that S3 bucket names need to be globally unique across all of AWS, so make sure to replace my-unique-bucket-name with a unique name that hasn't been taken by someone else.

Learn more

Aws CFT documentation is well managed so, you can refer to that Documentation in order to learn more about cloud formation templates. Please follow below link for Reference.

AWS CFT DOCUMENTATION

CONCLUSION

AWS CloudFormation Templates offer a powerful way to manage your cloud infrastructure. With the ability to define your infrastructure as code, you gain greater control, automation, and repeatability. Whether you're deploying a small application or building a robust architecture, CloudFormation can be your trusted companion in the cloud journey.

Thank you so much for giving your valuable time to read my blog. If you like it please like, share and comment.

ย