DEV Community

Cover image for Simplify your Infrastructure Management using AWS CloudFormation
Nitesh Thapliyal for AWS Community Builders

Posted on

Simplify your Infrastructure Management using AWS CloudFormation

In this blog, we'll explore what AWS CloudFormation is, why it's essential, and dive into a practical demonstration to showcase its capabilities.

From understanding the basics to creating your first CloudFormation stack, you'll gain valuable insights and hands-on experience, enabling you to automate your AWS infrastructure management effectively.

Let's explore the AWS CloudFormation and discover how to harness the power of Infrastructure as Code for your projects.

CloudFormationLogo

What is AWS CloudFormation?

It is an AWS service that allows you to define and provision AWS infrastructure as code using a template.

The template can be created either using JSON or YAML

When do we use AWS CloudFormation?

When you want to manage and provision your AWS infrastructure in a consistent and repeatable manner.

It allows you to automate the use of AWS services by defining infrastructure as a code.

This approach is particularly valuable in a DevOps environment where you need to create a large infrastructure and you prefer automation over manual things.

You can use AWS CloudFormation in the following scenarios:

  • Infrastructure as Code
    When you want to manage your infrastructure in AWS in a repeatable manner

  • Templated Deployment
    When you want to create, update, or delete a stack of AWS resources by defining a CloudFormation template

  • Complex Deployment
    For managing multiple AWS resources such as EC2 instances, RDS databases, S3 buckets, and more. CloudFormation helps ensure that all resources are created, configured, and connected correctly.

  • Stack updates
    CloudFormation handles updates by creating a change set, which helps prevent disruptions to your existing resources.

Let's understand practically how we can utilize CloudFormation...

For the demo purpose, I'm going to launch two EC2 instances by creating the CloudFormation Template.

Follow the steps for better understanding:

1. Open CloudFormation service in AWS

CFLandingPage

2. Create a CloudFormation Template file

For creating the template I'm using YAML you can use JSON also

Resources:
  NiteshOsUsingCF:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-099b3d23e336c2e83
      InstanceType: t2.micro
      AvailabilityZone: ap-south-1a

  NiteshOs2:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-099b3d23e336c2e83
      InstanceType: t2.micro
      AvailabilityZone: ap-south-1a
Enter fullscreen mode Exit fullscreen mode

here you can see I'm defining that I want to launch 2 EC2 instances with instance type t2.micro and availability zone as ap-south-1a

You can get the ImageId from the AWS AMI catalog, select the image of your choice, and copy its AMI ID

ami

Note - I'm defining EC2 instances for demo purposes you can define any AWS services like S3, EBS, etc... and their syntax you can check from the docs

To know more about how to create template and syntax you can check the aws doc here

3. Upload Template to CloudFormation

Template

Make sure your YAML or JSON formatting is correct and the file name is with extension .yml or .json

4. Give a name to your stack

StackName

Like here I've given my name as NiteshStack

5. Configure Stack

Configure the stack according to your need

ConfigureStack

6. Review your stack and click submit

After you review your stack configuration click the submit button

Submit

Now you can see the resource creation process is initiated

ResourceCreation

After a few minutes, you can see the resources are created

Create

Here we can see that new EC2 instances at ap-south-1a are created

ec2

If you want to delete your stack you can delete it

delete

Once you delete the stack your resources will be deleted and in our case, the EC2 instances will be deleted

Ec2 delete

That's how you can utilize AWS CloudFormation service to simplify the Infrastructure Management

Hope you find this blog insightful, Do give it a like 🌟

Thank you!!

Top comments (0)