Cloudformation with VPC, EC2 Instance and Auto-Scaling group
Revathi Joshi
Posted on October 10, 2022
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.
You can find all the projects related to EC2, in my GitHub Repository.
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 to prepare my CloudFormation Template.
Steps for implementation of this project:
- Build CloudFormation Template in Sections
- Save the Template as a .yaml on your computer
- Create the CloudFormation Stack — Upload your CloudFormation Template
- 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 (Syntax for the short form) returns the value of the specified parameter or resource.
Select function to select a single object from the list. In this case it is returning the first Availability Zone using the GetAZ function.
Sub function to update the AWS::Region pseudo parameter for the actual region the stack is created in. It uses the Base64 function 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
Name the CF stack - CF-stack
Accept all the default parameters.
Click Next/Next
Create Stack
Click Events Tab to see the Stack Creation in Progress
Click Resources Tab to see the Stack Creation Completed.
Click Stack Info Tab to see the Stack Creation Completed.
- On the EC2 Dashboard, 3 EC2 Instances are provisioned
- On the EC2 dashboard/Auto scaling/Auto scaling Groups
Group Details
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
- Since a KeyPair was not created, connect to the EC2 instance using EC2 Instance Connect.
Click Next
- After connecting to EC2 Instance, start the stress test by running the following command:
sudo stress --cpu 1 --timeout 60
- Check to see the one of the EC2 Instance is Terminated
- Click one of the instances, see the effect of stress test
- 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
- put on the browser to see Apache Test Page
Cleanup:
- On the CloudFormation Dashboard
- Check Stack/Delete
- Delete Stack
- 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.
Posted on October 10, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024