EC2: Your First Cloud Server
Launch a free-tier-eligible EC2 instance, secure it with a security group, connect over SSH, install nginx, use user data, and clean up.
Video chapters
- 00:00Intro
- 00:40EC2 in six words
- 01:32Step 1: Launch the instance
- 02:24Step 2: Network and firewall
- 03:27Step 3: Connect with SSH
- 04:27Step 4: Serve a web page
- 05:17Step 5: Automate it with user data
- 06:31Stop, terminate, and what costs money
- 08:03When something doesn't work
- 08:58What you built, and how to clean up
Time to build. In this episode you launch a virtual server on AWS, connect to it securely, and serve a web page to the internet, then automate the same setup with user data, and clean up so nothing keeps costing money.
What you need
- The AWS account and admin user from Episode 3
- A terminal with SSH (Episode 2)
EC2 in six words
First, six words you'll need.
An instance is one virtual server.
An A M I, or Amazon machine image, is the disk image it starts from, like Ubuntu, or Amazon Linux.
The instance type is its size: how many virtual C P Us, and how much memory. We'll use t3 micro, which is free tier eligible for new accounts. On the free plan, the console marks eligible types, and also lists others, like t3 small and t4g micro.
An E B S volume is the server's disk. It's separate from the instance, so it survives when you stop the server.
A security group is the firewall around the instance. It decides which traffic can reach which port.
And a key pair is how you log in over SSH, exactly as we practiced in episode two.
Quick reference
- Instance: One virtual server
- AMI: The disk image it starts from: Ubuntu, Amazon Linux…
- Instance type: Size: vCPUs + memory, e.g. t3.micro
- EBS volume: The server's disk. Survives a stop
- Security group: The firewall around the instance
- Key pair: How you log in over SSH
our server
Name: web-1 AMI: Ubuntu Server 24.04 LTS Type: t3.micro Storage: 8 GB gp3 (EBS) Network: default VPC, public IP Firewall: sg-web 22 from my IP 80 from anywhere Login: key pair web-key
Step 1 · Launch the instance
Step one: launch. Open the EC2 console, and click launch instance.
Give it a name, like web one, and add the tag project equals cloud course, from last episode's habits.
For the image, choose Ubuntu Server, the latest long term support version, marked free tier eligible.
For the instance type, choose t3 micro. Always look for the free tier eligible label while you're learning.
Under key pair, create a new one called web key. Your browser downloads the private key as a dot p e m file.
The default storage, an eight gigabyte gp3 volume, is plenty for a small web server.
That p e m file can only be downloaded once. Move it to a safe folder, like your dot ssh folder, and set its permissions to four hundred, as we learned.
Quick reference
- EC2 console → Launch instance
- Name: web-1 (add tag project = cloud-course)
- AMI: Ubuntu Server 24.04 LTS · Free tier eligible
- Instance type: t3.micro · Free tier eligible
- Key pair: Create new → web-key → .pem
- Storage: 8 GB gp3 is plenty
Step 2 · Network and firewall
Step two: the network and firewall. Keep the default V P C, and make sure auto-assign public IP is turned on, so the server can be reached from the internet.
Create a new security group, called s g web, with two inbound rules.
SSH on port twenty two, with the source set to my IP. The console fills in your current address, as a slash thirty two, meaning exactly one address. And HTTP on port eighty, from anywhere.
Never open SSH to zero dot zero dot zero dot zero slash zero. Bots scan the whole internet for open SSH ports all day long. If your home IP changes later, just update this rule.
HTTP is open to everyone, because that's the whole point of a website.
Click launch instance. In under a minute, the state changes to running, and shortly after, the status checks show two of two passed. Copy the instance's public IPv4 address. You'll need it next.
Quick reference
- Network: default VPC, auto-assign public IP: on
- Create security group: sg-web
- SSH only from My IP, never 0.0.0.0/0
- HTTP open to the world: it's a website
- Launch instance → wait for “Running” + 2/2 checks
sg-web · inbound rules
Type Port Source SSH 22 My IP (203.0.113.7/32) HTTP 80 0.0.0.0/0 Outbound: all traffic (default)
Step 3 · Connect with SSH
Step three: connect. First, make sure the key file is only readable by you.
Then SSH in, with dash i, the key file, the user name ubuntu, and your instance's public IP.
The first time, SSH asks if you trust this server. Type yes.
And you're in. You're now typing commands on a computer in an AWS data center.
The user name depends on the image. On Ubuntu it's ubuntu, and on Amazon Linux it's ec2 dash user. Using the wrong one is a very common reason for a permission denied error.
If you can't use a terminal right now, the EC2 console has a connect button, with EC2 instance connect, which opens a terminal in your browser.
Look closely at the prompt. It shows a private IP starting with one seven two dot thirty one. That's the server's address inside your V P C. The public IP you connected to is mapped to it by AWS.
Quick reference
chmod 400 ~/.ssh/web-key.pem ssh -i ~/.ssh/web-key.pem ubuntu@203.0.113.10 Are you sure you want to continue connecting (yes/no)? yes ubuntu@ip-172-31-20-15:~$
Step 4 · Serve a web page
Step four: serve a web page. Update the package list, and install nginx, just like in episode two.
Check that the service is active and running.
Replace the default page with your own. This command writes a small HTML heading into nginx's web folder. We use sudo tee, because that folder belongs to root.
Test it from the server itself with curl localhost.
You should see your heading.
Now open your browser, and visit the public IP, with http. Your page is live on the internet. Congratulations, that's your first cloud server serving real traffic.
If curl works on the server but the browser can't connect, the problem is almost always the security group. Remember the troubleshooting order from episode two.
Quick reference
sudo apt update && sudo apt install -y nginx systemctl status nginx # active (running) echo '<h1>Hello from EC2</h1>' | sudo tee /var/www/html/index.html curl localhost <h1>Hello from EC2</h1>
Step 5 · Automate it with user data
Typing commands by hand works once. But what if you need ten identical servers? Step five: user data.
When you launch an instance, open advanced details, and scroll to the user data box.
Anything you put there runs once, as root, when the server boots for the first time.
Here's the same setup as a script: update, install nginx, write the page, and make sure the service is enabled and running. Notice there's no sudo, because user data already runs as root.
Launch a second instance with this script and the same security group. A minute later, open its IP, and the page is already there, without you ever logging in. That's your first step towards infrastructure as code.
This matters more than it looks. In real systems, servers are created and destroyed automatically, for example when traffic grows at night and shrinks in the morning. Nobody logs in to set those servers up. They configure themselves at boot, from scripts like this one, or from a prepared image.
If the page doesn't appear, SSH in and read the cloud init output log. It shows every line your script printed, including errors.
Quick reference
- Launch instance → Advanced details → User data
- Runs once, as root, on first boot
- No SSH needed: the server sets itself up
- Logs: /var/log/cloud-init-output.log
user data · bash
#!/bin/bash apt-get update -y apt-get install -y nginx echo "<h1>Built by user data</h1>" \ > /var/www/html/index.html systemctl enable --now nginx
Stop, terminate, and what costs money
Now let's talk about money, because this is where beginners get surprised.
Stopping an instance is like shutting down a computer. You stop paying for compute, but the disk still exists and is still billed. And when you start it again, it usually gets a new public IP.
Terminating deletes the instance completely, and by default its root disk too. There's no undo, so terminate only what you're sure about.
If you need an IP that never changes, AWS offers Elastic IPs. They're useful for real projects, but they have a cost, so release any you aren't using.
In fact, AWS charges for public IPv4 addresses in general. While you're on free plan credits, this comes out of your credits, but in real projects it adds up, and it's one reason engineers put many services behind a single load balancer.
And right-size. Start with a small instance type, watch the C P U and memory, and only move to a bigger one when you actually need it. Bigger isn't automatically better. It's just more expensive.
You'll also hear about different ways to pay for EC2. On demand is what we're using: pay by the second, no commitment. Savings plans give a discount if you commit to a steady amount of usage for one or three years. And spot instances use spare capacity at a big discount, but AWS can take them back at short notice, so they suit batch jobs, not your main website.
Quick reference
- Stop: No compute charge. Disk kept and billed. Public IP changes
- Terminate: Deletes the instance (and its root disk by default)
- Elastic IP: A fixed public IP. Useful, but it has a cost
- Public IPv4: AWS charges for public IPv4 addresses
- Right-size: Start small. Check CPU before buying bigger
When something doesn't work
Here are the five problems you're most likely to hit, and how to fix them.
If SSH times out, it's almost always the security group. Your home IP may have changed since you created the rule. Edit the rule and choose my IP again.
If you get permission denied, public key, you're using the wrong user name, the wrong key file, or the key file's permissions are too open.
If the website won't load, check the port eighty rule, check that nginx is running, and make sure you typed http, not https. We haven't set up HTTPS on this server yet.
If your server seems to have vanished, check the region selector. You're probably looking at a different region.
And if the status checks fail, open the status checks tab for details. Often a reboot fixes it. With user data, launching a fresh instance is just as easy.
Quick reference
- SSH times out: Security group: is port 22 open for your current IP?
- Permission denied: Wrong user name, wrong key, or key not chmod 400
- Site won't load: Port 80 rule, nginx running, http not https
- “My server vanished”: Check the region selector, top right
- Checks failing: Status checks tab, then reboot or relaunch
What you built, and how to clean up
Let's look at what you built. You connect over SSH with your key, visitors connect with a browser, and both pass through the security group, which only allows what you chose, before reaching your EC2 instance running nginx.
Now clean up, the same day. Terminate both instances. Release any Elastic IPs. And check the volumes page, to make sure no disks were left behind. The EC2 console also has a global view, which shows resources in every region, so nothing hides in a region you forgot about.
In the next episode, we move from servers to storage. You'll learn S3, AWS's object storage, and host a static website with no server at all. The notes, with every command and setting from today, are linked in the description.
Quick reference
You → Visitors → Security group → EC2: web-1
Clean up: terminate instances · release Elastic IPs · check volumes
Practice
- Launch the same server using only the user data script, without SSH.
- Change the security group so SSH works only from your current IP, and test from a phone hotspot.
- Stop the instance, start it again, and note what changed.
Next episode
Episode 5: S3, object storage and hosting a static website.
Key takeaways
- An EC2 instance starts from an AMI, with an instance type, an EBS disk and a security group
- Open port 22 only to your own IP, and keep the .pem key safe with chmod 400
- User data runs a setup script on first boot, so servers are repeatable
- A stopped instance still pays for its disk: terminate what you no longer need