Skip to content

Beginner · Compute and storage: EC2 and S3 · topic 10 of 50 · 35 min

EC2: virtual servers in the cloud

Explanation

Amazon EC2 (Elastic Compute Cloud) rents you virtual servers, called instances, by the second. You pick an image (AMI) for the operating system, an instance type for the CPU and memory, a key pair or Session Manager to log in, and a security group as the firewall. You pay while the instance runs, so stop or terminate what you do not need.

Why rent a virtual server?

Buying a physical server means paying lakhs of rupees up front, waiting weeks for delivery, and guessing how big it must be. EC2 lets you start a server in about a minute, use it for an hour or a year, and throw it away when you are done. An instance is one virtual server: a slice of a big physical machine in an AWS data centre, with its own CPU, memory, disk and network card.

Think of it like this: EC2 is like booking an Ola or Uber instead of buying a car. You choose the size (mini, sedan, SUV), use it for your trip, pay for the time you used, and walk away. You never worry about servicing the engine.

AMIs: the starting disk image

An AMI (Amazon Machine Image) is a template for the instance's first disk: the operating system plus any software already installed. AWS publishes AMIs for Amazon Linux 2023, Ubuntu, Windows Server and others. You can also make your own AMI from a configured instance, so every new server starts with your app already on it. AMI IDs are different in each region, so the same Ubuntu image has one ID in Mumbai (ap-south-1) and another in Singapore.

Instance types and how to read the name

The instance type decides how much CPU, memory and network you get. The name follows a pattern: family letter(s), generation number, optional attribute letters, a dot, then the size. In m7g.large, m is the family (general purpose), 7 is the generation (newer is usually faster and cheaper per unit of work), g is an attribute (AWS Graviton, an ARM processor) and large is the size. Each size step up (large, xlarge, 2xlarge, 4xlarge) roughly doubles CPU and memory, and the price.

FamilyOptimised forTypical use
t (t3, t4g)General purpose, burstableSmall websites, dev and test servers
m (m7i, m7g)General purpose, balancedApp servers, small databases
c (c7g, c7i)Compute (more CPU per GB of RAM)Video encoding, batch jobs, game servers
r (r7g, r7i)Memory (more RAM per CPU)In-memory caches, large databases
i (i4i)Storage (fast local NVMe disks)Search engines, NoSQL databases
g and p (g5, p5)Accelerated computing (GPUs)Graphics, machine learning inference and training

Common attribute letters: g means Graviton (ARM), a means AMD, i means Intel, d means local NVMe instance storage, n means extra network bandwidth. Graviton types are often around 20 percent cheaper for the same work, but your software must run on ARM (most Linux software, Node.js, Python and Java do).

Burstable t types have a low baseline CPU level and earn CPU credits while idle. When traffic rises, they spend credits to run faster for a while. t3 and t4g start in unlimited mode by default, which means they can keep bursting after credits run out, but you pay a small extra charge for it. A t instance that is busy all day is usually cheaper as an m instance.

Connecting to an instance

  • SSH with a key pair: AWS keeps the public key and you download the private key once as a .pem file. Run chmod 400 on it, because SSH refuses keys that other users can read.
  • EC2 Instance Connect: the console or CLI pushes a short-lived public key to the instance for about 60 seconds, so you do not manage .pem files. Port 22 must still be reachable.
  • Session Manager (part of AWS Systems Manager): opens a shell through the SSM agent, with no inbound port open at all. The instance needs an IAM role with the AmazonSSMManagedInstanceCore policy. Sessions can be logged, which auditors like.
bash
chmod 400 asha-key.pem
ssh -i asha-key.pem ec2-user@203.0.113.10      # Amazon Linux user is ec2-user, Ubuntu uses ubuntu
aws ssm start-session --target i-0abc1234def567890   # Session Manager: no SSH port needed
Two ways in. Session Manager needs the Session Manager plugin for the AWS CLI on your laptop.

Security groups: the instance firewall

A security group is a set of allow rules attached to an instance's network card. There are no deny rules: anything not allowed is blocked. Security groups are stateful, which means if a request is allowed in, the reply is allowed out automatically. A new security group allows all outbound traffic and no inbound traffic. Allow SSH (port 22) only from your own IP address, written as a /32, never from 0.0.0.0/0 (the whole internet).

User data: set up the server at first boot

User data is a script you pass at launch. By default it runs once, as root, on the first boot. It is the simplest way to install software automatically, so you never set up servers by hand.

bash
# setup.sh, passed as user data
#!/bin/bash
dnf update -y
dnf install -y nginx
echo "<h1>Hello from $(hostname -f)</h1>" > /usr/share/nginx/html/index.html
systemctl enable --now nginx

# Launch one instance with that script
aws ec2 run-instances \
  --image-id ami-0abcdef1234567890 \
  --instance-type t3.micro \
  --key-name asha-key \
  --security-group-ids sg-0123456789abcdef0 \
  --subnet-id subnet-0123456789abcdef0 \
  --user-data file://setup.sh \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=web-1}]' \
  --count 1
The AMI, security group and subnet IDs are examples. Look up the current AMI ID for your region in the console.

Lifecycle and billing

An instance moves through states: pending (starting), running, stopping, stopped, shutting-down and terminated. Stop is like switching a laptop off: the root EBS disk is kept and you can start it again, but anything on instance store disks is lost and the automatic public IP usually changes. Terminate deletes the instance for good, and by default its root volume too.

Linux instances are billed per second, with a minimum of 60 seconds each time they start. Some operating systems and Marketplace images are billed per hour, so check the pricing page. A stopped instance has no compute charge, but you still pay for its EBS volumes. Since February 2024 every public IPv4 address is charged by the hour, about 0.005 USD per hour at the time of writing, whether it is an auto-assigned public IP or an Elastic IP (a fixed public address you keep until you release it). That is small for one server and large for a hundred forgotten ones.

