Launch Template AWS EC2 Automation

Launch Template AWS EC2 Automation

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

You can make a launch template with the configuration information 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, and network settings that you usually use to launch instances.

What are Launch Templates?

Launch Templates are a resource within AWS that lets you store launch parameters so that you don't have to define them every time you launch an EC2 instance. This means consistent and repeatable EC2 or spot fleet instance launches. Think of them as blueprints for your EC2 instances, containing all the necessary specifications like instance type, AMI, storage, and networking details.

Benefits of Using Launch Templates

  1. Consistency & Repeatability: By using Launch Templates, you can ensure that every instance you launch has the same configuration, avoiding discrepancies and configuration drift.

  2. Versioning: Whenever you make a change to a launch template, AWS keeps track of it as a new version. This means you can easily roll back to a previous configuration or compare differences between versions.

  3. Simplified Spot Fleet Requests: With Launch Templates, requesting spot instances becomes a breeze. Just link the template when requesting a spot fleet, and AWS takes care of the rest.

  4. Combining with Auto Scaling: Launch Templates can be paired with EC2 Auto Scaling Groups. When your application needs to scale out, Auto Scaling can use the Launch Template to ensure every new instance has the desired configuration.

Automating EC2 with Launch Templates

Here’s a step-by-step guide on how to create and automate EC2 instance deployment using Launch Templates:

  1. Create a Launch Template:

    • Open the EC2 Dashboard.

    • Navigate to "Launch Templates" and click "Create launch template".

    • Specify the details such as name, description, AMI, instance type, storage, and any other required parameters.

    • Save the template.

  2. Version Management:

    • Whenever you need to update the template (for example, using a new AMI), simply create a new version. This ensures that any previously launched instances or linked services aren't affected until you want them to be.
  3. Automated Deployment:

    • For Spot Fleet Requests: Choose "Request Spot Instances" from the EC2 Dashboard, then select your launch template, desired number of instances, and bid strategy.

    • For Auto Scaling: While creating or updating an Auto Scaling Group, select your launch template. AWS will then use this template whenever it needs to add instances to the group.

  4. Maintenance and Updates:

    • If you need to update the instances linked to an Auto Scaling Group, simply update the Launch Template and then update the group to use the new version. New instances will use the updated configuration.

Creating an EC2 Instance Using a Launch Template: A Step-by-Step Guide

  1. Setting up the Launch Template:

    • Log in to your AWS Management Console.

    • Navigate to the EC2 Dashboard.

    • In the left-hand menu, under "Instances", select Launch Templates.

    • Click Create launch template.

    • Provide a name and description for your template. This makes it easier to identify later.

    • Specify the required configurations:

      • AMI: This is the base OS and software stack your instance will run.

      • Instance type: Choose the size and power based on your needs (e.g., t2.micro).

      • Storage (EBS): Define the volume type, size, and other attributes.

      • Key pair: This will be used to securely connect to your instance.

      • Any other configurations specific to your needs.

      • Click Create launch template.

      • Use Security Group that has SSH 22 for Login and Custom TCP 8080 for Jenkins.

      • Go to Advanced Details and Add User Data to Install Jenkins and Docker

        You can refer my blog https://kshitijaa.hashnode.dev/user-data-in-aws

    #!/bin/bash
    sudo apt update -y
    sudo apt install openjdk-11-jre-headless -y
    java -version 2>/home/ubuntu/jenkins_output.txt

    curl -fsSL https://pkg.jenkins.io/debian/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 binary/ | sudo tee \
      /etc/apt/sources.list.d/jenkins.list > /dev/null
    sudo apt-get update -y
    sudo apt-get install jenkins -y

    sudo systemctl enable jenkins
    sudo systemctl start jenkins
    sudo systemctl status jenkins >>/home/ubuntu/jenkins_output.txt

    sudo apt install docker.io -y
    sudo systemctl enable docker
    docker --version  2>/home/ubuntu/docker_output.txt
    sudo systemctl start docker >>/home/ubuntu/docker_output.txt
    sudo usermod -aG docker $USER
    sudo gpasswd -a $USER docker

See the Template Details

  1. Launching an EC2 Instance Using the Template:

    • From the Launch Templates page, find the template you just created.

    • Select the template, then click Actions > Launch instance from template.

  • You can make any changes as Number of instance to 3 and keep everything as defined in the template.

  • Once you've reviewed your settings, click Launch instance. AWS will now provision a new EC2 instance based on your template specifications.

Reviewing and Managing your Instance:

  • Return to the EC2 Dashboard and click on Instances. You should see your newly created 3 instances launching or running.

  • Connect, manage, and monitor your instance as needed.

Now Let's Just browse the public Ip address of all these 3 instances with port 8080 to see Jenkins is installed or not

BOOM Our Template Is Working As Expected!

Conclusion

Launch Templates in AWS not only simplify the EC2 launch process but also ensure consistency across launches. They encapsulate best practices and desired configurations into a single resource, which can then be used across different AWS services. By integrating Launch Templates into your cloud strategy, you can make the process of provisioning and managing EC2 instances more streamlined and automated.