Cloudformation with VPC, EC2 Instance and Auto-Scaling group
Published: 2022-10-10 22:18:38

In this article, I am going build an EC2 infrastructure with a Custom VPC and its components with auto-scaling group using Cloudformation template.

As per Amazon, "**AWS CloudFormation** is a service that gives developers and businesses an easy way to create a collection of related AWS and third-party resources, and provision and manage them in an orderly and predictable fashion so that you can spend more time focusing on your applications."

It uses a **template which is a JSON - or YAML-formatted text file**. It describes all the AWS resources that you want and Cloud Formation takes care of provisioning and configuring those resources for you. 

A **stack** is a collection of AWS resources that you can manage as a single unit. All the resources in a stack are defined by the stack’s AWS CloudFormation template. A stack, for instance, can include all the resources required to run a web application, such as a web server, a database, and networking rules.


Link to my complete code in [GitHub Repository](https://github.com/awsmine/CloudFormation_templates/blob/main/CloudFormation_VPCEC2ASG.yaml).

You can find all the projects related to EC2, in my [GitHub Repository](https://github.com/awsmine/EC2_projects).

_**Let's get started!**_

## Objectives:

- A Custom VPC with a CIDR of 10.0.0.0/16
- 2 Public and 2 Private subnets
- An Internet Gateway (IGW) to allow internet access to the EC2
  instances
- Auto-scaling group configured with a minimum of 2 instances, 
  upto a maximum of 5
- EC2 t2.micro Instances with a stress tool.

## Pre-requisites:
- An AWS Account (not root account)
- Visual Studio Code Editor or Cloud9 IDE

## Resources Used:
I have used extensively [CloudFormation UserGuide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) to prepare my CloudFormation Template.

## Steps for implementation of this project:

1. Build CloudFormation Template in Sections
2. Save the Template as a .yaml on your computer
3. Create the CloudFormation Stack — Upload your CloudFormation
   Template
4. Stress Test EC2 Instances


###1. Build CloudFormation Template in Sections

I have included the following items in the CloudFormation Stack. Although a template made up of ten sections, the **Resources section** is the only one required. For this project we will be using 
- AWSTemplateFormatVersion
- Descripton
- Parameters
- Resources

Use **AWS CloudFormation built-in intrinsic functions** in your Template to assign values to properties that are not available until runtime.

[!Ref function](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html) (Syntax for the short form) returns the value of the specified parameter or resource.

[Select function](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-select.html) to select a single object from the list. In this case it is returning the first Availability Zone using the [GetAZ function](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getavailabilityzones.html).

[Sub function](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html) to update the AWS::Region pseudo parameter for the actual region the stack is created in. It uses the [Base64 function](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-base64.html) to pass encoded data to EC2 instances.

Substitutes variables in an input string with values that you specify. In your templates, you can use this function to construct commands or outputs that include values that aren't available until you create or update a stack.

###2. Save the Template as a .yaml on your computer

Save the file **_CloudFormation_VPCEC2ASG.yaml_** on your computer.

###3. Create the CloudFormation Stack — Upload your CloudFormation Template

A **stack** is a collection of AWS resources that you can manage as a single unit. All the resources in a stack are defined by the stack’s AWS CloudFormation template. A stack, for instance, can include all the resources required to run a web application, such as a web server, a database, and networking rules.

Go to the AWS Console/CloudFormation dashboard/Click Create Stack (with new resources(standard)/Template is ready/Upload a template file. Upload the **CloudFormation_VPCEC2ASG.yaml** file that you created in the previous step.

Click _**Next**_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gew20co0k8rxxmvwp5y.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zplstcj92v2cz0cvfmz4.png)



Name the CF stack - **_CF-stack_**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6cle14iyqu0glfmll5ts.png)





Accept all the default parameters.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1wj35dsvo9wvp7zzazi.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xl2ctgmpcyfbjo766o4k.png)

Click _**Next**_/_**Next**_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hca31tv6jq3pzsux2v1b.png)

Create Stack

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dwvsjdse09uc7zkde2wy.png)




Click **_Events_** Tab to see the **_Stack Creation in Progress_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gmeldbvoc1rawmvkbo3u.png)



Click Resources Tab to see the Stack Creation Completed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5wa89hww1puf7qm8b6i7.png)


Click Stack Info Tab to see the Stack Creation Completed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mw2a7u85xa6jf36jmllg.png)


- On the EC2 Dashboard, 3 EC2 Instances are provisioned

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amvdfqiink63r5l0beku.png)

- On the EC2 dashboard/Auto scaling/Auto scaling Groups


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2ztfgwv0j8xvbtllkkj.png)

Group Details


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8xygab5oas8yuetc1rn7.png)


###4. Stress Test EC2 Instances

- Navigate to the EC2 dashboard/Instances

- Check the box next to one of the newly created instances, then click Connect


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ai23sgv7kri90ftdemp0.png)

- Since a KeyPair was not created, connect to the EC2 instance using EC2 Instance Connect.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrzlfraekm59li4a6ko4.png)

Click Next

- After connecting to EC2 Instance, start the stress test by running the following command:



**`sudo stress --cpu 1 --timeout 60`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lyj1ckeryjleoix5q8mu.png)




- Check to see the one of the EC2 Instance is Terminated



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6vlm3q6abrl2zs3pzvs.png)

- Click one of the instances, see the effect of stress test

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8eys49qfnf8d3zdt6gvx.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usbumkksojed4i9et4t2.png)



- Repeat with the other EC2 instance. 
- After the cool down period is over your Auto Scaling policy will kick in and additional instances will be launched.

- Copy one of the EC2 instances Public IP address 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57y4nxe8n2m15ntxitqd.png)

- put on the browser to see Apache Test Page

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w78dt60kvk6c02vuq09g.png)

## Cleanup:

- On the CloudFormation Dashboard 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkja441xmnlynbjeev6c.png)

- Check **_Stack/Delete_**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3kf8uqmrfuf43ug3dw8.png)

- Delete Stack

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vz4djpl5d48lf5hd57t.png)

- you can verify the clean-up from the EC2 dashboard.

## What we have done so far:

Created an EC2 instance with a Custom VPC and its components with auto-scaling group using Cloudformation template.