Skip to main content

Command Palette

Search for a command to run...

Day 40- 90DaysOfDevOps

AWS EC2 Automation

Published
5 min read
Day 40- 90DaysOfDevOps

Hey Learners! Welcome back. In this challenge, we'll automate the task of creating EC2 and understand the concept of Templates in the AWS EC2 service. Let's start...

Automation in EC2:-

Amazon EC2 or Amazon Elastic Compute Cloud can give you secure, reliable, high-performance, and cost-effective computing infrastructure to meet demanding business needs.

Launch template in AWS EC2:-

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.

Use launch templates to automate instance launches, simplify permission policies, and enforce best practices across your organization. Save launch parameters in a template that can be used for on-demand launches and with managed services, including EC2 Auto Scaling and EC2 Fleet. Easily update your launch parameters by creating a new launch template version.

For example launch template, you can create one or more numbered launch template versions. Each version can have different launch parameters. When you launch an instance from a launch template, you can any version of the launch template. 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.

Instance Types:-

Amazon EC2 has a large number of instance types that are optimized for different uses. The different combinations of CPU, memory, Storage and networking capacity in instance types give you the freedom to choose the right mix of resources for your apps.

Each instance type comes with one or more instance sizes, so you adjust your resources to meet the needs of the workload you want to run.

AMI:-

An Amazon Machine Image (AMI) is an image that AWS supports and keeps up to date. It contains the information needed to start an instance. When you launch an instance, you must choose an AMI. When you need multiple instances with the same configuration, you can launch them from a single AMI.

Task 1:- Create a launch template with Amazon Linux 2 AMI and t2.micro instance type with Jenkins and Docker setup.

Log in to the AWS console and select EC2 service. In the left navigation pane, choose Launch Templates. Select Create launch template.

Enter a name for the launch template. Choose Amazon Linux 2 AMI as AMI with instance type t2.micro(#DilSeIndian).

Select the security group and enter the user data script for installing Docker and Jenkins in the Advanced details section. Select Key Pair for your instance and create a launch template.

Use the below script to install and run Docker and Jenkins in Amazon Linux...

sudo yum update
sudo yum install docker -y
sudo systemctl start docker
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
sudo yum install fontconfig java-11-amazon-corretto -y
sudo yum update
sudo yum install jenkins -y
sudo systemctl start jenkins

You can see the template created in the launch template dashboard.

Task 2- Create 3 Instances using Launch Template, There must be an option that shows the number of instances to be launched, can you find it?

To launch an instance from the launch templates select the Launch template you created earlier click on Actions and select Launch instance from the template.

OR

Go to the Instances drop-down menu of Launch Instance select Launch instance from the template and select the template you created earlier.

Specify the Number of instances as 3 select appropriate configuration settings as required and click Launch instance. Check whether the instances are created or not.

Select any one of the instances copy the Public IP and check with port 8080 on any browser whether Jenkins is installed or not. Add Port 8080 in SG for the selected Instance.

Auto-Scaling Group:-

Auto Scaling groups are collections of Amazon EC2 instances that enable automatic scaling and fleet management features. These features help you maintain the health and availability of your applications.

Goal:-

  1. Scale-Out (Add EC2 instances to match an increased load).

  2. Scale In (Remove EC2 instances to match a decreased load).

  3. Ensures we have a minimum and maximum number of machines running.

  4. Automatically register new instances to a load balancer.

  5. Replace unhealthy instances.

Create an auto-scaling group

Log in to the AWS console, search for EC2 service, scroll down and from the left navigation pane select Auto Scaling Group and select Create Auto Scaling Group.

Enter the name of the group and select a launch template as created earlier. Click Next.

Select the network settings like VPC, Subnets, Availablity Zones etc. as per requirement and click Next.

Leave default settings for Load Balancing for now and click Next.

Configure group size and scaling policies. Click Next. Add notifications and tags if you want to or you can just click Next to review the details*.*

Finally, click Create Autoscaling group and wait for some time to create. After getting created select ASG and check the activity logs for the same.

Check created instances.

Activity:-

Terminate one of the running instances and check the activity in ASG.

You can see that one new instance is created after terminating one as we have 2 as the desired capacity. Check in EC2 instances also.

This is how we can use launch templates with Auto Scaling Group. Don't forget to delete whatever you practised. After deleting ASG EC2 instances also deleted automatically, no need to delete manually.

Thank you so much for taking the time to read till the end! Hope you found this blog informative.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Find me on:- Hashnode LinkedIn Github

Happy Learning!