AWS from zero · Part 2: Networking and compute · Lesson 10 of 38 · about 45 minutes

EC2: running a server

The bakery's web server on EC2, launched the way production does it: an image looked up by name, a launch script, IMDSv2 required, and no SSH at all. What an instance type name means, what the server really costs a month (the public address is a third of it), and what happens when you stop it, make it bigger and start it again: a new address, the same disk, and a page that knows it moved.

By the end you'll know

  • What an EC2 (Elastic Compute Cloud) instance is made of: a type, an image, a disk and a network
  • How to read an instance type name like t4g.micro
  • How to look up the current image by name, instead of copying an ID that goes stale
  • Why to require IMDSv2 (Instance Metadata Service version 2), and why this server has no SSH
  • What a server really costs a month, broken into its parts
  • What stop, start and terminate each do to the address, the disk and the bill
  • How to change a server's size

Before you start

  • You need: the juniper-rye profile from Lesson 4, signed in, and Lessons 8 and 9: a VPC, a subnet and a security group.
  • The script: ec2-lab.sh from the practice folder's lesson-10. Everything it creates is named jr-lesson10-* and tagged lesson=10, and its delete step removes it.
  • How the script talks to AWS: only through the AWS CLI. The few lines of Python in it just turn the CLI's JSON into readable tables and never connect to AWS; boto3, AWS's Python library, comes in Lesson 20.
  • Where you run things: your own machine, labeled (Your machine).
  • Every output below is from a real run on November 10, 2026.

This lab runs a server, which bills by the second

The server, its disk and its public address cost about $0.0143 an hour together, and double that for the few minutes it runs at the bigger size. The lab takes about 10 minutes: a few cents from credits. A stopped server still bills for its disk. Run delete the same day, and check every line shows 0.

