Day 40 AWS EC2 Automation

Radheya Zunjur
5 min readAug 6, 2023

--

In last article we saw AWS basics and IAM. Now let’s dive into next part as we explore the power of AWS EC2 automation and uncover the path to a more efficient, scalable, and resilient cloud infrastructure. Join us on this 90-day journey of discovery and transformation, where we unlock the secrets to mastering AWS EC2 automation.

What is Automation in EC2:

Automation in EC2 refers to the process of using scripts, tools, or services to automatically deploy, configure, manage, and scale Amazon Elastic Compute Cloud (EC2) instances. Instead of manually performing repetitive tasks and configurations, automation allows users to define desired configurations and actions, and the system takes care of executing them, reducing human intervention and potential errors.

There are several aspects of EC2 that can be automated:

  1. Instance Provisioning: Automation allows you to create EC2 instances on-demand, eliminating the need for manual instance creation. With automation, you can specify instance type, operating system, storage, and other configurations through scripts or templates.
  2. Auto Scaling: EC2 Auto Scaling enables automatic scaling of your EC2 instances based on predefined conditions such as CPU utilization, network traffic, or custom metrics. This ensures that your applications have the right amount of resources at all times, optimizing performance and cost efficiency.
  3. Instance Configuration: Automation enables you to configure EC2 instances automatically as they are launched. This includes tasks like installing software, applying security settings, setting environment variables, and more.
  4. Networking: Networking tasks like assigning Elastic IP addresses, creating security groups, and configuring network interfaces can be automated, ensuring consistent and reliable network setups.
  5. Backup and Snapshots: EC2 automation allows you to schedule automated backups and snapshots of your instances and volumes, ensuring data protection and disaster recovery capabilities.
  6. Load Balancing: Automation can set up and configure Elastic Load Balancers (ELB) to distribute incoming traffic across multiple EC2 instances, enhancing the availability and fault tolerance of your applications.
  7. Monitoring and Alerting: You can automate the setup of CloudWatch monitoring and alarm configurations to get insights into the performance and health of your EC2 instances, and receive notifications when predefined thresholds are breached.

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.

What are Instance Types?

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

What is 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.

Tasks

Task 1)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.

Open aws console and go to ec2

On left pannel, Open Launch Templates and Create new template

Here we choose, Amazon Linux 2 AMI as told.

Click on Advanced Details. Use the following script as we did in last article for user data.

#!/bin/bash
sudo apt-get update -y
sudo apt install openjdk-11-jre -y
curl -fsSL https://pkg.jenkins.io/debian-stable/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-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo apt-get update
sudo apt-get install docker.io -y
sudo systemctl start docker

Now click on Create launch template

In the Launch Templates Dashboard, select the template you created and click on Actions. Select Launch Instance from the template.

Since we need 3 instances from the template we created, In Summary > Number of Instances > Type 3.

Click on Launch Instance. You will see you created 3 instances in one go.

Let’s create ASG(Auto Scaling Group) and make the process more automated depending on the usage.

On the left side of your screen, you can find Auto Scaling Groups.

Step 1: Click on Create Auto Scaling Group.

Click on Autoscaling Groups. Create New Autoscaling Group

Give suitable name for the Autoscaling group and Select our newly created template.

Step 2: Choose instance launch options

Select the availability zones and click on Next.

Step 3: Configure advanced options

Load balancing: Attach to a new load balancer

Attach to a new load balancer:

Load balancer type: Application Load Balancer

Load balancer scheme: Internal

Step 4: Configure group size and scaling policies

Desired capacity: 3

Minimum capacity: 1

Maximum capacity: 5

Click on Next.

Step 5: Add notifications & Step 6: Add Tags

Click on Next.

Once you reach the Review section, click on Create AutoScaling Groups.

When the utilization increases or decreases, the instances are scaled up or down automatically, based on the criteria you have given.

--

--

Radheya Zunjur
Radheya Zunjur

Written by Radheya Zunjur

Database Engineer At Harbinger | DevOps | Cloud Ops | Technical Writer

No responses yet