TerraformPilot

Troubleshooting

Fix Terraform Error - Invalid AMI ID NotFound

Fix the Terraform InvalidAMIID.NotFound error. Covers region-specific AMIs, data source lookups, deregistered images, and cross-account AMI sharing.

LLuca Berton2 min read

Quick Answer

#

The AMI ID doesn't exist in the target region. AMIs are region-specific — an AMI ID from us-east-1 won't work in eu-west-1. Use a data source to dynamically look up the latest AMI, or verify the AMI exists with aws ec2 describe-images.

The Error

#
Error: creating EC2 Instance: InvalidAMIID.NotFound:
  The image id '[ami-0abcdef1234567890]' does not exist
Error: Error launching source instance: InvalidAMIID.Malformed:
  Invalid id: "ami-12345"

What Causes This Error

#

1. AMI Is in a Different Region

#

AMIs are region-specific. The same Ubuntu AMI has different IDs in every region.

2. AMI Was Deregistered

#

AWS or the AMI owner removed the image. Older AMIs get deregistered when new versions are released.

3. Hardcoded AMI ID

#

Using a static AMI ID that became invalid after a region change or AMI deregistration.

4. AMI Not Shared

#

The AMI exists but belongs to another account and hasn't been shared with yours.

5. Typo in AMI ID

#

AMI IDs follow the pattern ami- followed by 17 hex characters. A wrong character returns NotFound.

How to Fix It

# #
data "aws_ami" "ubuntu" {
  most_recent = true
  owners      = ["099720109477"]  # Canonical
 
  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
  }
 
  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}
 
resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id  # Always valid
  instance_type = "t3.micro"
}

Solution 2: Use SSM Parameter for Latest AMI

#
# AWS maintains SSM parameters with latest AMI IDs
data "aws_ssm_parameter" "al2023" {
  name = "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64"
}
 
resource "aws_instance" "web" {
  ami           = data.aws_ssm_parameter.al2023.value
  instance_type = "t3.micro"
}

Solution 3: Verify AMI with CLI

#
# Check if AMI exists in your region
aws ec2 describe-images --image-ids ami-0abcdef1234567890 --region us-east-1
 
# Find the latest Ubuntu AMI in your region
aws ec2 describe-images --owners 099720109477 \
  --filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-jammy*amd64*" \
  --query 'Images | sort_by(@, &CreationDate) | [-1].[ImageId,Name]' \
  --output text --region us-east-1
 
# Check if AMI is shared with your account
aws ec2 describe-images --executable-users self --image-ids ami-xxx

Solution 4: Use AMI Map for Multi-Region

#
variable "ami_map" {
  type = map(string)
  default = {
    us-east-1 = "ami-0abcdef1234567890"
    us-west-2 = "ami-0fedcba0987654321"
    eu-west-1 = "ami-0111222333444555a"
  }
}
 
data "aws_region" "current" {}
 
resource "aws_instance" "web" {
  ami           = var.ami_map[data.aws_region.current.name]
  instance_type = "t3.micro"
}

Troubleshooting Checklist

#
  1. ✅ Is the AMI ID in the correct format? (ami- + 17 hex chars)
  2. ✅ Does it exist in your target region? (aws ec2 describe-images)
  3. ✅ Has the AMI been deregistered?
  4. ✅ Is it shared with your account?
  5. ✅ Can you switch to a data source lookup instead of a hardcoded ID?

Prevention Tips

#
  • Never hardcode AMI IDs — use data sources or SSM parameters
  • Use most_recent = true with owners to always get the latest valid AMI
  • Pin AMI IDs in production with a variable and update them through your change process
  • Test AMI availability in CI before deploying to new regions
#

Conclusion

#

InvalidAMIID.NotFound means the AMI doesn't exist in your region, was deregistered, or isn't shared with your account. Replace hardcoded AMI IDs with data source lookups or SSM parameters to automatically get the latest valid AMI for any region.

#aws#ami#ec2

Share this article