Day 11 - AWS Cloud Formation Template

📍Introduction

Imagine you want to build a house from scratch. Instead of manually constructing every part of the house, you decide to use a blueprint, this blueprint will help you create the house by defining all the necessary components and their configurations.
Similarly in the world of cloud computing, you can use a Cloud formation template to act as a blueprint for you to build define and manage AWS infrastructure as code, and it's capable of handling intricate architectures with numerous resources and configurations. At its core, CloudFormation allows developers and DevOps engineers to define their cloud infrastructure using simple, declarative JSON or YAML templates.

🏗️ Building Blocks of a CloudFormation Template

A CloudFormation template is a blueprint that describes the AWS resources and their configurations in a specific cloud environment. These templates are made up of several building blocks:

  1. Resources: The fundamental components that constitute your cloud infrastructure (e.g., EC2 instances, S3 buckets, DynamoDB tables, etc.).

  2. Parameters: Input values that allow you to customize your template and make it more flexible. Users can specify these parameters during the stack creation process.

  3. Mappings: Predefined key-value pairs used to select different configurations based on regions or environments.

  4. Conditions: Logical statements that control whether certain resources are created or certain properties are assigned.

  5. Outputs: Values that can be accessed after the stack creation, enabling inter-stack communication or providing useful information to users.

  6. Metadata: Additional information about the template, such as its purpose or the author's contact information.

🚀 The Advantages of CloudFormation Templates

  1. Automation and Consistency: CloudFormation allows you to automate the entire process of creating and managing AWS resources. This ensures consistency across different environments, reducing the chances of errors and misconfigurations.

  2. Version Control: CloudFormation templates can be version-controlled using services like Git, providing a historical record of changes and promoting collaboration among team members.

  3. Scalability: As your application grows, so does the complexity of your infrastructure. CloudFormation templates can handle intricate architectures and support scalable deployments with ease.

  4. Infrastructure as Code (IaC): With CloudFormation, infrastructure becomes code, making it easier to review, test, and maintain. This aligns with modern software development practices and helps integrate infrastructure changes into the CI/CD pipeline.

  5. Quick Replication: Sharing templates allows different teams to replicate environments easily. This proves useful for development, testing, and staging purposes.

🗝️Key Feature - Drift Detection

The drift detection feature in CloudFormation (CFT) is a powerful mechanism that helps you maintain control and visibility over your AWS resources created and managed using CloudFormation templates. Drift detection allows you to detect any manual changes or discrepancies made to your stack resources outside of CloudFormation's management, ensuring that your infrastructure remains in the desired state as defined in the template.

💡AWS CLI vs AWS CFT

The AWS CLI is a command-line tool for interacting with AWS services directly, while AWS CloudFormation is a service that allows you to define and manage your AWS infrastructure as code using templates. The CLI is more suitable for quick, one-time commands or ad-hoc tasks, while CloudFormation is ideal for managing complex, scalable, and repeatable infrastructure deployments in a structured and automated manner.

👩‍💻CFT Hands-on

Documentation Reference - What is AWS CloudFormation? - AWS CloudFormation (amazon.com)

Task 1 - Create a stack to create an s3 bucket, manually delete it & verify drift detection

  • In the console, go to CloudFormation

  • Click on create stack

  • Click on Template is ready & Upload a template file

  • Write & Choose file with code

Resources:
  S3Bucket:
    Type: 'AWS::S3::Bucket'
    Properties: 
      BucketName : "day11-task-bucket"
  • Enter stack name as s3-bucket & scroll to submit

  • The s3 bucket is created

  • Delete the bucket

  • Go to drifts in the stack you created, you can see drift status as deleted

Task 2 - Create an ec2 instance

  • Write the yaml file with code to create instance & save it

      AWSTemplateFormatVersion: '2010-09-09'
      Description: 'Create an Amazon EC2 Instance'
    
      Resources:
        MyEC2Instance:
          Type: 'AWS::EC2::Instance'
          Properties:
            ImageId: 'ami-053b0d53c279acc90'    # Replace with your desired Amazon Machine Image (AMI) ID
            InstanceType: 't2.micro'
            SecurityGroups:
              - 'default'                       # Replace with the name of an existing security group
            KeyName: 'awskey'                   # Replace with the name of an existing EC2 Key Pair
    
  • Upload the file & enter the stack name

  • Verify the instance is created

Hope you found the post useful :)

Image source - Google

These posts are part of the #30daysofaws learning series by Abhishek Veeramalla

0
Subscribe to my newsletter

Read articles from Usha Mukkanagoudar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Usha Mukkanagoudar
Usha Mukkanagoudar

Hi there! My name is Usha, and I'm passionate about all things DevOps. I'm always looking for ways to expand my knowledge and share what I've learned with others. As the saying goes, "Learning is the only constant," and I'm excited to be on a never-ending journey of growth and discovery in the world of DevOps.