Skip to main content

Cloud and deployment · Beginner · 15 min

How to Launch an AWS EC2 Instance and Connect with SSH

By Padding · Updated 15 Nov 2026
AWS EC2 · Ubuntu 26.04.1 LTS

You will launch an Ubuntu 26.04 LTS virtual server on AWS EC2 with the AWS CLI, allow SSH from your own address only, log in with a new key pair, and then terminate everything so it stops billing.

On this page

Amazon EC2 rents you virtual servers by the second. You pick an operating system image, a size and a network rule, and a minute later you have a machine with a public address that you log in to like any other server. This tutorial launches one small Ubuntu 26.04 LTS instance, connects to it, and then tears it all down.

This costs money. An EC2 instance, its disk and its public IPv4 address are all billed for as long as they exist, including while the instance is stopped (the disk, and any address that you keep, still bill). A small instance for an hour costs very little, but one that you forget can cost real money by the end of the month. The last step deletes everything. Do not skip it, and consider setting a budget alert in AWS Billing first. Check current prices at aws.amazon.com/ec2/pricing/ and whether your account has any free usage; the free-tier rules depend on when the account was created.

The tutorial uses the command line, because commands stay accurate when the console's layout changes. Everything you do in the console is also possible there.

Step 1 · Check who you are and where you are working

Everything in AWS belongs to a region. Resources you create in one region are invisible in another, which is a common reason for "my instance disappeared". Check the identity the CLI uses, and set a region for this shell session. Use a region near you; eu-west-1 is only an example:

Bash
aws sts get-caller-identity
export AWS_REGION=eu-west-1
echo "Working in $AWS_REGION"

get-caller-identity prints your account number and the user or role you are signed in as. If it fails, sign in again: aws sso login for Identity Center, or check your configured credentials. Never create access keys for the root user of the account.

Step 2 · Find the Ubuntu image

An instance starts from an Amazon Machine Image (AMI). Canonical, the publisher of Ubuntu, publishes the current image ID of each release as a public parameter, so you do not have to search for it. This reads the ID of the Ubuntu 26.04 server image for 64-bit x86 processors:

Bash
AMI_ID=$(aws ssm get-parameters \
  --names /aws/service/canonical/ubuntu/server/26.04/stable/current/amd64/hvm/ebs-gp3/ami-id \
  --query 'Parameters[0].Value' --output text)
echo "$AMI_ID"

The output is an ID like ami-0123456789abcdef0. It differs by region and changes when Canonical publishes a new build, so always read it fresh. The path format is documented at ubuntu.com/aws/docs/aws-how-to/instances/find-ubuntu-images/. The amd64 image needs an x86 instance type such as t3.micro. For Graviton (Arm) instance types, use arm64 in the path and a type such as t4g.micro.

Step 3 · Create a key pair

SSH logs you in with a key pair instead of a password. AWS creates the pair and gives you the private half once. It does not keep a copy, so you cannot get it again. The umask 077 makes the file private from the moment it is created, which SSH requires:

Bash
(umask 077; aws ec2 create-key-pair --key-name padding-demo --key-type ed25519 \
  --query KeyMaterial --output text > padding-demo.pem)
ls -l padding-demo.pem

The listing shows mode -rw-------. --key-type ed25519 creates a modern key (Linux instances accept RSA and ed25519). The AWS-stored half is the public key, which the instance installs for its default user at first boot. Keep padding-demo.pem safe, and do not commit it to Git. To use your own existing public key instead, upload it with aws ec2 import-key-pair.

Step 4 · Allow SSH from your address only

A security group is a firewall in front of the instance. The default rules allow no incoming traffic. Create a group in your account's default network, and allow only port 22 (SSH) and only from your current public IP address. Do not use 0.0.0.0/0 for SSH. That opens the login to every scanner on the internet within minutes.

Bash
SG_ID=$(aws ec2 create-security-group --group-name padding-demo-ssh \
  --description "SSH from my IP only" --query GroupId --output text)
MY_IP=$(curl -4 -s https://checkip.amazonaws.com)
aws ec2 authorize-security-group-ingress --group-id "$SG_ID" \
  --protocol tcp --port 22 --cidr "$MY_IP/32"
echo "Allowed $MY_IP to reach port 22 in $SG_ID"

create-security-group without a VPC uses your account's default VPC. If the command says there is no default VPC, create one with aws ec2 create-default-vpc, or pass --vpc-id for a network you have. -4 makes the lookup return an IPv4 address, and /32 means exactly one IPv4 address. If your network only has IPv6, the lookup returns nothing useful; the rule would then need the IPv6 form, --ip-permissions 'IpProtocol=tcp,FromPort=22,ToPort=22,Ipv6Ranges=[{CidrIpv6=YOUR-ADDRESS/128}]', and the instance would also need an IPv6 address, which this tutorial does not set up. If your home or office IP address changes, SSH will time out until you add the new one with another authorize-security-group-ingress command and, if you want, remove the old rule with aws ec2 revoke-security-group-ingress and the same arguments.

Step 5 · Launch the instance

Launch one small instance. --metadata-options HttpTokens=required makes the instance accept only the newer, safer version of the metadata service (IMDSv2), which protects against a class of attacks where a web app is tricked into leaking the machine's credentials. The tag gives it a name that you can see in the console:

Bash
INSTANCE_ID=$(aws ec2 run-instances \
  --image-id "$AMI_ID" \
  --instance-type t3.micro \
  --key-name padding-demo \
  --security-group-ids "$SG_ID" \
  --metadata-options HttpTokens=required \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=padding-demo}]' \
  --query 'Instances[0].InstanceId' --output text)
