AWS EC2 Automation Made Easy with Launch Templates and AMIs

How to Create and Use Launch Templates and Amazon Machine Images in AWS EC2 for Faster and Easier Configuration

AWS EC2 Automation Made Easy with Launch Templates and AMIs

Are you a DevOps learner who wants to automate your AWS EC2 instances? If yes, then this blog post is for you.

In this post, I’ll explain how to use launch templates and AMI in AWS EC2, two important concepts that can help you automate your EC2 configuration and performance.

By the end of this post, you’ll be able to:

  • Understand what launch templates and EC2 instance types are and how they work in AWS EC2

  • Understand what AMI is and how to choose one for your instances

  • Create a launch template with Amazon Linux 2 AMI and t2.micro instance type with Jenkins and Docker setup

  • Create three instances using the launch template and an auto-scaling group

Let’s get started!

What are Launch Templates and different EC2 Instance Types available in AWS EC2?

Launch templates are a feature of AWS EC2 that can help you automate your EC2 configuration and performance.

Launch Templates

A launch template is a template that contains the configuration information that you need to start an instance. You can save launch parameters in launch templates so you don’t have to type them in every time you start a new instance. For example, a launch template can have the AMI ID, instance type, network settings, user data, security groups, tags, etc. that you usually use to launch instances.

You can tell the Amazon EC2 console or the AWS CLI to use a certain launch template when you start an instance. You can also use launch templates with other AWS services such as auto-scaling groups, spot fleets, or EC2 Fleet.

Launch templates can help you simplify and standardize your EC2 configuration and reduce errors. You can also create different versions of launch templates to keep track of changes or test different configurations.

EC2 Instance Types

EC2 instance types are different combinations of CPU, memory, storage, and networking capacity that you can choose for your instances. The different instance types are optimized for different use cases such as general purpose, compute-optimized, memory-optimized, storage-optimized, etc.

Each instance type comes with one or more instance sizes, so you can adjust your resources to meet the needs of the workload you want to run. For example, t2.micro is an instance type that belongs to the general purpose family and has 1 vCPU and 1 GiB of memory. You can choose a larger or smaller size within the same family or switch to a different family depending on your requirements.

You can also choose between on-demand instances, reserved instances, spot instances, or dedicated instances depending on your pricing and availability preferences.

EC2 instance types can help you optimize your EC2 performance and cost by choosing the right mix of resources for your applications.

What is AMI and How to Choose One for Your Instances?

An AMI or Amazon Machine Image is an image that AWS supports and keeps up to date. It contains the information needed to start an instance, such as the operating system, the applications, and the configuration settings. When you launch an instance, you must choose an AMI that suits your needs and preferences.

There are different types of AMIs that you can choose from, such as:

  • AWS-provided AMIs: These are AMIs that AWS creates and maintains for common operating systems and platforms, such as Amazon Linux, Ubuntu, Windows, etc. You can use these AMIs for free or pay a small fee depending on the AMI type and usage.

  • AWS Marketplace AMIs: These are AMIs that third-party vendors create and sell on the AWS Marketplace. You can use these AMIs to access pre-configured software and services, such as databases, web servers, security tools, etc. You can pay for these AMIs per hour or per month depending on the vendor’s pricing model.

  • Community AMIs: These are AMIs that other AWS users create and share for free or for a fee. You can use these AMIs to access custom software and configurations that other users have created and tested. You can also create and share your own community AMIs if you want.

  • Your own AMIs: These are AMIs that you create from your own instances or snapshots. You can use these AMIs to launch instances with the same configuration and state as your original instance or snapshot. You can also copy or share your own AMIs with other AWS accounts or regions if you want.

To choose an AMI for your instances, you need to consider several factors, such as:

  • The operating system and platform that you want to run on your instances

  • The software and applications that you want to install or use on your instances

  • The security and compliance requirements that you need to meet for your instances

  • The performance and cost implications of using different types of AMIs

  • The availability and support of the AMIs in different AWS regions and accounts

