Day 40: Streamlining AWS EC2 Tasks through Automation

Day 40: Streamlining AWS EC2 Tasks through Automation


3 min read

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.

CloudFormation is AWS's infrastructure as a code service. You can use CloudFormation templates to define and provision your AWS infrastructure, including EC2 instances, in a repeatable and automated way.

To automate tasks in EC2, you'll typically use a combination of these services and tools, depending on your specific use case and requirements. This can range from basic instance provisioning to complex.

Launch Template in EC2

An Amazon EC2 Launch Template is a resource that contains the launch parameters to launch instances in an Amazon EC2 Auto Scaling, Spot Fleet, or an EC2 On-Demand capacity reservation. It allows you to specify the instance type, AMI (Amazon Machine Image), key pair, security groups, block device mappings, and other instance launch settings in a single configuration. EC2 Launch Templates help you standardize and automate instance launches, making it easier to manage your EC2 instances.

Instance Types

Amazon EC2 has a large number of instance types that are optimised 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 can adjust your resources to meet the needs of the workload you want to run.

Amazon Machine Image (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.


Create a launch template with Amazon Linux 2 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.

Create 3 Instances using Launch Template, there must be an option that shows the number of instances to be launched

Step 1: Create a Launch Template:

  • Log in to the AWS Management Console. Navigate to the EC2 dashboard.

  • In the navigation pane on the left, under "Instances," click on "Launch Templates."

  • Click the "Create launch template" button.

  • Fill in the details mentioned below:

    • Choose the latest Amazon Linux 2 AMI.

    • Select the "t2.micro" instance type.

    • Configure the instance details, including instance role and monitoring settings.

    • For network settings, specify your VPC and subnet.

    • Configure storage settings as needed.

    • In the "Advanced details" section, add the following user data script:

        # Update and install required packages
        sudo yum update -y
        sudo yum install -y java-1.8.0-openjdk
        sudo yum install -y docker
        sudo service docker start
        # Install Jenkins
        sudo wget -O /etc/yum.repos.d/jenkins.repo
        sudo rpm --import
        sudo yum install -y jenkins
        sudo service jenkins start
    • Click "Create launch template" to save your configuration.

Step 2: Launch instances from the Launch Template:

  • After creating the launch template, go to the "Instances" section in the EC2 dashboard.

  • Click the "Launch Instances" button.

  • Choose the "My launch template" tab and select the launch template you created.

  • In the "Number of instances" field, specify "3" to launch three instances.

  • Configure other instance details as needed, such as network settings and storage.

  • Review and launch the instances, and follow the prompts to create or select a key pair for SSH access.

  • Click "Launch Instances" to start the instances.

This will launch three EC2 instances with Amazon Linux 2, t2.micro instance type, and Jenkins and Docker installed using the provided user data script.

<That's all for today. Hope you like it. FOLLOW to join me in the journey of DevOps>