Automating Scalable Infrastructure on AWS with Launch Templates and Auto Scaling Groups
Introduction:
In today's fast-paced world, the ability to scale infrastructure efficiently is crucial for businesses to meet growing demands. Amazon Web Services (AWS) offers powerful tools like Launch Templates and Auto Scaling Groups that simplify the process of provisioning and managing instances while ensuring high availability and cost-effectiveness. In this blog, we'll explore how to create a Launch Template with Jenkins and Docker setup on Amazon Linux 2 AMI, launch multiple instances from it, and then take a step further by creating an Auto Scaling Group for dynamic scalability.
Step 1: Creating a Launch Template with Jenkins and Docker
To create a Launch Template, we'll first prepare the user data script to install Jenkins and Docker on our instances. This script will be executed when the instances start up, automating the software installation process.
#!/bin/bash
# Day 39 User Data script for installing Jenkins and Docker
# Update the package repository
sudo yum update -y
# Install Jenkins
sudo amazon-linux-extras install java-openjdk11 -y
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.key
sudo yum install jenkins -y
sudo systemctl start jenkins
sudo systemctl enable jenkins
# Install Docker
sudo amazon-linux-extras install docker -y
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ec2-user
Next, we'll create the Launch Template using the AWS Management Console:
Go to the EC2 Dashboard and click on "Launch Templates" in the navigation pane.
Click the "Create launch template" button.
Choose an Amazon Machine Image (AMI) that suits your requirements, such as Amazon Linux 2.
Select the desired instance type, in this case, t2.micro.
In the "Advanced Details" section, paste the user data script in the "User data" field.
Optionally, you can configure additional settings like instance tags, IAM instance profiles, and more.
Finally, click "Create launch template" to save the template.
Step 2: Launching Instances Using the Launch Template
Now that we have our Launch Template set up, let's proceed to launch multiple instances from it. AWS provides an option to specify the number of instances to be launched.
Go to the EC2 Dashboard and click on "Launch Templates" in the navigation pane.
Select the Launch Template you created in Step 1.
Click the "Actions" button and choose "Create instance."
In the dialog box that appears, specify the number of instances you want to launch (e.g., 3 instances).
Adjust other settings like subnet, security group, key pair, etc., as per your requirements.
Click "Launch Instances" to start the provisioning process.
You can launch instances using the AWS Management Console by selecting the Launch Template you created in Step 1 and specifying the number of instances you want to launch.
Alternatively, you can use the AWS CLI to launch instances with the Launch Template. Here's an example:
aws ec2 create-launch-template --launch-template-name "MyLaunchTemplate" \
--version-description "Version 1" \
--launch-template-data "{
\"ImageId\": \"ami-xxxxxxxxxxxxxxxxx\",
\"InstanceType\": \"t2.micro\",
\"UserData\": \"<user-data-script>\"
}"
Replace "ami-xxxxxxxxxxxxxxxxx" with the actual Amazon Linux 2 AMI ID.
Step 3: Creating an Auto Scaling Group (ASG) for Dynamic Scalability
Auto Scaling Groups allow us to automatically adjust the number of instances based on the load or other defined criteria. Let's create an Auto Scaling Group using the AWS Management Console:
Go to the EC2 Dashboard and click on "Auto Scaling Groups" in the navigation pane.
Click the "Create Auto Scaling Group" button.
Select the Launch Template you created earlier.
Set the minimum and maximum number of instances the group should maintain (e.g., Min: 2, Max: 5).
Define scaling policies based on CPU utilization, network traffic, or custom metrics.
Choose the desired subnets and security groups for the instances.
Complete the wizard by specifying additional configurations like tags and notifications.
You can create an Auto Scaling Group using the AWS Management Console or AWS CLI. Here's an example using the AWS CLI:
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name "MyAutoScalingGroup" \
--launch-template "LaunchTemplateName=MyLaunchTemplate,Version=1" \
--min-size 1 \
--max-size 5 \
--desired-capacity 2 \
--availability-zones "us-east-1a,us-east-1b"
In this example, we set the minimum capacity to 1 instance, the maximum capacity to 5 instances, and the desired capacity to 2 instances. The Auto Scaling Group will automatically adjust the number of instances to maintain the desired capacity based on the specified Launch Template and scaling policies.
Note: Make sure to replace the placeholders with your actual Launch Template name, version, and AMI ID.
Conclusion:
With Launch Templates and Auto Scaling Groups, you can easily create a scalable and resilient infrastructure on AWS. By automating instance provisioning and managing scaling based on demand, businesses can ensure cost efficiency and meet varying workloads effectively. Implementing these strategies can empower organizations to focus on building and deploying applications without worrying about the underlying infrastructure's elasticity. Embrace the power of AWS to simplify and optimize your cloud infrastructure for today's dynamic business landscape.
To connect with me - https://www.linkedin.com/in/subhodey/