Sub topic 3
Choosing an AMI
An Amazon Machine Image (AMI) is the template EC2 uses to create an instance's root volume and launch settings. Choosing the right one decides which operating system you run, who patches it, and how repeatable your launches are. This chapter assumes you can use the AWS CLI, know basic Linux administration, and understand that an instance runs in one Region and one Availability Zone.
What an AMI contains
An AMI bundles four things:
- A snapshot (or template) of the root volume, holding the operating system and any preinstalled software.
- Launch permissions that control which AWS accounts can use the image.
- A block device mapping that lists the volumes to attach at launch.
- Metadata such as architecture, virtualization type, boot mode, and root device type.
When you launch an instance, EC2 copies the AMI's root volume contents onto a new volume for that instance. Changes you make afterward do not alter the AMI.
Regional scope and identifiers
An AMI ID looks like ami-0123456789abcdef0. AMIs are Regional. The same image has a different ID in each Region, and an ID from us-east-1 is invalid in eu-west-1. If your script hard-codes an AMI ID, it breaks when you move Regions and eventually launches an outdated image.
Where AMIs come from
| Source | Examples | Trade-off |
|---|---|---|
| AWS-provided | Amazon Linux 2023, Windows Server | Maintained by AWS and well integrated with EC2 tooling. |
| AWS Marketplace | Vendor-hardened Linux, security appliances | Convenient and may carry software charges. Review the vendor and pricing. |
| Community AMIs | Images shared publicly by other accounts | Unvetted. Anyone can publish one. |
| Your own | Images you build from a configured instance | Full control, and you must maintain them. |
Treat community AMIs as untrusted unless you can verify the publisher. An image can contain backdoors, stale packages, or credentials. For production, prefer AWS-provided images, vetted Marketplace images, or images you build yourself.
Criteria for choosing
Check each of these before you pick an image:
- Operating system and support. Pick a distribution your team can patch and operate. Amazon Linux 2023 uses
dnfandsystemdand receives updates from AWS. - Architecture.
x86_64images run on Intel and AMD instance types.arm64images run on Graviton types such ast4g. An architecture mismatch causes the launch to fail. - Root device type. Most current AMIs are EBS-backed, so the root volume persists across stop and start. This matters for the lifecycle chapter.
- Boot mode and virtualization. Current AMIs use HVM. Some newer instance types require UEFI-capable images.
- Licensing. Windows and some Linux distributions include license charges in the hourly price.
- Freshness. An old image means more patches to apply after first boot.
Finding AMIs with the CLI
You can search for AMIs with describe-images. This command lists the newest Amazon-owned Amazon Linux 2023 x86_64 images:
aws ec2 describe-images \
--owners amazon \
--filters "Name=name,Values=al2023-ami-2023*" \
"Name=architecture,Values=x86_64" \
"Name=state,Values=available" \
--query 'sort_by(Images,&CreationDate)[-3:].[ImageId,Name,CreationDate]' \
--output tableThe --owners filter is a security control. Without it, the search can return community images that match the name pattern. Always pin the owner to an account you trust.
Using SSM parameters for the latest image
Hard-coding IDs is fragile. AWS publishes public Systems Manager (SSM) parameters that always point to the current recommended AMI in each Region. The launch example from the plan resolves one at run time:
AMI_ID=$(aws ssm get-parameter \
--name /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 \
--query Parameter.Value --output text)
aws ec2 run-instances \
--image-id "$AMI_ID" \
--instance-type t3.micro \
--key-name my-key \
--security-group-ids sg-0123456789abcdef0 \
--subnet-id subnet-0123456789abcdef0 \
--metadata-options HttpTokens=required \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=tutorial-web}]'The parameter path contains the architecture, so an arm64 launch needs the arm64 variant of the path. The call runs in your configured Region and returns the ID valid there.
Building your own AMI
Once you have configured an instance, you can capture it as an image:
aws ec2 create-image \
--instance-id i-0123456789abcdef0 \
--name "web-golden-2026-11-26" \
--description "nginx baseline" \
--tag-specifications 'ResourceType=image,Tags=[{Key=Name,Value=web-golden}]'By default EC2 reboots the instance to get a consistent file system. Adding --no-reboot avoids the interruption, but file system integrity is then not guaranteed. The call also creates EBS snapshots that back the image, and these incur storage charges.
Remove secrets, SSH keys, shell history, and logs from an instance before you capture it. Anyone who can launch your AMI can read what is baked into it.
Image vs. bootstrapping
You have two ways to get software onto a new instance:
- Install at boot with user data. The image stays generic and the setup is easy to read and change. Launches are slower, and they depend on package repositories being reachable.
- Bake into an AMI. Launches are fast and consistent. You must rebuild the image to change software.
A common approach is to bake slow, stable components (runtime, agents, hardening) into the AMI and use user data for small, per-environment settings.
Sharing, copying, and cleaning up
An AMI is private by default. You can share it with specific accounts by modifying its launch permissions, and you can copy it across Regions:
aws ec2 copy-image \
--source-image-id ami-0123456789abcdef0 \
--source-region us-east-1 \
--region eu-west-1 \
--name "web-golden-2026-11-26"Run the copy command against the destination Region. To retire an AMI, you must deregister it, then delete its snapshots separately. Deregistering alone leaves the snapshots, and you keep paying for them.
aws ec2 deregister-image --image-id ami-0123456789abcdef0
aws ec2 delete-snapshot --snapshot-id snap-0123456789abcdef0Common errors
InvalidAMIID.NotFound: the ID belongs to a different Region, or the image was deregistered.Unsupportedor architecture errors: anarm64image was paired with anx86_64instance type, or the reverse.AuthFailureorOptInRequiredon a Marketplace image: your account has not subscribed to the product.- Unexpected charges after deleting an AMI: its snapshots still exist.
Exercise
A teammate's script launches instances with a hard-coded AMI ID from us-east-1. It now fails in eu-west-1 with InvalidAMIID.NotFound. Give two fixes, and name one trade-off of each.
Show answer
Fix 1: resolve the AMI at launch time from the public SSM parameter in the target Region. Trade-off: you always get the newest image, so launches are not reproducible over time. Fix 2: copy your own AMI to eu-west-1 with copy-image and use the new ID there. Trade-off: you must maintain one AMI per Region, copy it again after each rebuild, and pay for the extra snapshot storage.
Exercise
You deregister an AMI you built last month. Why can the bill still include charges for it, and what do you do?
Show answer
Deregistering removes only the image registration. The EBS snapshots that back the image remain and are billed for storage. Find the snapshots (for example, by the AMI ID in their description) and remove them with delete-snapshot.