AWS CloudFormation: Simplifying Infrastructure as Code

First of all, we need to understand what is Infrastructure As Code(IAC).

"It is a DevOps practice that involves automating the process of deploying and managing IT infrastructure through version-controlled configuration files."

In other words, IAC is a method of managing infrastructure resources by defining them in code i.e. usually in YAML or JSON format. This code can be used to automate the deployment of infrastructure resources in a repeatable and consistent way. This approach ensures that the entire infrastructure is consistent across environments, reducing the risk of errors and increasing efficiency. IAC can be implemented using tools such as AWS CloudFormation, Terraform, Ansible, Chef, and Puppet, among others.

What is CloudFormation?

AWS CloudFormation is an infrastructure as code (IAC) service provided by Amazon Web Services (AWS) that allows users to create, manage and deploy a collection of AWS resources in a repeatable and automated fashion. CloudFormation uses JSON or YAML templates to define AWS resources such as EC2 instances, RDS instances, VPCs, security groups, and more.

Use Cases of AWS CloudFormation:

  1. Infrastructure as Code: AWS CloudFormation provides a way to define infrastructure resources in code, enabling you to version control, test, and reuse templates. This helps automate the process of provisioning, updating, and deleting resources and ensures that the entire infrastructure is consistent across environments.

  2. Multi-Environment Management: AWS CloudFormation enables the management of multiple environments such as development, staging, and production, by defining and deploying infrastructure resources in a consistent way. It reduces the risk of errors and increases efficiency as the entire infrastructure is identical across all environments.

  3. Disaster Recovery: With AWS CloudFormation, users can easily create a backup infrastructure environment in another region to ensure business continuity in the event of a disaster. This ensures that businesses can quickly restore services and resume operations in the case of an emergency.

  4. Application Stacks: AWS CloudFormation can be used to deploy entire application stacks, making it easier to manage the lifecycle of an application. This includes defining and deploying resources such as web servers, application servers, databases, and load balancers.

Advantages of AWS CloudFormation:

  1. Automation: AWS CloudFormation automates the process of provisioning, updating, and deleting AWS resources, making it easier to manage infrastructure resources at scale.

  2. Consistency: With AWS CloudFormation, you can ensure that the entire infrastructure is consistent across environments, reducing the risk of errors and increasing efficiency.

  3. Version Control: CloudFormation templates can be version controlled, enabling you to track changes to your infrastructure resources over time.

  4. Cost-Effective: AWS CloudFormation can help reduce costs by automating the process of provisioning and de-provisioning resources.

  5. Service Integration: CloudFormation enables service integration by allowing a single template to manage the deployment of individual resources or multiple resources. With this capability, you can easily integrate various AWS cloud services by writing a template that sets up an EC2 virtual machine within an AWS Virtual Private Cloud (VPC), or deploys an S3 storage bucket while configuring access control using the IAM service. This approach simplifies the process of integrating multiple services as you build out a complete cloud environment.

  6. Easy updates: In addition to deploying new resources, you can apply changes to existing resources with CloudFormation templates. This ability simplifies the process of, for example, adding more storage to a fleet of ec2 instances or changing access control rules.

Now The Hands-on Part:

In this example, we will simply create a simple S3 Bucket using Cloud Formation which will give a brief overview and clear idea how it works.

STEP-1: Create a new stack in CloudFormation

STEP-2: Now we have to create a template from which our stack i.e. resources will be made using CloudFormation.

  1. Template is ready - if u have template ready in your local system or you can import from existing s3 bucker (it is also next field to choose)

  2. Use a sample template - their are existing template made by AWS for each service that can be used instead of creating one from scratch.

  3. Create template in Designer - AWS also provide GUI in which you can drag and drop connect elements and resources and create template.

Now choose the template file in which you have written for your stack. (in this case we are creating on s3 bucket)

TEMPLATE FILE:

AWSTemplateFormatVersion: "2010-09-09"
Description: Simple cloud formation for bucket creation and configuration

Parameters: # it can be refered just like variables and acan be refered afterwards
  BucketName: { Type: String, Default: "myusukhbuckett" }

Resources:
  MainBucket: #it is a logical name we give to a paticualar resource
    Type: "AWS::S3::Bucket" # type of resource 
    Properties: # properties of that resource you want to create
      BucketName: !Ref BucketName # refering to parameters for name
      BucketEncryption: # encryption used in s3 bucket
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: AES256

STEP-3: Now name your Stack.

STEP-4: Now, we have stack configuration options. As per now I am not touching anything but they can can be and are useful in advance usecases.

STEP-5: Review your stack and its configurations and do next.

STEP-6: Our stack and components are Ready !!

Cross checking the created resources

Yes, the Bucket is created successfully.

Browse More Templates on: https://aws.amazon.com/cloudformation/resources/templates/

Hope you liked the blog...

More coming soon...

Thankyou!

Did you find this article valuable?

Support Sukhpreet Singh by becoming a sponsor. Any amount is appreciated!