Skip to content

How do I identify EC2 instances with Amazon Linux 1 AMIs in my account?

4 minute read
Content level: Foundational
0

I need to identify the Elastic Compute Cloud (EC2) instances across regions in my account that were launched with an Amazon Linux 1 Amazon Machine Image (AMI).

Short description:

This guide explains how to use an AWS CLI script in CloudShell to identify EC2 instances that were launched with Amazon Linux 1 AMIs. The Amazon Linux AMI (also called Amazon Linux 1) reached its end of life on December 31, 2023. The Amazon Linux AMI and operating system will not receive any security updates or bug fixes starting January 1, 2024. Customers may want to identify the instances in their account across regions. This script will provide the:

  • EC2 Instance ID
  • AWS AMI ID
  • Instance running state
  • Availability Zone

Resolution:

Customers can leverage the following steps and script to identify instances that were launched with an Amazon Linux 1 AMI.

Note: This script works for Amazon Linux 1 and works for all regions within a single account. The script does not traverse accounts.

Complete the following steps:

  1. Open the AWS Console for the account that you would like to investigate. For these purposes, the region does not matter.
  2. In the AWS Console, open CloudShell.
  3. Paste this script into the CloudShell environment:
for REGION in $(aws ec2 describe-regions --query 'Regions[].RegionName' --output text); do
  aws ec2 describe-instances --region "$REGION" \
    --query 'Reservations[].Instances[].[InstanceId,ImageId,State.Name,Placement.AvailabilityZone]' \
    --output text | while read ID AMI STATE AZ; do
      [ -z "$ID" ] && continue
      NAME=$(aws ec2 describe-images --region "$REGION" --image-ids "$AMI" \
              --query 'Images[0].Name' --output text 2>/dev/null)
      case "$NAME" in
        amzn-ami-*) echo -e "AL1\t$REGION\t$ID\t$STATE\t$AZ\t$AMI\t$NAME" ;;
      esac
    done
done
  1. Press Enter to run the script. The script execution time depends on the number of regions and instances in your account.

Note: The script parses for AMIs based on pattern matching. AMIs with the matching naming convention will be identified. To verify the OS use the command uname -r or cat /etc/os-release. Please also consider that custom AMIs created from an Amazon Linux 1 base may not be identified based on this script.

Understanding the script output

The script performs the following actions:

  1. Iterates through all AWS Regions in your account
  2. Describes all EC2 instances in each region
  3. Retrieves the AMI name for each instance
  4. Filters instances that match the Amazon Linux 1 naming pattern (amzn-ami-*)
  5. Outputs a tab-separated list with the following information:
    • Amazon Linux version identifier (AL1)
    • AWS Region
    • Instance ID
    • Instance state (running, stopped, etc.)
    • Availability Zone
    • AMI ID
    • AMI name

The output appears in the following format:

AL1    us-east-1    i-12345678900000000    running    us-east-1a    ami-abcdef0000000000    amzn-ami-hvm-2018.03.0.20181129-x86_64-gp2

Identifying Amazon Linux 2 and Amazon Linux 2023 AMIs

Amazon Linux AMIs follow specific naming conventions:

  • Amazon Linux 1 AMIs: amzn-ami-*
  • Amazon Linux 2 AMIs: amzn2-ami-*
  • Amazon Linux 2023 AMIs: al2023-ami-*

Adapt the script to identify Amazon Linux 2 or Amazon Linux 2023 instances by modifying the pattern matching line:

amzn-ami-*) echo -e "AL1\t$REGION\t$ID\t$STATE\t$AZ\t$AMI\t$NAME" ;;

Replace amzn-ami-* with the appropriate AMI wildcard pattern:

  • For Amazon Linux 2: amzn2-ami-*
  • For Amazon Linux 2023: al2023-ami-*

Also update the descriptor AL1 to match the AMI version you're searching for (for example, AL2 or AL2023).

Example for Amazon Linux 2:

amzn2-ami-*) echo -e "AL2\t$REGION\t$ID\t$STATE\t$AZ\t$AMI\t$NAME" ;;

Additional considerations

Multi-account environments: This script operates within a single AWS account. If you manage multiple AWS accounts, you need to run the script separately in each account. Consider using AWS Organizations with AWS Systems Manager to automate inventory collection across multiple accounts.

Alternative approaches: Also use AWS Systems Manager Inventory to collect metadata about your instances, including AMI information. Systems Manager Inventory provides a centralized view of your instance metadata and supports querying across multiple accounts when configured with AWS Organizations.

Performance: The script queries each region sequentially. In accounts with many regions and instances, the script takes several minutes to complete. The script output appears progressively as each region completes processing.

Related information

AWS
EXPERT

published 8 days ago122 views