301 — Launch an EC2 Instance

Beginner

Find the Amazon Linux 2023 AMI for your region, launch a t2.micro instance with run-instances, wait for running state, and terminate — all from the CLI.

Learning Objectives

1
Find an Amazon Linux 2023 AMI ID for the active region using the CLI
2
Create an EC2 key pair and save the private key with correct permissions
3
Launch a t2.micro EC2 instance using aws ec2 run-instances
4
Monitor instance state until it reaches running using aws ec2 wait
5
Retrieve the public IP address of the running instance
6
Terminate the instance and verify it enters the terminated state
Step 1

Find the latest Amazon Linux 2023 AMI

Query EC2 for the most recent Amazon Linux 2023 AMI in your active region. AMI IDs are region-specific — the ID that works in us-east-1 will not work in eu-west-1.

Commands to Run

AMI_ID=$(aws ec2 describe-images \
  --owners amazon \
  --filters "Name=name,Values=al2023-ami-2023*" \
  "Name=architecture,Values=x86_64" \
  --query "Images | sort_by(@, &CreationDate) | [-1].ImageId" \
  --output text)
echo "AMI ID: $AMI_ID"

What This Does

Amazon Linux 2023 AMIs are published by Amazon (owner ID amazon).

The filter on al2023-ami-2023* scopes results to standard AL2023 images — the 2023 after the prefix matters, because a looser al2023-ami-* pattern would also match al2023-ami-minimal-* (stripped-down images without common tools).

The x86_64 filter excludes ARM images. sort_by(@, &CreationDate) | [-1] selects the newest image.

The result is stored in a shell variable so every subsequent command can reference it without retyping the long ID.

Expected Outcome

AMI ID: ami-0abcdef1234567890

The exact ID will differ by region and change as Amazon publishes new AMIs.

Any value starting with ami- is correct.

Pro Tips

  • 1
    AMI IDs change when Amazon releases a new build. Always query for the latest rather than hardcoding an ID — hardcoded IDs go stale and run-instances will return InvalidAMIID.NotFound.
  • 2
    AWS also publishes the latest AL2023 AMI ID as a public SSM parameter — a one-liner alternative to the describe-images query: AMI_ID=$(aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 --query "Parameters[0].Value" --output text)
  • 3
    To also see the AMI name and creation date: aws ec2 describe-images --owners amazon --filters "Name=name,Values=al2023-ami-2023*" "Name=architecture,Values=x86_64" --query "Images | sort_by(@, &CreationDate) | [-1].{ID:ImageId,Name:Name,Created:CreationDate}" --output table
  • 4
    Ubuntu users: the equivalent query uses --filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-*-22.04-amd64-server-*" --owners 099720109477

Common Mistakes to Avoid

  • āš ļøOmitting --output text — without it, the AWS CLI returns a quoted JSON string like "ami-0abcdef1234567890", which makes the value unusable in shell variable assignments.
  • āš ļøRunning the query without setting your default region. If AWS_DEFAULT_REGION is unset and no --region flag is passed, the CLI uses the region in ~/.aws/config. Verify with: aws configure get region
Was this step helpful?

All Steps (0 / 11 completed)