You can browse and search for different types of AMIs in the AWS console or the AWS CLI. You can also filter and compare the AMIs based on various criteria, such as name, description, owner, platform, etc.

You can also use launch templates to save your preferred AMI and other launch parameters in a template that you can reuse when launching instances.

To illustrate how launch templates and EC2 instance types work, let’s see an example of creating and using them in AWS EC2.

How to Create and Use Launch Templates in AWS EC2

In this section, I’ll show you how to create and use launch templates and EC2 instance types in AWS EC2. We’ll use a scenario where we want to run Jenkins on three EC2 instances with a Docker setup. Jenkins is a popular open-source tool for continuous integration and continuous delivery (CI/CD) that allows you to automate the building, testing, and deployment of your software projects. Docker is a software platform that allows you to build, run, and share applications using containers.

To do this, we’ll use the following steps:

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

  2. Create three instances using the launch template and an auto-scaling group

Let’s see each step in detail.

Step 1: Create a Launch Template with Amazon Linux 2 AMI and t2.micro Instance Type with Jenkins and Docker Setup

To create a launch template, we’ll use the same user data script that we used in the previous blog post to install Jenkins and Docker on the instance. Here is the script:

#!/bin/bash
# Add Jenkins repository key and source
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
# Update packages
sudo yum upgrade -y
# Add required dependencies for the jenkins package
sudo yum install fontconfig java-17-amazon-corretto -y
# Install Jenkins
sudo yum install jenkins -y
# Reload the daemon
sudo systemctl daemon-reload
# Start Jenkins service
sudo systemctl start jenkins
# Enable Jenkins service
sudo systemctl enable jenkins
# Install Docker
sudo amazon-linux-extras install docker
# Start Docker service
sudo service docker start
# Enable Docker service
sudo systemctl enable docker
# Add ec2-user to docker group
sudo usermod -a -G docker ec2-user
# Restart the docker service
sudo systemctl restart docker

To create a launch template using this script, we’ll use the following steps:

  1. Go to the AWS console and navigate to the EC2 service.

  2. Click on Launch Templates in the left navigation pane.

  3. Click on the Create Launch template button.

  4. Enter a name and an optional description for your launch template, such as Jenkins-Docker-LT.

  5. Choose Amazon Linux 2 AMI (HVM), SSD Volume Type from the list of available AMIs.

  6. Choose t2.micro from the list of available instance types.

  7. In the User data section, paste the user data script that we created earlier.

  8. In the Network settings section, select Create New Security Group from the drop-down menu and enter a name and an optional description for your security group, such as Jenkins-Docker-SG.

  9. Click on the Add security group rule button and add a rule to allow inbound traffic on port 8080 for HTTP protocol from anywhere. This will allow us to access the Jenkins web page from our browser.

  10. Click on the Add rule button again and add a rule to allow inbound traffic on port 22 for SSH protocol from anywhere. This will allow us to SSH into our instances.

  11. In the Key pair (login) information section, select Choose an existing key pair from the drop-down menu and choose a key pair that you have access to or create a new one if you don’t have one already.

  12. In the Resource tags section, click on the Add tag button and add any tags that you want to assign to your launch template and your instances, such as Name, Project, etc.

  13. Click on the Create Launch Template button and wait for your launch template to be created.

You have successfully created a launch template with Amazon Linux 2 AMI and t2.micro instance type with Jenkins and Docker setup.

Step 2: Create Three Instances Using the Launch Template and an Auto-Scaling Group

To create three instances using the launch template, we’ll use an auto-scaling group that will automatically launch and manage the instances for us. An auto-scaling group is a group of EC2 instances that can automatically scale up or down based on demand or predefined rules. We’ll use an auto-scaling group to ensure that we always have three instances running with the same configuration and settings.

