Day 40: AWS EC2 Automation ☁

Pooja BhavaniPooja Bhavani
4 min read

Automation in EC2:

  • Amazon EC2 or Amazon Elastic Compute Cloud can give you secure, reliable, high-performance, and cost-effe

  • ctive computing infrastructure to meet demanding business needs.Also, if you know a few things, you can automate many things.

Launch template in AWS EC2:

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

  • You can tell the Amazon EC2 console to use a certain launch template when you start an instance.

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.

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.

Task1:

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.

  • Go to the EC2 dashboard in the AWS Management Console.

  • Click on "Launch Templates" in the left sidebar and then "Create launch template".

  • Provide a descriptive name for your template (e.g., "JenkinsDockerTemplate") and an optional description and click on Auto Scaling guidance.

  • Click on Quick Start and choose the desired Amazon Linux 2 AMI version.

  • Select "t2.micro" for this example. You can adjust this based on your resource requirements later.

  • Select you Key pair as your Requirement.

  • Navigate to the "Advanced" section and expand "User data."

  • Choose "Paste user data" and paste the complete user data script that install Jenkins and Docker.

#!/bin/bash

# Installing Jenkins
sudo apt update
sudo apt install fontconfig openjdk-17-jre -y

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
sudo apt-get install jenkins -y

sudo apt install docker.io -y

  • Carefully review all configuration details, especially the user data script and Click "Create Launch Template" to create your template.

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

  • Go to the EC2 dashboard and click on "Instances" in the left sidebar and Click on "Launch Instance Arrow" in that choose "Launch instance from template".

  • Select the launch template you created earlier.

  • Specify the number of instances to be launched. This option is usually found in the right side "Summary", under the "Number of instances" section. Enter "3" in this field to launch three instances from your chosen Launch Template.

  • In Network settings Subnet select us-east-1a and security group select launch-wizard-1 and continue configuring the instance details as needed and launch the instances.

  • Review your configuration, including the number of instances, and click "Launch" to create three EC2 instances with the pre-configured settings from your Launch Template.

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

  • In the AWS Management Console, navigate to the EC2 service.Under the "Auto Scaling" section in the left navigation pane, select "Auto Scaling groups."Click on "Create Auto Scaling group."

  • Name your Auto scaling group name and under "Launch source," select "Launch template" and choose your "JenkinsDockerTemplate" and click on Next.

  • In Choose instance launch options provide the subnet you want to keep your servers at and click on Next.

  • In Configure advanced options leave it as default and click on Next.

  • In Configure group size and scaling you have to Set the "Desired capacity" as default and in scaling you have to choose limits in Min desired capacity you have keep ad default and in Max desired capacity you have to keep 5 and Instance scale-in protection and click on Next.

  • In Add notifications keep it default and click on Next and In Add tags also keep as default and click on Next.

  • Carefully review your configuration, especially the scaling policies, to ensure they align with your workload needs and Click "Create Auto Scaling group" to bring your ASG to life!

  • Your Auto scaling group is created.


0
Subscribe to my newsletter

Read articles from Pooja Bhavani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pooja Bhavani
Pooja Bhavani