Careful: Common mistakes: (1) Opening SSH to 0.0.0.0/0; fix it by allowing only your IP or using Session Manager. (2) Losing the .pem file; AWS cannot give it back, so store it safely or use Session Manager. (3) "Unprotected private key file" error; run chmod 400 on the key. (4) Leaving test instances and Elastic IPs running; set a budget alert and tag everything with an owner. (5) Picking a t instance for a CPU-heavy job and wondering why it slows down after an hour; check CPU credits in CloudWatch.
Tip: Choose the region closest to your users. For users in India that is usually Mumbai (ap-south-1) or Hyderabad (ap-south-2). Prices differ slightly between regions.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

EC2: virtual servers in the cloud · 1/8

EC2 = virtual servers Pay while running

EC2 rents you virtual servers called instances. You start one in a minute and pay only while it runs.

Prefer a video by a teacher? Find free videos on “aws ec2 tutorial for beginners launch instance ssh security group” on YouTube ↗

In real life: A coaching institute in Kota runs mock tests

An exam coaching institute runs online mock tests every Sunday morning for 20,000 students. On weekdays one t3.small instance serves the website. On Sunday at 7 am a script launches six c7g.large instances from the institute's own AMI (the test app is already installed), and terminates them at noon. They pay for five hours of the big servers instead of a whole week, and the user data script registers each new server with the load balancer automatically.

bash
aws ec2 run-instances --image-id ami-0abcdef1234567890 \
  --instance-type c7g.large --count 6 \
  --security-group-ids sg-0123456789abcdef0 \
  --iam-instance-profile Name=mocktest-app-role
# at noon
aws ec2 terminate-instances --instance-ids i-0aaa1111 i-0bbb2222
Launching for the busy hours and terminating afterwards. IDs are examples.

Practice (0/4 done)

Task 1 · Code · Python

Write monthly_cost(hourly_price, count, hours=730, public_ipv4=False) that returns the monthly cost in USD of running count instances, rounded to 2 decimal places. 730 is the usual number of hours in a month. If public_ipv4 is True, each instance also has one public IPv4 address that costs 0.005 USD per hour. Example: monthly_cost(0.0112, 2) returns 16.35.

Task 2 · Code · Python

Write parse_instance_type(name) that splits an EC2 instance type into its parts and returns a dict with keys "family" (letters), "generation" (an int), "attributes" (letters after the number, may be "") and "size" (text after the dot). Return None if the name does not match the pattern. Example: "m7g.large" gives {"family": "m", "generation": 7, "attributes": "g", "size": "large"}.

Task 3 · Question

Your company wants staff to open a shell on private EC2 instances with no inbound port open and every session logged. What do you use?

Task 4 · Practice

Lab (Free Tier or a small budget): launch a t3.micro Amazon Linux 2023 instance in your nearest region with a user data script that installs nginx. Allow HTTP from anywhere and SSH only from your IP. Open the public IP in a browser, connect once with SSH or Session Manager, then terminate the instance and confirm no Elastic IP is left allocated.

Quick quiz

Question 1

1.In the instance type r7g.xlarge, what does the g mean?

Question 2

2.Which family would you pick for a large in-memory cache?

Question 3

3.What is true about security groups?

Question 4

4.You stop (not terminate) an EBS-backed instance. What happens?

Question 5

5.An SSH client says "UNPROTECTED PRIVATE KEY FILE". What is the fix?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

In the instance type r7g.xlarge, what does the g mean?

Answer: AWS Graviton (ARM) processor

The attribute g means a Graviton ARM processor. GPU instances are the g and p families, where the letter comes first.

Which family would you pick for a large in-memory cache?

Answer: r

r is memory optimised: more RAM per vCPU.

What is true about security groups?

Answer: They are stateful and contain allow rules only

Security groups only allow, and return traffic for an allowed connection is allowed automatically.

You stop (not terminate) an EBS-backed instance. What happens?

Answer: The root volume is kept and you still pay for it

Stopping ends the compute charge but the EBS volumes remain, and storage is still billed.

An SSH client says "UNPROTECTED PRIVATE KEY FILE". What is the fix?

Answer: chmod 400 on the .pem file

SSH refuses private keys that other users can read. chmod 400 makes the key readable only by you.

Which storage loses its data when the instance is stopped?Related

Answer: Instance store

Instance store disks are physically attached to the host and are wiped on stop or terminate.

From Block and file storage: EBS, instance store and EFS

When would the EC2 launch type make more sense than Fargate?Related

Answer: For a steady, heavy workload that needs GPUs or special instance types

The EC2 launch type gives you control of instance types such as GPUs, and can be cheaper for steady, tightly packed load.

From Amazon ECS and AWS Fargate

After a VPC lab you terminated the EC2 instances. Which leftover is most likely still charging by the hour?Related

Answer: A NAT gateway

NAT gateways charge per hour plus per GB processed. VPCs, security groups and route tables have no hourly charge.

From Cost control in practice

You stopped an EC2 instance. What can still cost money?Related

Answer: Its attached disk storage

Stopped instances do not pay for compute, but their EBS volumes are still billed.

From Pricing, billing and cost control

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 3 more topics in “Compute and storage: EC2 and S3” before its exam: Block and file storage: EBS, instance store and EFS, Amazon S3: buckets, objects and storage classes, Securing and sharing S3 data.
  2. Module 3 exam: Compute and storage, EC2 and S3 · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Networking and databases: VPC and RDS. 50 topics left in the course before the final exam.

← Previous: Pricing, billing and cost control