Technology and services · 15 min
Amazon EC2, Auto Scaling and Elastic Load Balancing
In this lesson you will learn what an Amazon EC2 instance is, how to choose its type, in which states it is billed and how Amazon EC2 Auto Scaling and Elastic Load Balancing keep an application running when demand changes.
Amazon EC2 (Elastic Compute Cloud) is the AWS service that rents you virtual machines, so you can run a program on a server without having to buy it. For example, an online store can rent on EC2 the computer its website runs on, instead of buying one and keeping it in an office. EC2 is combined with two services that work with it: Amazon EC2 Auto Scaling, which adjusts how many machines are running, and Elastic Load Balancing, which spreads the incoming visits among them.
What is a virtual machine?
A physical server has CPU, which is the processor that does the calculations, memory, where programs keep what they are using at that moment, disk, where files are stored, and network, which is its connection to other computers. A program called a hypervisor splits that hardware into pieces, and each piece works like a separate computer, with its own operating system, which is the base program that runs the computer, such as Windows or Linux. Each of those pieces is called a virtual machine. For example, the same server can have one virtual machine with Linux and another with Windows, and the hypervisor makes sure none of them sees what is in the others.

Thanks to virtual machines, AWS can rent each customer just the piece of a server it needs. For example, the online store rents a small virtual machine and another customer rents a bigger one on the same physical server, without either of them seeing the other's data.
Amazon EC2
AWS defines EC2 as secure, resizable compute capacity in the cloud. Compute capacity means processing power, that is, the machines your programs run on. Each EC2 virtual machine is called an instance, and you launch it, that is, create it and start it, in minutes, and pay for the capacity you use. For example, if the store needs a machine to test a new version of its website, it launches an instance in the morning, uses it for a few hours and deletes it in the afternoon.
When you launch an instance you make four decisions:
- The AMI (Amazon Machine Image), which is the template with the operating system and the programs already installed, from which you can launch one instance or a hundred identical ones, and which lives in a Region although it can be copied to another. For example, the store can save its already configured web server in an AMI and launch identical copies when it needs them.
- The instance type, which sets how much CPU, memory and network the instance has. For example, a website with few visits fits in a small type, and one with many visits needs a bigger one.
- The network, which defines which VPC and which subnet the instance is in and which security group filters the traffic, that is, the connections that reach it and the ones that leave it.
- The storage, which is the instance's disks, where the operating system and the files are kept.
Instance type families

Instance types are grouped into families based on the resource the application uses the most, and each family is recognized by one or more letters. These are the families you need to recognize:
- General purpose (M, T), which is useful when no resource dominates, as in a web server. For example, the store's website, which uses a little CPU, a little memory and a little disk.
- Compute optimized (C), which is useful for batch processing, that is, large jobs done all at once, for video or for game servers. For example, the store can use one of these to convert all its product videos every night.
- Memory optimized (R, X), which is useful for large datasets that are processed in memory. For example, an analysis that needs millions of sales loaded at the same time.
- Storage optimized (I, D), which handles a lot of reading and writing on local disk, that is, on the disk inside the same server.
- Accelerated computing (P, G, Inf, Trn), which comes with GPUs or chips for machine learning.
To choose the family, ask yourself which resource your application runs out of first. For example, if the store runs out of memory before CPU, it chooses a memory optimized one, and if it wants to train a model that needs GPUs, it chooses an accelerated computing one.
Which family do you choose?
0 of 5Tap the correct column for each workload.
A web server that doesn't run out of any resource first
Servers for an online game
A large dataset that is processed in memory
Training a machine learning model that needs GPUs
Encoding video in batches
Within general purpose, the T family works with bursts, that is, with short rises in work. A T instance has a baseline level of CPU, accumulates credits while it works lightly and spends them when the load goes up, so it is useful for quiet applications that have a peak now and then. For example, a school's website is barely used during the week, but on the day grades are posted everyone logs in at the same time, and the T instance uses the credits it saved during that stretch.
When is an instance billed?

An instance goes through several states, which are the situations it can be in, and each state is billed differently:
- The running state, which is billed even if nobody is using the instance, at a price that depends on how you buy the capacity (On-Demand, Savings Plans or Spot, among other options). For example, if the store leaves a test instance that nobody uses running all night, it pays for those hours anyway.
- The stopped state, where the instance is off and its usage stops being billed, but the attached EBS volumes are still billed, and where you can change its type. For example, the store can stop its test instance on Friday and start it again on Monday with its files intact.
- The terminated state, where the instance is deleted and you can no longer recover it. For example, if the store terminates its test instance by mistake, it has to launch a new one from the AMI.
cloud path tip: when you finish a lab, stopping the instance is not enough to stop paying, because its EBS volumes are still billed. If you no longer need it, terminate it.
True or false?
0 of 4If an instance is running but nobody uses it, it isn't billed.
If you stop an instance, you still pay for the attached EBS volumes.
A terminated instance can be started again.
You can change an instance's type while it is stopped.
Amazon EC2 Auto Scaling
An online store doesn't know whether in December it will need 4 instances or 20, and Amazon EC2 Auto Scaling saves it from having to guess, because it adds or removes instances based on demand, that is, on how many people are using the application at that moment. To use it, you put the instances together in an Auto Scaling group and give it three numbers: the minimum, the desired capacity and the maximum.

