0
0
AWScloud~5 mins

Amazon Machine Images (AMIs) in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to create a virtual server in the cloud, you need a starting point with an operating system and software. Amazon Machine Images (AMIs) are like blueprints that let you launch these servers quickly with everything set up.
When you want to launch a new virtual server with a specific operating system and software pre-installed.
When you want to save the setup of a server to reuse it later or launch multiple identical servers.
When you want to create a backup of a server's current state to restore or clone it.
When you want to share a server setup with others in your team or organization.
When you want to customize a server image with your own software and settings for faster deployment.
Commands
This command lists the latest Amazon Linux 2 AMIs available from Amazon. It helps you find the Image ID to use when launching a server.
Terminal
aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn2-ami-hvm-2.0.????????-x86_64-gp2" --query 'Images[*].[ImageId,Name]' --output text | sort -k2
Expected OutputExpected
ami-0abcdef1234567890 amzn2-ami-hvm-2.0.20230314.0-x86_64-gp2 ami-0abcdef1234567891 amzn2-ami-hvm-2.0.20230405.0-x86_64-gp2 ami-0abcdef1234567892 amzn2-ami-hvm-2.0.20230510.0-x86_64-gp2
--owners - Filters images owned by Amazon
--filters - Filters images by name pattern
--query - Selects specific fields to display
This command launches a new EC2 instance using the selected AMI ID. It specifies the instance type, key pair for access, security group, and subnet.
Terminal
aws ec2 run-instances --image-id ami-0abcdef1234567892 --count 1 --instance-type t2.micro --key-name my-keypair --security-group-ids sg-0123456789abcdef0 --subnet-id subnet-0abc1234def567890
Expected OutputExpected
{ "Instances": [ { "InstanceId": "i-0123456789abcdef0", "ImageId": "ami-0abcdef1234567892", "InstanceType": "t2.micro", "State": { "Code": 0, "Name": "pending" } } ] }
--image-id - Specifies the AMI to use
--instance-type - Specifies the server size
--key-name - Specifies the SSH key for access
--security-group-ids - Specifies the security groups for the instance
--subnet-id - Specifies the subnet for the instance
This command checks the status and public IP of the launched instance to confirm it is running and accessible.
Terminal
aws ec2 describe-instances --instance-ids i-0123456789abcdef0 --query 'Reservations[*].Instances[*].[InstanceId,State.Name,PublicIpAddress]' --output table
Expected OutputExpected
--------------------------------------------- | DescribeInstances | +----------------------+--------------------+ | i-0123456789abcdef0 | running | | 54.123.45.67 | | ---------------------------------------------
--instance-ids - Specifies which instance to check
--query - Filters output fields
This command creates a new AMI from the running instance to save its current state as a reusable image.
Terminal
aws ec2 create-image --instance-id i-0123456789abcdef0 --name "my-server-backup-20240601" --no-reboot
Expected OutputExpected
{ "ImageId": "ami-0fedcba9876543210" }
--no-reboot - Prevents instance reboot during image creation
This command checks the status of the newly created AMI to ensure it is available for use.
Terminal
aws ec2 describe-images --image-ids ami-0fedcba9876543210 --query 'Images[*].[ImageId,Name,State]' --output text
Expected OutputExpected
ami-0fedcba9876543210 my-server-backup-20240601 available
--image-ids - Specifies the AMI to describe
Key Concept

If you remember nothing else from this pattern, remember: AMIs are reusable snapshots of server setups that let you launch identical servers quickly.

Common Mistakes
Using an incorrect or outdated AMI ID when launching an instance
The instance launch will fail or use an unexpected operating system
Always list and verify the latest AMI IDs before launching instances
Not specifying the correct key pair or security group when launching an instance
You may not be able to access the instance or it may be insecure
Specify a valid key pair and security group that allows your access
Creating an AMI without the --no-reboot flag and expecting no downtime
The instance will reboot, causing temporary downtime
Use --no-reboot if you want to avoid downtime but understand it may affect image consistency
Summary
List available AMIs to find the right image for your server.
Launch an EC2 instance using the chosen AMI with proper access and network settings.
Check the instance status and IP to confirm it is running.
Create a new AMI from a running instance to save its current setup.
Verify the new AMI is available before using it to launch more servers.