Day 40: 90DaysOfChallenge

Day 40: 90DaysOfChallenge

AWS EC2 Automation

EC2

EC2 stands for Elastic Compute Cloud, and it's a key service provided by Amazon Web Services (AWS). In simple terms, EC2 is like renting virtual computers in the cloud. The "Elastic" part means one can easily scale the computing capacity up or down depending on their needs. Compute refers to the computing power provided by the virtual machines (instances) that is rented. EC2 provides a flexible and scalable solution for running applications and services in the cloud, without the hassle of managing physical hardware.

AMI

An Amazon Machine Image (AMI) is a supported and maintained image provided by AWS that provides the information required to launch an instance. One must specify an AMI while launching an instance. One can launch multiple instances from a single AMI whenever multiple instances with the same configuration is required. One can use different AMIs to launch instances whenever instances with different configurations is required.

Instance Type

Amazon EC2 has a large number of instance types that are optimized for different uses. The different combinations of CPU, memory, storage and networking capacity in instance types gives the freedom to choose the right mix of resources for their apps. Each instance type comes with one or more instance sizes, so one can adjust the resources to meet the needs of the workload they want to run.

Launch Template

One can use a launch template to store instance launch parameters so that parameters do not have to be specified every time an instance is launched. For example, one can create a launch template with the AMI ID, instance type, and network settings that is typically used to launch instances. When one wants to launch an instance using the Amazon EC2 console, an AWS SDK, or a command line tool, they can specify the launch template instead of entering the parameters again.

The following diagram shows a launch template with three versions. The first version specifies the instance type, AMI ID, subnet, and key pair used to launch the instance. The second version is based on the first version and also specifies a security group for the instance. The third version uses different values for some of the parameters. Version 2 is set as the default version. If one launched an instance from this launch template, the launch parameters from version 2 would be used if no other version were specified.

Task

  • 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.

    1. Login to AWS Management Console.

    2. Go to EC2 Dashboard and click on Launch Templates Option present on left side bar.

    3. The below page opens up. Click on Create launch template.

    4. Provide the Launch template name and Template version description.

    5. Select Amazon Linux as Amazon Machine Image.

    6. Select Instance Type as t2.micro and and key pair value from the values stored in the local.

    7. Select the existing security group if you have any or create a new one.

    8. Expand Advance Details Option and enter the below code to install Java, Jenkins and Docker in User Data Script text field.

Note:The below script would run for Ubuntu AMI. For running the below user scripts, select AMI as Ubuntu instead of Amazon Linux 2.

#!/bin/bash

#Install Java
sudo apt update -y
sudo apt install fontconfig openjdk-17-jre -y
java --version

#Install Jenkins
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update -y
sudo apt-get install jenkins -y

#Install Docker
sudo apt-get update -y
sudo apt install docker.io -y

  1. Click on Create Template at the bottom of the screen.

  2. The template is created successfully.

  3. A template with name "qaserver" is created successfully.

  4. Let's start and connect the instance to check if Jenkins and Docker is installed or not. Enter the below commands to check the status of Java, Jenkins and Docker.

$ docker --version
$ java --version
$ jenkins --version

We can see Java, Jenkins and Docker is installed in our EC2 Instance.

  • Create 3 Instances using Launch Template, there must be an option that shows number of instances to be launched ,can you find it? :)

  1. Go to EC2 Dashboard.

  2. Click on "Launch instance from template" from the dropdown of "Launch Instance"

  3. Select the "qaserver" template created earlier from the list of dropdown on Source Template field.

  4. Provide Number of Instance = 3 and click on Launch Instance at the bottom. Here, one can provide the number for launching the number of instance.

  5. 3 Instances are created successfully with 3 instance ids as below. Click on each of the instance ids to launch the instance.

  6. Wait for few minutes for the instances to be up and running. Go back to Dashboard to check all the 3 instances are up and running.

  7. Connect any one of the instance to check instance is up and running. Enter the commands for checking the version of Java, Jenkins and docker to check if they are installed or not.

  • You can go one step ahead and create an auto-scaling group, sounds tough?

Auto Scaling

Amazon EC2 Auto Scaling ensures that one have the correct number of Amazon EC2 instances available to handle the load for their application. One can create collections of EC2 instances, called Auto Scaling groups. where they can specify the minimum number of instances in each Auto Scaling group, and Amazon EC2 Auto Scaling ensures that the group never goes below this size. One can specify the maximum number of instances in each Auto Scaling group, and Amazon EC2 Auto Scaling ensures that the group never goes above this size. If one specifies the desired capacity, either when they create the group or at any time thereafter, Amazon EC2 Auto Scaling ensures that the group has this many instances. If one specify scaling policies, then Amazon EC2 Auto Scaling can launch or terminate instances as demand on the application increases or decreases. Below is an example of Auto Scaling group -

Let's create an Auto Scaling group now -

  1. Go to EC2 Dashboard and click on Auto Scaling on left side bar.

2. Click on Create Auto Scaling Group.

3. Provide the Auto Scaling Group Name.

4. Select the "qaserver" template which we have created earlier and click on Next.

5. Select the default VPC and the default subnet from the Network Configuration.

6. Specify the Group Size. Here, Desired capacity entered is 4.

7. Provide the minimum and the maximum desired capacity. Here, min desired capacity entered is 1 and max desired capacity is 4.

8. Click on Next and setup the configuration and at last click on Create Auto Scaling Group. Auto Scaling group is created as seen below -

9. We can see we have 5 instances running which is the maximum desired capacity. We had 1 instance created earlier and after implementation of auto scaling group 4 new instances were created as the desired capacity in auto scaling group was 4.

10. Let's delete two instances and check if one new instances is created automatically or not as the desired capacity in auto scaling group is 4.

11. We can see one new instance is created with 4 instances up and running.

Hurray! We have learnt how to launch a template, create instances from the template and how to scale instances using Auto Scaling Group.

Thanks for reading!

Happy Learning!