EC2 basics
What an EC2 instance is, the choices made at launch, the instance life cycle, and bootstrapping with user data.
What it is
Amazon EC2 (Elastic Compute Cloud) rents out virtual servers, called instances, in the AWS Cloud. It's AWS's main Infrastructure as a Service offering: AWS runs the physical hardware and the virtualisation layer, and the customer gets a machine to log in to, install software on and run as needed.
EC2 exists because buying servers is slow and risky. A physical server takes weeks to arrive and costs the same whether it's busy or not. An EC2 instance starts in about a minute, is billed by the second, and can be thrown away when the work is done.
EC2 is also the base of a whole family of features that later groups cover:
- Virtual disks with Amazon EBS.
- Spreading traffic across instances with Elastic Load Balancing.
- Adding and removing instances automatically with Auto Scaling groups.
What gets chosen at instance launch
- AMI (Amazon Machine Image): the template with the operating system and any pre-installed software. Linux, Windows and macOS are available. See AMIs.
- Instance type: how much CPU (vCPUs), memory and network performance the instance gets, for example
t3.microorm7g.2xlarge. See Instance types. - Storage: network-attached volumes (EBS, EFS) or the server's own local disks (instance store).
- Network settings: which VPC and subnet the instance goes in, and whether it gets a public IP address. See IP addressing.
- Security group: the firewall rules for the instance. See Security groups.
- Key pair: the SSH key used to log in to Linux instances (or to decrypt the Windows Administrator password). See Connecting to EC2.
- User data: an optional script that runs when the instance first starts.
An IAM role can also be attached, so code on the instance can call AWS services without access keys.
User data: bootstrapping an instance
Bootstrapping means running commands automatically when a machine starts, so it comes up ready to work. On EC2 this is done with a user data script.
- By default it runs only once, at the instance's first start.
- It runs as the root user, so it can install packages and change system settings without
sudo. - It's limited to 16 KB.
- Typical jobs: install updates and software, download files, write configuration, start a service.
This script turns a fresh Amazon Linux 2023 instance into a small web server:
#!/bin/bash
dnf install -y httpd
echo "<h1>Hello from $(hostname -f)</h1>" > /var/www/html/index.html
systemctl enable --now httpdUser data is good for settings that change per launch. For software that takes a long time to install, a pre-built golden AMI starts faster. Many teams combine the two.
The instance life cycle
What gets billed depends on the instance's state:
- Pending → running: the instance boots. While it's running it is billed per second, with a 60-second minimum each time it starts (Linux, Windows and most other systems; a few commercial OS images bill per hour).
- Stop: the instance shuts down. The instance isn't billed while it's stopped, but its EBS volumes still are. Its public IP address is released. Starting it again usually moves it to new hardware.
- Reboot: like restarting a PC. It stays on the same host, keeps its IP addresses, and doesn't start a new 60-second minimum.
- Terminate: the instance is deleted. By default its root EBS volume is deleted too (the Delete on termination setting). This can't be undone.
- Hibernate: a stop that also saves the memory contents. See EC2 Hibernate.
Pricing
An instance's bill has several parts:
- The instance, by the second, at the rate for its type, Region and OS. A
t3.microrunning Linux in us-east-1 costs about $0.0104 an hour. - EBS storage, per GB-month, even while the instance is stopped.
- Public IPv4 addresses, $0.005 an hour each, whether the instance uses them or not.
- Data transfer out to the internet and between AZs or Regions.
How the instance is bought can cut the first part by up to 90%. See Purchasing options.
Updated since the slides
The course launches t2.micro instances under the old 12-month free tier. AWS changed the free tier for accounts created from July 2025: new accounts get credits for 6 months, usable on types such as t3.micro and t4g.micro. t2 is a previous-generation family, so these notes use t3.micro. AWS has also charged for every public IPv4 address since February 2024.
Real-world examples
- A small business website runs on one
t3.smallinstance with a user data script that installs WordPress. - A game studio launches 200 instances for a weekend tournament and terminates them on Monday, paying only for those hours.
- A bank runs a vendor's risk engine on EC2 because the vendor only supports one specific Linux version.
Exam tips
- "Bootstrap instances at launch", "install software when the instance starts" → user data.
- User data runs once, as root, at first boot by default.
- "Pay only while running" → stop instances that aren't needed; storage is still billed.
- Terminating deletes the root EBS volume by default (the DeleteOnTermination setting); extra volumes are kept by default.
Common traps
- "Put the database password in the user data." User data isn't encrypted and can be read from inside the instance and by anyone allowed to view it. Use Secrets Manager or Parameter Store.
- "User data runs on every reboot." Only on the first start, unless configured otherwise.
- "A stopped instance costs nothing." Its EBS volumes and any Elastic IPs are still billed.
Hands-on lab: launch a web server with user data
Goal: launch a Linux instance that installs and starts a web server by itself, then see what stop and start do to it.
Costs money
A t3.micro instance, its public IPv4 address and an 8 GB gp3 volume cost about $0.02 an hour together. Terminate the instance when you finish (or after the next labs, which reuse it).
Start the launch wizard
Open the EC2 console in us-east-1 and choose Launch instances. Name the instance saa-web, then choose Add additional tags and add Project = saa-lab.
Choose the AMI and type
Keep Amazon Linux 2023 (the default AMI). For Instance type, choose t3.micro.
Create a key pair
Under Key pair, choose Create new key pair: name saa-lab-key, type RSA, format .pem (or .ppk if you'll use PuTTY). Your browser downloads it. Keep it: the next labs use it.
Network and firewall
Under Network settings, keep the default VPC and Auto-assign public IP: Enable. Choose Create security group, name it saa-web-sg, tick Allow SSH traffic from: My IP, and tick Allow HTTP traffic from the internet.
Add the user data
Keep the default 8 GiB gp3 storage. Open Advanced details, scroll to User data, and paste the web server script from the user data section above. Choose Launch instance.
Open the website
In Instances, wait until the state is Running and the status checks show 2/2 checks passed. Copy the Public IPv4 address and open http:// followed by it in your browser (use http, not https). You'll see Hello from ip-172-31-….
Stop and start
Select the instance, choose Instance state → Stop, and watch the public IP disappear. Then Start it. It gets a new public IP. Open the new address: the page still works, because systemctl enable made the web server start on every boot, even though the user data didn't run again.
Expected result: a web page served from your instance without logging in to it, and proof that stopping releases the public IP while the instance and its disk survive.
Clean up: if you're doing the Security groups and Connecting to EC2 labs today, keep saa-web. Otherwise choose Instance state → Terminate (delete) instance. Keep the key pair file; key pairs are free.
Beyond the exam: instance metadata
Every instance can query information about itself at http://169.254.169.254/latest/meta-data/: its instance ID, AMI, IP addresses, IAM role credentials and its user data. This is the instance metadata service (IMDS). Version 2 (IMDSv2) requires a session token first, which blocks a class of attacks where a web app is tricked into fetching that URL for an attacker:
TOKEN=$(curl -s -X PUT http://169.254.169.254/latest/api/token -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-idNew instances launched from current AMIs require IMDSv2 by default. You can enforce it account-wide.