Lesson 5 of 8 in Cloud fundamentals with AWS
Virtual machines on EC2
Amazon EC2 rents you virtual machines by the second. You choose an image (AMI) for the starting disk, an instance type for the CPU and memory, a security group for the firewall and a key pair for login.
Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.
Read these first: VPC and subnets: your private network in AWS, Linux and the shell
The problem this solves
A virtual machine is the simplest thing the cloud sells: a computer you can log in to. You pick it, use it for an hour and delete it. The trouble is that a single launch screen asks for six decisions, and each one has a trap.
Pick an instance type that is not free-tier eligible and the hours cost money. Leave the firewall open to the whole internet and bots find the server within minutes. Stop the machine instead of terminating it and the disk keeps billing.
Knowing what each part does makes the screen stop looking like a form to fill in.
How it works
- Data
- Runs code
- Security
An image supplies the starting disk, the instance type sets the CPU and memory, the security group sets the firewall, and the result is one running machine with a root disk.
- AMI to Running VM: 1 copied to disk
- Instance type to Running VM: 2 sizes the machine
- Security group to Running VM: 3 filters traffic
- Running VM to Root disk: 4 boots from
- People and clients
- Security
- Runs code
Your SSH client connects to port 22, the security group lets you in only if a rule matches your address, and the VM checks your key against the public key it was given at launch.
- Your laptop to Security group: 1 connect to port 22
- Security group to VM: 2 allowed if rule matches
- Public key to VM: installed for login
An Amazon Machine Image (AMI) is a template for the machine's disk: an operating system and maybe some software. Amazon Linux 2023 is AWS's own Linux image. An instance type is a size, a family and a number such as t3.micro, which fixes the CPU and memory. Not all types are free-tier eligible, and AWS can tell you which ones are, as the first step shows.
A security group is a stateful firewall attached to the machine. It lists what may come in, and replies to allowed traffic go out automatically. By default nothing comes in. Your job is to open only the port you need, to only the address that needs it. A key pair is two linked files: AWS stores the public key and puts it on the VM, and you keep the private key and use it to log in.
The billing rules matter more than the form. While the instance runs, you pay for it. When you stop it, you stop paying for compute but you still pay for its disk, and a stopped machine is easy to forget. Terminating deletes the machine and, by default, its root disk. AWS also bills for every public IPv4 address by the hour, whether or not the machine is busy.
Azure calls these Virtual Machines, with images and sizes. Google Cloud calls them Compute Engine instances, with images and machine types.
Try it yourself
This lab launches one small machine and deletes it within the hour. Set a timer on your phone before you start. You need an SSH client: Linux, macOS, WSL or the OpenSSH client in recent Windows.
- 1Look up what is free-tier eligible and the current image
aws ec2 describe-instance-types --filters Name=free-tier-eligible,Values=true --query "InstanceTypes[].InstanceType" --output text aws ssm get-parameter --name /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 --query Parameter.Value --output text
Both commands are read-only and run in CloudShell. The first prints the instance types your plan treats as free-tier eligible in this Region. The second prints the ID of the current Amazon Linux 2023 image. The console's launch screen picks the same image for you.
- 2Launch one instance in the console
Open EC2, then Launch instance. Name it
learn-vm. Choose Amazon Linux 2023 and an instance type marked Free tier eligible. Under Key pair, create a new one namedlearn-keyand keep the downloaded.pemfile safe. Under Network settings, keep the default VPC, leave Auto-assign public IP enabled, and set SSH traffic to allow from My IP, never from anywhere. Leave the storage at its default size. Choose Launch instance. If your free plan blocks a type, the console says so. - 3Log in over SSH
chmod 400 learn-key.pem ssh -i learn-key.pem ec2-user@<public-ip>
Run this from the folder with the key file, on your own computer rather than CloudShell. Copy the public IPv4 address from the instance's detail page. Amazon Linux's default user is
ec2-user. Typeyesthe first time to accept the host fingerprint. - 4Look around and read the instance's metadata
uname -sr TOKEN=$(curl -s -X PUT http://169.254.169.254/latest/api/token -H "X-aws-ec2-metadata-token-ttl-seconds: 60") curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-type exit
Run these on the VM.
169.254.169.254is a link-local address that only the instance itself can reach, and it answers questions about the instance. Asking for a short-lived token first is the safer second version of that service. The last command should print the instance type you chose. - 5Clean up: terminate, do not just stop
In the EC2 console select
learn-vm, then Instance state, then Terminate (delete) instance. Confirm. Also open Key pairs and deletelearn-key, then delete the.pemfile from your computer. - 6Verify nothing is left running
aws ec2 describe-instances --filters Name=instance-state-name,Values=pending,running,stopping,stopped --query "Reservations[].Instances[].[InstanceId,State.Name]" --output table aws ec2 describe-volumes --query "Volumes[].[VolumeId,State,Size]" --output table aws ec2 describe-addresses --query "Addresses[].PublicIp" --output text
All three should list nothing for this Region. A terminated instance can stay visible in the console for a short while, which is normal.
What breaks, and where to look
Permissions 0644 for 'learn-key.pem' are too open. It is required that your private key files are NOT accessible by others. This private key will be ignored.
- WHY IT HAPPENS
- SSH refuses to use a private key that other users on your computer can read.
- CHECK FIRST
- Run
ls -l learn-key.pemto see the permissions, thenchmod 400 learn-key.pemso only you can read it.
ssh: connect to host <public-ip> port 22: Connection timed out
- WHY IT HAPPENS
- The security group allows port 22 only from the address you had at launch, and your address has since changed. It can also mean the instance has no public IP or its subnet has no route to an internet gateway.
- CHECK FIRST
- Run
curl -s https://checkip.amazonaws.comto see your current address, then compare it with the inbound rule on the instance's Security tab. Edit the rule to your new address.
ec2-user@<public-ip>: Permission denied (publickey).
- WHY IT HAPPENS
- You used the wrong login name for the image (Ubuntu images use
ubuntu, for example) or the wrong key file. - CHECK FIRST
- Run
ssh -v -i learn-key.pem ec2-user@<public-ip>and read which keys are offered. Check the image's documentation for its default user, and confirm the key pair name on the instance matches your file.
When not to use this
- One hand-made VM is a pet. For anything that must stay up, put VMs behind a load balancer in an Auto Scaling group, or use containers or a managed platform, so no single machine matters.
- Do not use a VM for jobs that run for seconds. Functions and managed services bill only while they work.
Check yourself
What is the difference between stopping and terminating an instance?
Stopping halts the compute charge but keeps the machine and its disk, which still bills. Terminating deletes the machine and, by default, its root disk.
Why restrict SSH to your own IP address?
An open port 22 is found and attacked by bots within minutes. Allowing only your address means everyone else is blocked by the firewall.
Which key do you keep, and which does AWS keep?
You keep the private key. AWS stores the public key and installs it on the VM so the VM can verify you.
Keep going
Lesson 59 of 92 in the DevOps path