To create an auto-scaling group using the launch template, we’ll use the following steps:

  1. Go to the AWS console and navigate to the EC2 service.

  2. Click on Auto Scaling Groups in the left navigation pane.

  3. Click on the Create Auto Scaling group button.

  4. Enter a name for your auto-scaling group, such as Jenkins-Docker-ASG.

  5. Choose Launch Template as the source for your auto-scaling group and select the launch template that you created in the previous section, such as Jenkins-Docker-LT.

  6. Click on the Next button.

  7. Choose VPC as the network for your auto-scaling group and select one or more subnets from different availability zones. This will ensure high availability and fault tolerance for your instances.

  8. Click on the Next button.

  9. Enter 3 in the Desired capacity field. This will tell the auto-scaling group to launch three instances using the launch template.

  10. Enter 3 in the Minimum capacity field. This will tell the auto-scaling group to keep at least three instances running at all times.

  11. Enter 3 in the Maximum capacity field. This will tell the auto-scaling group to not launch more than three instances at any time.

  12. Choose None as the scaling policy for your auto-scaling group. This will tell the auto-scaling group to not scale up or down based on any metrics or alarms.

  13. Click on the Next button.

  14. Add any notifications or tags that you want to assign to your auto-scaling group.

  15. Click on the Next button.

  16. Review your auto-scaling group details and click on the Create Auto-Scaling group button.

  17. Wait for your auto-scaling group to be created.

You have successfully created an auto-scaling group using the launch template.

How to Access Jenkins Web Page on Each Instance and How to Test Docker Setup on Each Instance

In this section, I’ll show you how to access the Jenkins web page on each instance and how to test the Docker setup on each instance. We’ll use a tool called curl that can send HTTP requests and display the response in the terminal. We’ll also use a command called docker run that can run a container from a Docker image.

To do this, we’ll use the following steps:

  1. Go to the AWS console and navigate to the EC2 service.

  2. Click on Instances in the left navigation pane and select one of the instances that belong to your auto-scaling group, such as Jenkins-Docker-ASG-1.

  3. Copy the public IP address of the instance from the details panel.

  4. SSH into the instance using your key pair and the ec2-user username.

  5. Run curl by running the following command:

curl http://localhost:8080/

This command will send a GET request to the Jenkins web page on port 8080 and display the HTML response in the terminal.

  1. You should see some HTML code that contains the text “Unlock Jenkins”. This means that Jenkins is running on your instance and you need to unlock it using an initial password.

  2. To get the password, run the following command:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

This command will display the password that you need to enter on the Jenkins web page.

  1. Copy the password and paste it into your browser’s address bar after the instance’s public IP address and port 8080. For example, http://<instance-public-ip>:8080/<password>. You should see the Jenkins welcome page that asks you to customize Jenkins or install suggested plugins.

  2. Follow the instructions to complete the Jenkins setup.

  3. To test the Docker setup on your instance, run the following command:

docker run hello-world

This command will pull and run a Docker image called hello-world that prints a message in the terminal.

  1. You should see a message that says “Hello from Docker!” and some details about the Docker version and image ID. This means that Docker is running on your instance and you can run containers from Docker images.

  2. Repeat steps 2-11 for each of your instances that belong to your auto-scaling group.

You have successfully accessed the Jenkins web page on each instance and tested the Docker setup on each instance.

Conclusion

In this blog post, I have explained how to use launch templates and auto-scaling groups in AWS EC2, two powerful tools that can help you automate your EC2 configuration and scaling. I have also shown you how to create and use them in practice by launching three EC2 instances with Jenkins and Docker setup using a launch template and an auto-scaling group.

I have also shown you how to access the Jenkins web page on each instance and how to test the Docker setup on each instance.

I hope you found this blog post informative and engaging. If you want to learn more about AWS and DevOps, here are some links to additional resources:

This is the end of the blog post for Day 40.

Thank you for reading and stay tuned for more posts in this series! 😊

Did you find this article valuable?

Support Ajit Fawade by becoming a sponsor. Any amount is appreciated!