AWS EC2 Template & Auto-Scaling
What is AWS EC2?
AWS EC2 (Amazon Elastic Compute Cloud) is a web service provided by Amazon Web Services (AWS) that allows you to rent virtual servers, also known as instances, on-demand. These instances provide resizable compute capacity in the cloud, meaning you can easily scale up or down based on your computing needs. EC2 instances are commonly used for various purposes, such as hosting websites, running applications, performing data analysis, and more. They provide you with control over the operating system, networking, storage, and other aspects of the virtual server, offering a flexible and cost-effective solution for deploying and managing your applications in the cloud.
Features of Amazon EC2:
Amazon EC2 (Elastic Compute Cloud) offers a range of features that make it a powerful and flexible cloud computing service. Here are some key features of Amazon EC2:
Virtual Instances: EC2 allows you to create and launch virtual servers, known as instances, with a variety of configurations, operating systems, and instance types to suit your specific needs.
Elasticity: You can easily scale the number of instances up or down based on demand. This elasticity ensures that you have the right amount of computing resources available when needed without overprovisioning.
Instance Types: EC2 offers a diverse range of instance types optimized for different use cases, such as general-purpose, compute-optimized, memory-optimized, storage-optimized, GPU instances, and more.
Customizable: You have full control over the operating system, networking, security settings, and software installed on your instances. This allows you to create a tailored environment for your applications.
Security: EC2 provides features like security groups and network access control lists (ACLs) to control inbound and outbound traffic, and you can also use key pairs for secure remote access.
Storage Options: EC2 offers various storage options, including instance store (ephemeral storage) and Amazon EBS (Elastic Block Store) for persistent and scalable block storage.
Load Balancing: Amazon EC2 supports load balancing through Elastic Load Balancing (ELB), distributing incoming traffic across multiple instances to improve availability and fault tolerance.
Auto Scaling: You can set up Auto Scaling to automatically adjust the number of instances based on predefined conditions, ensuring your application's performance remains consistent during traffic spikes.
Monitoring and Logging: EC2 instances can be monitored using Amazon CloudWatch, providing insights into performance metrics, utilization, and resource consumption. You can also set up logs for troubleshooting and analysis.
Networking: EC2 instances can be launched in a Virtual Private Cloud (VPC), allowing you to isolate and control network resources. You can configure subnets, route tables, and network gateways for enhanced network security and management.
Global Reach: Amazon EC2 has multiple regions and availability zones around the world, allowing you to deploy your applications closer to your users for reduced latency and improved performance.
Pay-as-You-Go Pricing: EC2 follows a pay-as-you-go pricing model, meaning you are billed only for the resources you use. This cost-effective approach helps optimize your budget and infrastructure spending.
AWS EC2 Pricing:
Instance | CPU | Memory (GiB) | Storage (GB) | Linux On-Demand cost |
t2.micro | 1 | 1 | EBS only | $0.0116 per Hour |
t2.small | 1 | 2 | EBS only | $0.023 per Hour |
t2.medium | 2 | 4 | EBS only | $0.0464 per Hour |
t2.large | 2 | 8 | EBS only | $0.0928 per Hour |
t2.xlarge | 4 | 16 | EBS only | $0.1856 per Hour |
t2.2xlarge | 8 | 32 | EBS only | $0.3712 per Hour |
m5.large | 2 | 8 | EBS only | $0.096 per Hour |
m5.xlarge | 4 | 16 | EBS only | $0.192 per Hour |
m5.2xlarge | 8 | 32 | EBS only | $0.384 per Hour |
Instance type: The type of instance to launch. An instance type defines the hardware of the host computer used for your instance. Each instance type provides different computing and memory capabilities. You can select an instance type based on the amount of memory and computing power that you need for the application or software that you plan to run on your instance.
AWS EC2 Instance Template:
You can create a launch template that contains the
configuration information
to launch an instance. You can use launch templates to storelaunch parameters
so that you do not have to specify them every time you launch an instance.For example, a launch template can contain the AMI ID, instance type, and network settings that you typically use to launch instances. When you launch an instance using the Amazon EC2 console, an AWS SDK, or a command line tool, you can specify the launch template to use.
Amazon Machine Image (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.
AWS Auto Scaling:
- Amazon EC2 Auto Scaling helps you maintain
application availability
and lets you automaticallyadd
orremove EC2 instances
usingscaling policies
that you define. Dynamic or predictive scaling policies let you add or remove EC2 instance capacity to service established or real-time demand patterns. Thefleet management
features ofAmazon EC2 Auto Scaling
help maintain the health and *availability of your fleet.
Task 1:
Create a launch template with Amazon Linux 2 AMI and t2.micro instance type with Jenkins and Docker setup using user data script.
- Step 1: Go to the EC2 service and click on Launch Templates under INSTANCES and hit Create launch template.
- Step 2: Select Application and OS as Amazon Linux 2 and t2.micro as instance type.
- Step 3: Select Create a new key pair and give a name to it and download it and Network type as VPC and Subnet as Public Subnet.
- Step 4: Click on Advanced details and paste the below script in User data and click on Create launch template.
#!/bin/bash
sudo apt update
sudo apt install openjdk-8-jdk
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 update
sudo apt install jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
- Step 5: Go to the EC2 service and click on Auto Scaling Groups under Auto Scaling and hit Create Auto Scaling group.
Create 3 Instances using Launch Template, there must be an option that shows the number of instances to be launched using Auto Scaling Group.
- Step 1: Go to the EC2 service and click on Auto Scaling Groups under Auto Scaling and hit Create Auto Scaling group.
- Step 2: Select Network as VPC and Subnet as Public Subnet and click on Next.
- Step 3: Here we are not using Load Balancer so click on Next.
- Step 4: Configure group size and scaling policies and click on Next and skip notifications and tags we have created the Auto Scaling group successfully.
- Step 5: Go to the EC2 service and click on Auto Scaling Groups under Auto Scaling and click on Auto Scaling group and click on Edit and change the Desired capacity to 3 and click on Update.
- Step 6: So After that, we delete the Auto Scaling group and it will delete all the instances.
Subscribe to my newsletter
Read articles from Mizan Firdausi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by