The short version

  • An instance is four choices: a type (how big), an image (what's installed), a disk and a network.
  • t4g.micro reads as: family t (burstable), generation 4, g for Graviton (AWS's Arm processor), size micro.
  • Look images up by name through a public parameter, so you always get the current one.
  • Require IMDSv2, and leave out SSH if nothing needs to log in.
  • The bakery's server is $10.42 a month: $6.13 for the server, $0.64 for the disk, and $3.65 for the public IPv4 address.
  • Stopping keeps the disk and the private address, loses the public address, and still bills for the disk. Terminating deletes the lot.

What this lesson costs

PartCost
Sizes, images and prices$0
The VPC, subnet and security group$0
A t4g.micro for a few minutes, then a t4g.small$0.0084, then $0.0168 an hour
Its 8 GB disk and public IPv4 addressAbout $0.0009 and $0.005 an hour
This lessonA few cents, from credits
If the server were left running$10.42 a month as a t4g.micro

What a server is made of

In Lesson 1's places to live, EC2 is the apartment: you rent the space and choose everything in it. Lessons 7 to 9 gave the bakery a furnished flat, then a building and its doors. Now the apartment itself. An EC2 instance is a virtual server, and launching one means four choices:

ChoiceWhat it decidesThe bakery's
Instance typeHow many CPUs, how much memory, which processort4g.micro, then t4g.small
Image, an AMI (Amazon Machine Image)The operating system and anything installed on itAmazon Linux 2023 for Arm
Disk, an EBS (Elastic Block Store) volumeWhere the operating system and files live8 GB of gp3
NetworkWhich subnet, which security group, whether it gets a public addressA public subnet, port 80 from your IP

Sizes, images and prices

In the console: EC2 > Instance types lists every size, with a Free tier eligible filter. EC2 > AMIs > Public images, searching for al2023, lists the images.

(Your machine)

BASH
cd ~/Apps/aws-practice/lesson-10
chmod +x ec2-lab.sh
./ec2-lab.sh fence
OUTPUT
== Sizes this account's plan can launch, Arm only, with prices in us-east-2
  t4g.micro  2 CPUs   1.0 GB memory  network Up to 5 Gigabit $0.0084 an hour, $  6.13 a month
  t4g.small  2 CPUs   2.0 GB memory  network Up to 5 Gigabit $0.0168 an hour, $ 12.26 a month
== The image: the newest Amazon Linux 2023 for Arm, looked up by name
  ami-0f119f573d13441ab
  al2023-ami-2023.12.20261109.0-kernel-6.18-arm64
  published 2026-11-04
== Its disk, gp3
  $0.08 per GB a month, so the default 8 GB is $0.64 a month

Reading an instance type

Every instance type name has the same parts. Take t4g.micro:

PartMeans
tThe family: t is burstable, cheap and fine when it's mostly idle
4The generation. Newer is usually faster for the same price.
gGraviton, AWS's own Arm processor. No letter usually means Intel; a means AMD; i also means Intel.
.microThe size. Each step up, nano, micro, small, medium, large, xlarge, roughly doubles the memory and the price.

The families you'll meet most:

FamilyBuilt forExample use
t, burstableMostly idle, with short busy spells. Builds up credits while quiet, spends them when busy.A small shop's site, a test server
m, general purposeA balance of CPU and memory, all dayApp servers
c, compute optimizedMore CPU per GB of memoryBusy APIs, video encoding
r, memory optimizedMore memory per CPUDatabases, caches
g and p, acceleratedGPUs (graphics processing units)Machine learning, graphics

Lesson 9 found that the free plan only launches some types; for Arm, that's the t4g.micro and t4g.small above. Graviton is the reason the lab uses Arm: a t4g costs less than the Intel t3 of the same size, and Amazon Linux runs on both.

Look the image up, don't copy it

An AMI ID like ami-0f119f573d13441ab is different in every Region and changes every time AWS publishes an update, which for Amazon Linux happens every few weeks. A script with a copied ID launches an old, unpatched image, or fails in another Region. So the lab asks AWS for the current one by name:

BASH
aws ssm get-parameter --name /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-arm64 \
  --query Parameter.Value --output text

AWS keeps that public parameter pointed at the newest image. The run got one published six days earlier.

Launch it

In the console: EC2 > Launch instance. Pick Amazon Linux 2023 for 64-bit Arm and t4g.micro, choose Proceed without a key pair, set the network under Network settings > Edit, and paste the launch script under Advanced details > User data. In the same section, set Metadata version to V2 only.

(Your machine)

BASH
./ec2-lab.sh launch
OUTPUT
== A small network for it
  VPC 10.10.0.0/16, public subnet, internet gateway, route table
  security group jr-lesson10-web-sg: port 80 from your IP only, no SSH
== Launching jr-lesson10-web: t4g.micro, Amazon Linux 2023 (billing starts now)
  running, public address 3.128.205.170
== Give it 2 minutes to install its web server, then run ./ec2-lab.sh show

The network is Lesson 9's, rebuilt. The launch itself is one call:

BASH
aws ec2 run-instances --image-id "$ami" --instance-type t4g.micro \
  --network-interfaces "DeviceIndex=0,SubnetId=$subnet,Groups=$sg,AssociatePublicIpAddress=true" \
  --metadata-options HttpTokens=required,HttpEndpoint=enabled \
  --user-data file:///tmp/jr-lesson10-userdata.sh \
  --tag-specifications "ResourceType=instance,Tags=[...]" "ResourceType=volume,Tags=[...]"
PartWhat it does
--image-id, --instance-typeThe image and the size
--network-interfacesThe subnet, the security group, and a public IPv4 address
--metadata-options HttpTokens=requiredRequires IMDSv2, explained below
--user-dataThe launch script, as in Lesson 7
--tag-specifications, twiceTags both the server and its disk, so the disk is findable too
No --key-nameNo key pair, so no SSH key exists to lose

The launch script installs nginx and one more thing: a small script that cloud-init, the program that runs launch scripts, repeats at every boot. It asks the server's own metadata which size and zone it's in, and writes them into the page. That's how the page will notice the resize later.

What you're running, and what it costs

In the console: EC2 > Instances > pick the server. The Details, Storage and Networking tabs show the same facts.

(Your machine)

BASH
./ec2-lab.sh show
OUTPUT
== What's running
  name           jr-lesson10-web
  state          running
  type           t4g.micro (arm64)
  image          al2023-ami-2023.12.20261109.0-kernel-6.18-arm64
  zone           us-east-2a
  private IP     10.10.0.55
  public IP      3.128.205.170
  metadata       IMDSv2 required
  key pair       none
  disk           8 GB gp3, 3000 IOPS, 125 MB/s, deleted with the server: yes
== What it costs (on-demand, a 730-hour month)
  server         $  6.13 a month
  disk           $  0.64 a month
  public IPv4    $  3.65 a month
  total          $ 10.42 a month, about $0.0143 an hour
== The page at http://3.128.205.170/
  Served by a t4g.micro in us-east-2a, started 14:43 UTC.

Line by line, the ones worth knowing:

  • Two addresses. 10.10.0.55 is private, from Lesson 8's subnet range; servers in the VPC use it to reach each other. 3.128.205.170 is public, on loan from AWS, and only exists because the subnet routes to an internet gateway.
  • The disk is gp3, AWS's general-purpose SSD (solid-state drive) storage, with a baseline of 3,000 IOPS (input/output operations per second) and 125 MB/s whatever its size. "Deleted with the server: yes" is the default for a root disk; extra disks default to no, and are a common leftover bill. Lesson 16 covers disks properly.
  • The page came from the server's own metadata: the size and zone are real, not typed.

The public address is a third of the bill

$3.65 of the $10.42 is the public IPv4 address. Lesson 1 found the same charge; here it's 35% of a small server's cost. Real systems avoid it by keeping servers in private subnets behind a load balancer (Lesson 12), so only the load balancer has public addresses.

IMDSv2, and why there's no SSH

Every EC2 server can ask a local address, 169.254.169.254, about itself: its size, its zone, and, if it has an IAM role, temporary credentials for that role. That's the instance metadata service, and it's how the page knew its size. The risk: if an app on the server can be tricked into fetching a web address an attacker chooses, called SSRF (server-side request forgery), the attacker can point it at that address and read the credentials.

IMDSv2 requires a token first, fetched with a PUT request and a special header, which tricked requests almost never manage. The launch script does exactly that before asking anything. Requiring it is one setting, HttpTokens=required, and the output confirms it.

No key pair means no SSH. This server never needs anyone to log in: its launch script set it up. With nothing listening for logins and no key to steal, there's one less way in. Lesson 11 shows how to reach a server when you do need a shell, still without SSH.

Require IMDSv2 on every server

A server that allows IMDSv1 can hand its IAM credentials to anyone who tricks an app into fetching the right address. Set HttpTokens=required at launch, or make it the default for the whole Region in the EC2 console's account settings, and keep SSH closed unless something really needs it.

Stop, resize, start

In the console: EC2 > Instances > pick the server > Instance state > Stop instance. Once it's stopped: Actions > Instance settings > Change instance type. Then Instance state > Start instance.

(Your machine)

BASH
./ec2-lab.sh resize
OUTPUT
== Stopping jr-lesson10-web (t4g.micro, public IP 3.128.205.170)
  state: stopped
  public IP: None
  its disk: in-use, still billed while stopped
== Changing the size: t4g.micro to t4g.small
== Starting it again
  state: running, type: t4g.small
  public IP: 18.188.248.130 (it was 3.128.205.170)
== The page now says
  Served by a t4g.small in us-east-2a, started 14:44 UTC.

Three things happened that matter:

  1. Stopping gave the public address back. The server came back on 18.188.248.130, not 3.128.205.170. Anything that pointed at the old address, a bookmark, a DNS (Domain Name System) record, a firewall rule somewhere else, is now wrong. The private address and the disk stayed.
  2. The size can only change while stopped. It's the server's hardware; the disk carries everything over, so it starts up exactly as it was, just bigger. That's scaling up, or vertical scaling. Lesson 12 does the other kind, scaling out: more servers, not a bigger one.
  3. The page updated itself. The every-boot script ran again and wrote t4g.small, at the new start time.

Stop, reboot or terminate

RebootStopTerminate
Server chargeContinuesStopsStops
Disk chargeContinuesContinuesStops, if the disk is deleted with it
Public IPv4 addressKeptLost; a new one at startGone
Private addressKeptKeptGone
Can come backYesYesNo

To keep the same public address through stops, attach an Elastic IP address, at the same $0.005 an hour. Better still, put a load balancer or a DNS name in front (Lessons 12 and 14), so nobody depends on a server's address at all.

Stopped servers still cost money

A stopped server's disk keeps billing, and so does any Elastic IP attached to it. A few stopped servers with large disks, forgotten for months, add up. Terminate what you don't need, and check the disk is gone too.

Paying less: the four ways to buy

Everything in this lesson is on-demand: by the second, no commitment. For servers that run all the time, AWS sells the same thing cheaper:

Way to payDiscountThe catchFits
On-demandNoneThe highest priceLabs, tests, anything short or unpredictable
Savings PlansUp to 72%You commit to an amount an hour for 1 or 3 yearsServers that always run
Reserved InstancesUp to 72%Like a Savings Plan, tied to one type in one RegionOlder setups; Savings Plans mostly replaced them
SpotUp to 90%AWS can take the server back with 2 minutes' warningWork that can stop and restart: batch jobs, builds

Lesson 37 works out when a commitment pays off.

Take it down

In the console: EC2 > Instances > Instance state > Terminate instance, then VPC > Your VPCs > Actions > Delete VPC.

(Your machine)

BASH
./ec2-lab.sh delete
OUTPUT
== Terminated jr-lesson10-web. Billing for it, and its disk, stops now.
== Deleted jr-lesson10-vpc and everything in it
== Left over from this lab (tagged lesson=10)
  servers:            0
  disks:              0
  VPCs:               0
== Things that bill, anywhere in us-east-2
  servers:            0
  disks:              0
  Elastic IPs:        0

The second check looks across the whole Region, tagged or not: stopped servers, disks left behind and idle Elastic IPs are the three classic EC2 leftovers.

In real systems

  • Servers are cattle, not pets. Nobody logs in to fix one; a launch script or a prepared image builds it, and a broken one is replaced, not repaired.
  • Teams build their own images with every package already installed, so new servers start in seconds instead of installing at boot.
  • IMDSv2 is required account-wide, usually by a policy no one can switch off (Lesson 36).
  • Few production servers have public addresses. They sit in private subnets behind a load balancer, and patch through a NAT gateway or VPC endpoints.
  • Graviton is the default choice for new Linux workloads, for the price.
  • Rightsizing is a regular chore: a server running at 5% CPU all month is paying for a size it doesn't need.

Spot the problem

Work out what's wrong before you read the explanation.

"We stopped it overnight to save money, and now the shop is down." A small company runs its shop on one EC2 server. Its domain name points straight at the server's public IP address. To save money, someone stops the server every night and starts it each morning. The next morning the shop won't load, though the server shows as running. What happened? Stopping released the public address, and the server came back with a different one. The domain still points at yesterday's address, which AWS may have given to someone else. The fix: attach an Elastic IP address and point the domain at that, or better, put a load balancer in front and point the domain at the load balancer. Then check: does the address in the DNS record match the server's address after a stop and start?

Keep this

RememberWhy
Type, image, disk, networkThe four choices behind every server
t4g.micro: family, generation, processor, sizeYou can read any type name
Look up the AMI by nameIDs differ by Region and go stale in weeks
Require IMDSv2Stops tricked requests reading the server's credentials
The public IPv4 address is $3.65 a monthA third of a small server's bill
Stop loses the public address, keeps the disk billUse an Elastic IP, a load balancer or DNS
Resize while stoppedScaling up; Lesson 12 scales out

Where you are now

The bakery's server ran on EC2 with an image looked up by name, IMDSv2 required and no way to log in. It cost $10.42 a month on paper, a third of that for its public address. It stopped, grew from a t4g.micro to a t4g.small, came back on a new address, and its page noticed. Then it was terminated with its disk, leaving nothing. Next, Lesson 11 gets a shell on a server like this one, with no SSH, no open port and no key: AWS Systems Manager.

Check yourself

  1. What does the g in t4g.micro mean?

  2. Why does the lab look up the AMI through a parameter instead of using an ID?

  3. From the run, how much of the $10.42 a month was the public IPv4 address?

  4. The server was stopped and started. Which of these changed?

  5. What does requiring IMDSv2 protect against?

  6. A server is stopped for a month. What do you still pay for?

Key terms

  • EC2 (Elastic Compute Cloud): AWS's virtual servers.
  • Instance: one virtual server.
  • Instance type: a server's size and kind, such as t4g.micro.
  • Burstable: a t-family server that builds up CPU credits while idle and spends them when busy.
  • Graviton: AWS's own Arm processor, the g in t4g.
  • AMI (Amazon Machine Image): what a server starts from: the operating system and anything installed.
  • EBS (Elastic Block Store) volume: a server's disk.
  • gp3: EBS's general-purpose SSD (solid-state drive) disk type.
  • User data: the launch script a server runs at first boot.
  • Instance metadata service: the local address a server asks about itself, at 169.254.169.254.
  • IMDSv2: the version of it that requires a token, protecting against SSRF (server-side request forgery).
  • Elastic IP address: a public IPv4 address you keep through stops and starts.
  • Scaling up: a bigger server. Scaling out: more servers.
  • On-demand, Savings Plans, Reserved Instances, Spot: the four ways to pay for EC2.

Go deeper

BASH
aws ec2 describe-instances --profile juniper-rye --query 'Reservations[].Instances[].MetadataOptions'