AWS EC2 Automation

AWS EC2 Automation

#90 Days of DevOps Challenge - Day 40

Define EC2:-

Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the Amazon Web Services (AWS) Cloud. Using Amazon EC2 eliminates your need to invest in hardware upfront, 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. Amazon EC2 enables you to scale up or down to handle changes in requirements or spikes in popularity, reducing your need to forecast traffic.

Why is AWS EC2 important?

  1. You don’t require any hardware units

  2. Easily scalable (up or down)

  3. You only pay for what you use

  4. You have complete control

  5. Highly secure

  6. You can access your assets from anywhere in the world

Key features of EC2:-

  • Have full Control :- If you want to have complete control over your instances then there is no better choice than this. In this, the root access is being provided which helps them to interact as they would do with any system. While they are recovering the data on their boot partition, this service gives them an option to stop the instance at any time.

  • Reliable and secure:- If you are worried that this is not going to be secured, then you are mistaken. The services provided by AWS are very secure and so is EC2 instance. They offer a highly reliable ecosystem that can be used for speed and predictability. As these are being run under the Amazon-proven network and data center, this is highly secure. When you are using the EC2 instance, you will work with the Amazon VPC and this will enhance the security for better networking and resourcing. Moreover, the users are given the choice to decide which instances will be revealed on the internet and which should remain private for them only.

  • Elastic Web-Scale Computing:- As the name suggests, this service is going to help the users to expand the capacity as per their needs within the minutes, which generally takes longer than that. Many servers can be run in parallel with these instances. These services give the user a choice to increase or decrease the automation as per their needs.

  • Cost Saving:- The services that you are going to get with the EC2 instance are inexpensive. The users can select the plan as per their requirements. This will help them to save costs that they had to pay unnecessarily. The user has to pay a very low amount as compared to other services out there.

  • Flexibility:- The user with the flexibility not only to choose the type of instances but they can also choose the software packages, the instance storage and operating system for them as well. Not only this, the configuration can be changed along with the configuration of the memory.

  • Easy to start:- The services that come with EC2 instance are very easy to start. You can simply go to the AWS Management console and from there you can choose the pre-configured package as well and deploy this software. This can be done in no time and you are all ready to use this platform.

Automation in EC2:-

AWS helps you use automation so you can build faster and more efficiently. Using AWS services, you can automate manual tasks or processes such as deployments, development & test workflows, container management, and configuration management

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.

  • It can be created via the AWS Management Console, CLI, or SDKs. Once created, they can be used for manual instance launches or integrated with other services like Auto Scaling or AWS CloudFormation for automated provisioning.

Instance Types:-

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.

  • General-Purpose Instances:- The computation, memory, and networking resources in general-purpose instances are balanced. Scenarios, where you can use General Purpose Instances, are gaming servers, small databases, personal projects, etc. Assume you have an application with a kind of equal computing, memory, and networking resource requirements. Because the program does not require optimization in any particular resource area, you can use a general-purpose instance to execute it . Ex:- t2. micro,m6a.2xlarge, m6a.4xlargem,5.12xlarge, and m5.24 large

  • Compute-Optimized Instances:- Compute-optimized instances are appropriate for applications that require a lot of computation and help from high-performance CPUs. You may employ compute-optimized instances for workloads including web, application, and gaming servers just like general-purpose instances. This instance type is best suited for high-performance applications like web servers, Gaming servers. Ex:- c5d.24large

  • Memory-Optimized Instances:- Memory-optimized instances are geared for workloads that need huge datasets to be processed in memory. Memory here defines RAM which allows us to do multiple tasks at a time. Data stored is used to perform the central processing unit (CPU) tasks it loads from storage to memory to run. This process of preloading gives the CPU direct access to the computer program. Assume you have a workload that necessitates the preloading of significant volumes of data prior to executing an application. A high-performance database or a task that requires real-time processing of a significant volume of unstructured data might be involved in this scenario. In this case, consider using a memory-optimized instance. It is used to run applications that require a lot of memory with high performance. Ex:- R7g.medium

  • Storage-Optimized Instances:- Storage-optimized instances are made for workloads that demand fast, sequential read and write access to huge datasets. Distributed file systems, data warehousing applications, and high-frequency online transaction processing (OLTP) systems are examples of workloads that are suited for storage-optimized instances. Storage-optimized instances are built to provide applications with the lowest latency while accessing the data.

    Ex:- ls4gn, l4i, D, and H.

  • Accelerated Computing Instances:- Coprocessors are used in accelerated computing instances to execute specific operations more effectively than software running on CPUs. Floating-point numeric computations, graphics processing, and data pattern matching are examples of these functions. A Hardware-Accelerator/ Co-processor is a component in computing that may speed up data processing. Graphics applications, game streaming, and application streaming are all good candidates for accelerated computing instances. Ex:- p3.2xlarge,p3.8xlarge and p3.16xlarge

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.

Amazon Machine Image (AMI) is a master image for the creation of virtual servers -- known as EC2 instances -- in the Amazon Web Services (AWS) environment. The machine images are like templates that are configured with an operating system and other software that determine the user's operating environment.

Task1:

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.

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

Step 1:- First we navigate to AWS and click on Launch Template -> create launch template

Step 2:- Then we need to give the launch template name

Now we need to select the instance type and key pair and Network Settings

Step 3:- After that click on Advance details-> User data and need to write the shell script where Jenkins and Docker installation steps will be mentioned and click on launch instance

#!/bin/bash
 sudo apt update
 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

Step 4:- Once we have created we can see the template is created

now using the template we can launch n number of EC2 machine

Step 5:- Now we need to click on Launch instance from template and we need to select the instance number and launch the template

and after sometimes we can see the server is up and running

Creating an Auto-Scaling group

  • If we need to scale the server in that case we need an autoscalling group. Let's go ahead and create the template for this

First, we need to create Auto Scaling Groups -> Create Auto Scaling group and then we need to give the name and select a template and click on Next

Now we need to select VPC and Availability Zone(AZ) based upon your requirement and click on Next

Now no required of the load balancer and VPC

We can select Desired capacity, Minimum capacity and Maximum capacity based upon needed or requirements of the particular project and click on Next

Then we need to review and click on Create Autoscaling group

Now if we go and check from EC2 machines we can see all machine is up and running

Now we can see if I deleted since minimum we select 2 So 2 EC2 machines will launch automatically

Note:- If we need to delete and then we need to delete auto-scaling groups

Devops#devops,#90daysofDevOps

Thank you for reading!! I hope you find this article helpful!!

if any queries or corrections to be done to this blog please let me know.

Happy Learning!!

Saikat Mukherjee

Did you find this article valuable?

Support Saikat Mukherjee's blog by becoming a sponsor. Any amount is appreciated!