Day 40 - Amazon EC2 Automation

Day 40 - Amazon EC2 Automation

We have seen detailed about Amazon EC2 and how to launch an EC2 instance in the blog, Please refer -> AWS : Amazon EC2 .

In this blog, we will do more automation with EC2.

So, Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers.

Launch Templates in EC2

A launch template is a feature provided by Amazon EC2 that allows you to create reusable configurations for launching instances.

You can create a launch template that contains the configuration information to launch an instance. You can use launch templates to store launch parameters so that you do not have to specify them every time you launch an instance.

Launch templates support versioning, which means you can create multiple versions of a template to track changes over time. If you do not specify a version, the default version is used.

You can set any version of the launch template as the default version — by default, it’s the first version of the launch template.

Create a launch template and launch instance from template

We will create a launch template with Ubuntu AMI and t2.micro instance type with Jenkins and Docker setup (You can use the Day 39 User data script for installing the required tools).

Step 1: Login to AWS Console and on search bar type "EC2".

Step 2: Select Launch Templates on the left side of your screen under Instances.

Step 3: Click on "Create Launch Template" Button.

Step 4: In Create launch template, under Launch template name and description give the following details:

  1. Launch template name: DockerJenkinsTemplate

  2. Template version description: A template with Docker and Jenkins installed

Step 5: Now, Select the below AMI and instance type.

  1. Application and OS Images (Amazon Machine Image): Ubuntu

  2. Instance Type: t2.micro

Step 6: In Advance details, under user data use the below script: -

#!/bin/bash
sudo apt-get update -y
sudo apt install openjdk-11-jre -y
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo apt-get update
sudo apt-get install docker.io -y
sudo systemctl start docker

Step 7: Click on Create Launch Template.

Step 8: In the Launch Templates Dashboard, select the template you created and click on Actions. Select Launch Instance from the template.

Step 9: Since we need 3 instances from the template we created, In Summary > Number of Instances > Type 3.

Step 10: Select/Create the Key Pair.

Step 11: Click on Launch Instance.

Step 12: Go to instances, You will see three instances that we created now is running.

So, Finally we have created our instances using template.🎉

Create Auto Scaling Group

An Auto Scaling group is a collection of Amazon EC2 instances that are treated as a logical unit. You configure settings for a group and its instances as well as define the group’s minimum, maximum, and desired capacity.

Setting different minimum and maximum capacity values forms the bounds of the group, which allows the group to scale as the load on your application spikes higher or lower, based on demand.

To scale the Auto Scaling group, you can either make manual adjustments to the desired capacity or let Amazon EC2 Auto Scaling automatically add and remove capacity to meet changes in demand.

Let’s create ASG(Auto Scaling Group) and make the process more automated depending on the usage.

Step 1: On your instance page to left side of your screen, you can find Auto Scaling Groups.

Step 2: Go to Auto Scaling Group.

Step 3: Click on Create Auto Scaling Group.

Step 4: Choose the launch template or configuration

  1. Auto Scaling group name: DockerJenkinsScaling

  2. Launch template: DockerJenkinsTemplate

  3. Version: Default(1)

Step 5: Click on Next.

Step 6: In choose instance launch options, Select the Availability Zones.

Step 7: Click on Next.

Step 8: In Configure advanced options, Select Attach to a new load balancer in Load balancing, In Listener and Routing, Select Create a target group. And let the other options be as default.

Step 9: Click on Next.

Step 10: Configure group size and scaling policies.

  1. Desired capacity: 3

  2. Minimum capacity: 1

  3. Maximum capacity: 5

  4. And leave all the options as it is.

Step 11: Click on Next.

Step 12: Add notifications. Leave it as it is.

Step 13: Click on Next.

Step 14: Add tags. Leave it as it is.

Step 15: Click on Next.

Step 16: Review your ASG.

Step 17: Click on "Create Auto Scaling group".

When the utilization increases or decreases, the instances are scaled up or down automatically, based on the criteria we have given.

Conclusion

In Conclusion, A launch template is a configuration template that defines various settings for launching EC2 instances, such as the Amazon Machine Image (AMI), instance type, key pair, security groups, and user data. It provides a convenient way to ensure consistency and repeatability when launching instances.

Auto Scaling Groups (ASG) enable automatic scaling of EC2 instances based on defined policies. They use launch templates to specify the configurations of the instances they launch. ASGs help ensure that the desired number of instances is maintained, providing scalability and high availability.

Together, launch templates and auto-scaling groups simplify the process of deploying and managing EC2 instances in a dynamic and automated manner. They enhance flexibility, enable efficient resource utilization, and help in responding to changing workloads or demand patterns by automatically adjusting the number of instances.

In this blog, I have discussed how to automate EC2 instance creation using Launch Templates and Auto Scaling Groups.

Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨

*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊

~Smriti Sharma✌