Day 27: AWS EC2 Automation ☁

Day 27: AWS EC2 Automation ☁

Hello Everyone!

In the last article, we saw AWS basics and IAM. Now let’s dive into the next part which is automation in EC2.

Amazon Elastic Compute Cloud (Amazon EC2) provides on-demand, scalable computing capacity in the Amazon Web Services (AWS) Cloud. Using Amazon EC2 reduces hardware costs so you can develop and deploy applications faster.

You can use Amazon EC2 to launch as many or as few virtual servers as you need, configure security and networking, and manage storage. You can add capacity (scale up) to handle compute-heavy tasks, such as monthly or yearly processes, or spikes in website traffic. When usage decreases, you can reduce capacity (scale down) again.

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.

Auto Scaling in EC2

Amazon Elastic Compute Cloud (EC2) Auto Scaling is a feature that ensures the right number of Amazon EC2 instances are available for an application’s load. You can create a collection of EC2 instances (an auto-scaling group), specifying the minimum number of instances in the group—EC2 Auto Scaling will ensure the group always has enough instances.

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.

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

  • Create 3 Instances using Launch Template.

  • You can go one step ahead and create an auto-scaling group.

Open the AWS console and go to ec2

Here we choose, Amazon Linux 2 AMI as told.

Click on Advanced Details. Use the following script as user data.

#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
EC2AZ=$(TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` && curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/placement/availability-zone)
echo '<center><h1>This Amazon EC2 instance is located in Availability Zone: AZID </h1></center>' > /var/www/html/index.txt
sed "s/AZID/$EC2AZ/" /var/www/html/index.txt > /var/www/html/index.html

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.

Let's create an Auto Scaling group

  • From EC2, click on the auto-scaling groups tab.

  • Choose the launch template that you created.

  • Choose two AZ in the network tab.

  • I'm selecting the instance numbers as two for desired, minimum, and maximum running capacity.

  • Create the Auto Scaling group and check in EC2 for the launched instances.

Let's connect the IP address of each instance and see the results. Results should show the availability zone of each instance as we have configured in user data.

AS-1

AS-2

Hence, we conclude that we have successfully configured a Launch template and launched 2 EC2 instances by Autoscaling group.


Thanks for reading;)