Simplify your Infrastructure Management using AWS CloudFormation
Tags: aws, cloud, tutorial, devops
Published: 2023-10-21 13:55:07

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/spc46nengjsjmhpssllc.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4nzzdwz0evnam6r7r9jc.png)

### 2. Create a CloudFormation Template file

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

```yaml
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
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxdft0hfjxoljwawcp15.png)

**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](https://docs.aws.amazon.com/AWSCloudFormation)

### 3. Upload Template to CloudFormation

![Template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2uppk4mgijo0sp2ixd22.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aminc4lerpgjh5uvalo5.png)

Like here I've given my name as **NiteshStack**

### 5. Configure Stack

Configure the stack according to your need

![ConfigureStack](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8l6mks2c1wf7x1tg3q29.png)

### 6. Review your stack and click submit

After you review your stack configuration click the **submit** button 

![Submit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9o8tunoz8vcldo7zqnv.png)

``Now you can see the resource creation process is initiated``

![ResourceCreation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdrf8m6l5j8sn0gjelw6.png)

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

![Create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ojd5h6euu43mrqj018s.png)

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

![ec2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4z2vkk6s490o70btz62s.png)

``If you want to delete your stack you can delete it``

![delete](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ob996pknquo4tgo2l67x.png)

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

![Ec2 delete](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lld5mpiz1srevjhj60je.png)

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!!