Day 40 of 90 Days of DevOps Challenge: AWS EC2 Automation

ยท

4 min read

AWS

I hope your journey with AWS cloud and automation is going smoothly! ๐Ÿ˜ Today, we're diving deeper into automating tasks in AWS EC2.

Automation in EC2:

Amazon Elastic Compute Cloud (EC2) offers secure, reliable, high-performance, and cost-effective computing infrastructure to meet various business needs. By leveraging automation techniques, you can significantly reduce manual effort and optimize workflows.

Launch Templates in AWS EC2:

A launch template simplifies the process of launching new instances. It saves your frequently used configurations (such as AMI ID, instance type, and network settings) so you can quickly launch instances without repeatedly entering the same details.

  • Benefits of a Launch Template:

    • Saves time by reusing configurations

    • Ensures consistency across instances

    • Allows you to automate instance creation with pre-configured setups

Instance Types:

EC2 offers a wide variety of instance types, each optimized for different workloads. Choosing the right combination of CPU, memory, storage, and network capacity ensures that your applications perform efficiently.

AMI:

An Amazon Machine Image (AMI) provides the necessary information to launch an instance. If you need to launch multiple instances with the same setup, using an AMI ensures that they all have the same configuration.


Task 1: Automate EC2 Instance Creation with Launch Templates

Step 1: Create a Launch Template

  1. Navigate to EC2 in the AWS Console: Go to the AWS Management Console and select EC2.

  2. Launch Template Creation:

    • In the left navigation pane, click Launch Templates.

    • Click Create Launch Template.

    • Name your template and give it a description.

    • AMI Selection: Choose Amazon Linux 2 AMI.

    • Instance Type: Select t2.micro (free tier eligible).

    • Key Pair: Choose an existing key pair or create a new one for SSH access.

    • Security Group: Select a security group that allows HTTP and SSH access (you can create one if necessary).

    • User Data: Use the user data script from Day 39 to install Jenkins and Docker automatically:

        #!/bin/bash
      
        # Update the instance
        sudo apt update -y
      
        # Install Java (OpenJDK 11)
        sudo apt install openjdk-17-jdk -y
      
        # Add Jenkins repository and install Jenkins
        wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.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 update -y
        sudo apt install jenkins -y
      
        # Start and enable Jenkins
        sudo systemctl start jenkins
        sudo systemctl enable jenkins
      
        # Install Docker
        sudo apt install docker.io -y
        sudo systemctl start docker
        sudo systemctl enable docker
      
        # Add 'docker' group permissions to both 'ubuntu' user and 'jenkins' user
        sudo usermod -aG docker $USER
        sudo usermod -aG docker jenkins
      
        # Restart Jenkins to apply Docker group changes
        sudo systemctl restart jenkins
      
    • Network Settings: Choose the default VPC and subnet, unless you require a specific network setup.

    • Storage: Leave it as the default (8 GiB General Purpose SSD).

    • Tags: (Optional) Add tags for better instance management.

  3. Save the Launch Template: After configuring all the details, click Create Launch Template.

Step 2: Launch EC2 Instances from the Template

  1. Use the Launch Template: Go back to the Launch Templates section, select the template you just created, and click Actions > Launch Instance from Template.

  2. Launch Multiple Instances: In the launch instance wizard, find the option to specify the number of instances to launch. Set this to 3.

  3. Launch the Instances: Review your configurations and click Launch.

Step 3: (Optional) Create an Auto-Scaling Group

  1. Navigate to Auto Scaling Groups: On the EC2 Dashboard, under Auto Scaling, click Auto Scaling Groups.

  2. Create an Auto Scaling Group:

    • Choose your Launch Template.

    • Specify a VPC and Subnets.

    • Set the Desired Capacity, Minimum, and Maximum number of instances.

    • Configure the Scaling Policies to adjust the number of instances based on traffic or resource usage.

  3. Complete Setup: Follow the on-screen instructions to complete the auto-scaling setup.


Conclusion

Today, you learned how to automate EC2 instance creation using Launch Templates, a powerful tool for reusing configurations and simplifying the process of spinning up new instances. Additionally, you've explored auto-scaling, which ensures that your infrastructure dynamically adjusts to demand, giving you both cost-efficiency and reliability.

By automating these tasks, you're taking a significant step toward optimizing cloud workflows and increasing the efficiency of your cloud infrastructure.

Keep experimenting and pushing your limits, and don't forget to apply what youโ€™ve learned in your real-world DevOps tasks. Happy automating! ๐ŸŽ‰

ย