Launching Your First EC2 Instance
In this walkthrough you launch a small Linux web server, connect to it with SSH, see it serve a web page and then delete it. You will do it first in the console and then with the CLI, so you understand both. Make sure your budget alerts are set up before you begin.
Part 1: Launch from the console
Sign in through your IAM Identity Center portal, pick your practice Region in the top-right corner, open EC2 and choose Launch instance.
- Name:
kuza-web. This creates a tagName=kuza-web. - AMI: Amazon Linux 2023 (64-bit x86).
- Instance type: a small type such as
t3.micro. The console marks types eligible for the Free Tier on your account; check the label rather than assuming. - Key pair: choose Create new key pair, name it
kuza-key, type ED25519, format.pem(OpenSSH). The private key downloads once; AWS does not keep a copy, so store it safely. - Network settings: keep the default VPC and let AWS create a security group. Tick Allow SSH traffic from and set it to My IP, not Anywhere. Tick Allow HTTP traffic from the internet.
- Storage: keep the default 8 GiB gp3 root volume.
- Advanced details → User data: paste the script below. It runs once, as root, when the instance first boots.
#!/bin/bash dnf update -y dnf install -y nginx echo "<h1>Habari! Served from EC2</h1>" > /usr/share/nginx/html/index.html systemctl enable --now nginx
Choose Launch instance. After a minute the instance state becomes Running and the status checks pass. Copy its Public IPv4 address and open http:// followed by that address in your browser. You should see the heading from the script.
If the console warns that your security group allows SSH from 0.0.0.0/0, fix it before launching. Open SSH invites constant automated login attempts. "My IP" may change when you switch networks (for example from campus Wi-Fi to mobile data); just edit the rule when it does.
Part 2: Connect with SSH
On macOS, Linux or Windows 10/11 (which include OpenSSH), open a terminal in the folder containing the key. On macOS and Linux, first make the key readable only by you, or SSH refuses to use it:
chmod 400 kuza-key.pem ssh -i kuza-key.pem ec2-user@203.0.113.25
Replace 203.0.113.25 with your instance's public IP. The default user is ec2-user on Amazon Linux and ubuntu on Ubuntu. The first time, SSH asks you to confirm the server's fingerprint; type yes. Once in, try a few commands:
whoami systemctl is-active nginx curl -s localhost
Type exit to disconnect. If you cannot use SSH, select the instance and choose Connect → EC2 Instance Connect to get a terminal in the browser.
For real projects, consider Systems Manager Session Manager. It gives you a shell through the AWS API, so you can close port 22 completely and manage access with IAM instead of key files.
Part 3: The same thing with the CLI
These commands launch an equivalent server in your default VPC. IDs in the outputs are examples.
aws ec2 create-key-pair --key-name kuza-key-cli --key-type ed25519 \
--query "KeyMaterial" --output text > kuza-key-cli.pem
chmod 400 kuza-key-cli.pem
aws ec2 create-security-group --group-name kuza-web-sg \
--description "Practice web server"
MY_IP=$(curl -s https://checkip.amazonaws.com)
aws ec2 authorize-security-group-ingress --group-id sg-0123456789abcdef0 \
--protocol tcp --port 22 --cidr "$MY_IP/32"
aws ec2 authorize-security-group-ingress --group-id sg-0123456789abcdef0 \
--protocol tcp --port 80 --cidr 0.0.0.0/0
aws ec2 run-instances \
--image-id resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 \
--instance-type t3.micro \
--key-name kuza-key-cli \
--security-group-ids sg-0123456789abcdef0 \
--user-data file://user-data.sh \
--tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=kuza-web-cli}]" \
--query "Instances[0].InstanceId" --output text
The resolve:ssm: prefix asks EC2 to look up the latest Amazon Linux 2023 AMI ID from a public parameter, so you never hard-code an AMI ID that differs between Regions. Wait for it to start, then get the public IP:
aws ec2 wait instance-running --instance-ids i-0abc1234def567890
aws ec2 describe-instances --instance-ids i-0abc1234def567890 \
--query "Reservations[0].Instances[0].PublicIpAddress" --output text
Part 4: Clean up
Terminate the instance when you are done. In the console: select it, then Instance state → Terminate (delete) instance. With the CLI:
aws ec2 terminate-instances --instance-ids i-0abc1234def567890
Then delete the security group (once the instance has terminated) and key pairs you no longer need:
aws ec2 delete-security-group --group-id sg-0123456789abcdef0 aws ec2 delete-key-pair --key-name kuza-key-cli
Clicking Stop instead of Terminate and thinking the bill has ended. A stopped instance keeps its EBS volume, which is billed per GB-month. Terminate practice instances, and check the EC2 dashboard in your Region shows zero running instances.
Troubleshooting
- SSH times out: the security group does not allow port 22 from your current IP, or the instance has no public IP.
- "Permission denied (publickey)": wrong user name, wrong key file or key permissions too open.
- Web page does not load: port 80 is not allowed, you typed
https://instead ofhttp://, or the user data script failed (check/var/log/cloud-init-output.logon the instance).
Key points
- Launching needs an AMI, an instance type, a key pair, a security group and storage.
- Allow SSH only from your IP, or use Instance Connect or Session Manager instead.
- User data scripts configure a server automatically on first boot.
- The CLI can do everything the console does;
resolve:ssm:finds the latest AMI for you. - Terminate instances and delete leftovers after every practice session.
Tip: use the ← and → keys to move between pages.
