Bird
Raised Fist0
AWScloud~5 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of an Amazon Machine Image (AMI)?
easy
A. To monitor server performance
B. To store user data in the cloud
C. To save a server setup so it can be reused later
D. To manage network traffic

Solution

  1. Step 1: Understand what an AMI represents

    An AMI is a snapshot of a server's setup including its software and settings.
  2. Step 2: Identify the main use of AMIs

    AMIs allow you to reuse this saved setup to launch new servers quickly.
  3. Final Answer:

    To save a server setup so it can be reused later -> Option C
  4. Quick Check:

    AMI = reusable server setup [OK]
Hint: AMI saves server setup for reuse [OK]
Common Mistakes:
  • Confusing AMI with data storage
  • Thinking AMI monitors performance
  • Assuming AMI manages network
2. Which AWS CLI command correctly creates an AMI from a running instance with ID i-1234567890abcdef0?
easy
A. aws ec2 start-image --instance i-1234567890abcdef0 --name MyServerImage
B. aws ec2 launch-image --instance-id i-1234567890abcdef0 --name MyServerImage
C. aws ec2 make-ami --id i-1234567890abcdef0 --image-name MyServerImage
D. aws ec2 create-image --instance-id i-1234567890abcdef0 --name MyServerImage

Solution

  1. Step 1: Identify the correct AWS CLI command for creating an AMI

    The correct command is aws ec2 create-image with the instance ID and a name.
  2. Step 2: Match the command syntax with the options

    aws ec2 create-image --instance-id i-1234567890abcdef0 --name MyServerImage uses the correct command and parameters.
  3. Final Answer:

    aws ec2 create-image --instance-id i-1234567890abcdef0 --name MyServerImage -> Option D
  4. Quick Check:

    create-image + instance-id = create AMI [OK]
Hint: Use 'create-image' with instance ID to make AMI [OK]
Common Mistakes:
  • Using wrong command like start-image or launch-image
  • Mixing up parameter names
  • Omitting instance ID
3. You run this AWS CLI command:
aws ec2 create-image --instance-id i-0abc123def456 --name TestImage
What will be the immediate result?
medium
A. An AMI creation request is started; image becomes available after processing
B. The instance is stopped and then an AMI is created
C. A new AMI is created and available instantly
D. The command fails because instance ID is invalid

Solution

  1. Step 1: Understand the behavior of create-image command

    The command starts the AMI creation process but the image is not instantly ready.
  2. Step 2: Identify the correct immediate result

    The AMI creation runs in background; the image becomes available after some time.
  3. Final Answer:

    An AMI creation request is started; image becomes available after processing -> Option A
  4. Quick Check:

    AMI creation is asynchronous [OK]
Hint: AMI creation takes time; not instant [OK]
Common Mistakes:
  • Assuming AMI is ready immediately
  • Thinking instance stops automatically
  • Believing command fails without error
4. You tried to create an AMI with this command:
aws ec2 create-image --instance i-0abc123def456 --name MyImage
But it failed. What is the error?
medium
A. Instance ID format is incorrect
B. Missing required parameter --instance-id
C. AMI name is invalid
D. You cannot create AMI from a running instance

Solution

  1. Step 1: Check the command parameters

    The command uses --instance instead of the required --instance-id parameter.
  2. Step 2: Identify the cause of failure

    The AWS CLI expects --instance-id to specify the instance; missing this causes failure.
  3. Final Answer:

    Missing required parameter --instance-id -> Option B
  4. Quick Check:

    Use --instance-id to specify instance [OK]
Hint: Use --instance-id, not --instance [OK]
Common Mistakes:
  • Using wrong parameter name
  • Assuming instance ID format error
  • Thinking AMI can't be made from running instance
5. You want to launch multiple identical servers quickly using an AMI. Which steps should you follow?
hard
A. Create an AMI from a configured instance, then launch new instances using that AMI
B. Launch new instances, then manually configure each one separately
C. Create snapshots of volumes, then attach them to new instances
D. Use AWS Lambda to copy instance settings to new servers

Solution

  1. Step 1: Understand how to reuse server setups

    Creating an AMI from a configured instance saves its setup for reuse.
  2. Step 2: Use the AMI to launch new instances

    Launching new servers from the AMI ensures they have the same software and settings quickly.
  3. Final Answer:

    Create an AMI from a configured instance, then launch new instances using that AMI -> Option A
  4. Quick Check:

    AMI enables fast identical server launches [OK]
Hint: Create AMI first, then launch servers from it [OK]
Common Mistakes:
  • Configuring each server manually
  • Using snapshots instead of AMIs for full setup
  • Thinking AWS Lambda copies server setups