echo "$INSTANCE_ID"
aws ec2 wait instance-running --instance-ids "$INSTANCE_ID"

The wait command returns when the instance is in the running state, usually after under a minute. t3.micro is a small general-purpose type. Because the instance launches into a default subnet, it gets a public IPv4 address automatically. Public IPv4 addresses are billed by the hour, whether or not they are in use.

Find the address:

Bash
PUBLIC_IP=$(aws ec2 describe-instances --instance-ids "$INSTANCE_ID" \
  --query 'Reservations[0].Instances[0].PublicIpAddress' --output text)
echo "$PUBLIC_IP"

Step 6 · Connect with SSH

The default user on the Ubuntu image is ubuntu. Connect with the private key. The instance can still be running its first-boot setup for a minute or two after it reaches running, so if the first attempt is refused or times out, wait a little and try again:

Bash
ssh -i padding-demo.pem ubuntu@"$PUBLIC_IP"

The first time, SSH asks whether to trust the host key and prints a fingerprint. If you want to verify it rather than type yes blindly, compare it with the fingerprints in the instance's console output once it has booted: aws ec2 get-console-output --instance-id "$INSTANCE_ID" --latest --output text. The output can be empty during the first minutes, and the fingerprints are only printed once the machine has finished booting.

Check it worked

You are at a prompt like ubuntu@ip-172-31-...:~$. Check the system, and that you have administrative rights through sudo:

Bash
lsb_release -d
sudo whoami

The first command prints Ubuntu 26.04 followed by a point release, and the second prints root. The ubuntu user can use sudo without a password, and has no password for logging in at all, so the key is the only way in. Log out with exit.

Troubleshooting

  • Permissions 0644 for 'padding-demo.pem' are too open and UNPROTECTED PRIVATE KEY FILE!: run chmod 600 padding-demo.pem.
  • Permission denied (publickey): you used the wrong user name (it must be ubuntu on this image, not root or ec2-user), the wrong key file, or a different -i file than the key pair you launched with.
  • Connection timed out: the security group does not allow your current IP address on port 22, or the instance has no public address. Check your address with curl -s https://checkip.amazonaws.com and compare it with the rule in aws ec2 describe-security-groups --group-ids "$SG_ID".
  • InvalidKeyPair.Duplicate when you create the key pair: a key pair named padding-demo already exists in this region. Pick another name in every command, or delete the old one if it is yours and unused (aws ec2 delete-key-pair --key-name padding-demo).
  • An error occurred (UnauthorizedOperation): your user is not allowed to do that action. The message names it, and an administrator can add it to your policy.
  • Commands report nothing or InvalidInstanceID.NotFound: you are in a different region than the one the resource lives in. Check echo $AWS_REGION.

Step 7 · Set up the server properly

A new instance is a bare server. Before you put anything real on it, update it and harden it. The tutorials /tutorials/initial-server-setup-ubuntu-26-04 and /tutorials/harden-ssh-on-ubuntu-26-04 apply here as they do on any Ubuntu server, with one AWS-specific difference: your firewall is the security group, so open ports 80 and 443 there when you serve a website, not only in the machine's own firewall. Keep stricter rules for SSH than for web ports.

Step 8 · Terminate everything

This is the step that stops the billing. Terminating an instance deletes it and, by default, its root disk. It cannot be undone. List what you are about to delete first, and only continue if it is the demo instance:

Bash
aws ec2 describe-instances --instance-ids "$INSTANCE_ID" \
  --query 'Reservations[0].Instances[0].[InstanceId,State.Name,Tags[?Key==`Name`]|[0].Value]' --output text

Then terminate, wait, and remove the security group and key pair. The group can only be deleted once the instance that uses it is gone:

Bash
aws ec2 terminate-instances --instance-ids "$INSTANCE_ID"
aws ec2 wait instance-terminated --instance-ids "$INSTANCE_ID"
aws ec2 delete-security-group --group-id "$SG_ID"
aws ec2 delete-key-pair --key-name padding-demo
rm padding-demo.pem

Check it worked

Bash
aws ec2 describe-instances --instance-ids "$INSTANCE_ID" --query 'Reservations[0].Instances[0].State.Name' --output text

The state is terminated. A terminated instance stays visible in the list for about an hour, and does not bill. Also check the console's EC2 pages in the same region for volumes and Elastic IP addresses you may have created by hand, since those keep billing after the instance is gone. This tutorial created none. Look at the Billing pages the next day to confirm that the charges have stopped.

Next steps

  • Make the login safer: /tutorials/harden-ssh-on-ubuntu-26-04.
  • Do the first-day setup: /tutorials/initial-server-setup-ubuntu-26-04.
  • Understand key-based login in general: /tutorials/log-in-to-a-server-with-ssh-keys.
  • AWS references: docs.aws.amazon.com/cli/latest/reference/ec2/run-instances.html and docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.