For example, a group with minimum 4, desired 6 and maximum 12 keeps 6 instances with the current demand, and never goes below 4 or above 12. When demand goes up, as in a year-end sale, the group launches instances up to the maximum, and when it goes down, as in the early morning, it terminates the extra ones without going below the minimum. That ability to grow and shrink with demand is called elasticity, and thanks to it the store doesn't pay for extra instances or fall short when many customers arrive.
How many instances are left?
Your Auto Scaling group has minimum 4, desired 6 and maximum 12. In the early morning demand drops almost to zero. How many instances does the group keep?
Besides adjusting the number, Auto Scaling checks the health of each instance, that is, whether it is still responding properly, and replaces the one that fails. It also distributes the instances across Availability Zones, so that a failure in a single place doesn't take them all down. That is why even a group with minimum 1 makes sense. For example, if the only instance of the school's website goes down, Auto Scaling launches another one without anyone doing it by hand.
Elastic Load Balancing
When there are six instances running, users need a single address to connect to, and that address is provided by an Elastic Load Balancing load balancer. A load balancer receives all the visits and spreads them across several targets, which can be instances, containers or IP addresses, in one or several zones. For example, when a thousand customers enter the store at the same time, the load balancer sends a share to each instance so none of them gets overloaded.
The load balancer also checks the health of each target with a health check, which is a test to see whether the target responds, and only sends traffic to the healthy ones. When an instance fails that check, the load balancer stops sending it traffic and Auto Scaling launches another one in its place, so the two services work together. For example, if one of the store's six instances freezes, customers keep shopping on the other five while Auto Scaling launches the missing one.

Types of load balancer

Elastic Load Balancing has three current-generation load balancer types, in addition to the Classic Load Balancer, which is from the previous generation and from which AWS recommends migrating to one of the three. Each type works at a different layer, and layers are the levels a network communication is divided into: at layer 7 you see the full request, with the web address that was requested, and at layers 4 and 3 you only see the connections and the data packets with their IP addresses. The three types are these:
- The Application Load Balancer, which works at layer 7 (HTTP and HTTPS, the language used to request web pages) and reads each request, so it can send it to different groups of instances based on the path or the domain. For example, the store sends what arrives at /api to some instances and what arrives at /imagenes to others.
- The Network Load Balancer, which works at layer 4 (TCP and UDP, the basic rules data travels by on the network), handles millions of requests per second and provides a static IP per zone, that is, an address that doesn't change. For example, an online game that receives a huge number of connections at the same time can use it.
- The Gateway Load Balancer, which works at layer 3 and places virtual appliances in the path of the traffic, which are security programs from other companies, such as firewalls, that check the traffic and block what is dangerous. For example, a bank uses it so that everything reaching its instances first goes through the firewall it chose.
Which load balancer do you use?
Your web application has to send requests to /api to one group of instances and requests to /imagenes to another. Which load balancer do you use?
On the exam
Questions on this topic describe a problem and ask you for the service that solves it. Some phrases in the question point to each service:
- "Full control of the operating system" or "software that needs a server", which point to EC2.
- "I don't know how much capacity I will need" or "adjust to demand", which point to Auto Scaling.
- "Distribute the traffic" or "a single entry point", which point to Elastic Load Balancing.
When the question asks for the type of load balancer, the technical detail in the question decides the answer. URL paths or HTTP traffic lead to the Application Load Balancer, a static IP or extreme performance lead to the Network Load Balancer, and third-party firewalls lead to the Gateway Load Balancer. For example, if the question says that a store doesn't know how many customers it will have in its year-end sale, the answer is Auto Scaling, and if it says that it needs to send /api and /imagenes to different instances, it is the Application Load Balancer.
cloud path tip: if you use an account with the new AWS experience, EC2, Auto Scaling and Elastic Load Balancing are included in the free plan, so you can practice with all three.
Review question
An application runs on EC2 instances in two Availability Zones. The team wants another instance to be launched if one fails, without anyone having to do it by hand. Which service does that?
Next
- Compute (AWS Whitepapers)
- cloud path practice exams