This error occurs when Terraform attempts to launch an EC2 instance using an AMI ID that doesn't exist in the specified AWS region. The most common cause is using an AMI ID from a different region, as AMI IDs are region-specific.
The InvalidAMIID.NotFound error indicates that AWS cannot find the specified Amazon Machine Image (AMI) ID in the configured region. AMI IDs are unique identifiers for virtual machine templates, and each one is tied to a specific AWS region. When you change your Terraform configuration to use a different region or specify an incorrect AMI ID, AWS will reject the request because the image doesn't exist in that location.
Check your Terraform configuration or environment for the configured AWS region. Look for:
provider "aws" {
region = "us-west-2"
}Or check your environment: echo $AWS_REGION
Log into the AWS EC2 Console, switch to the same region as your Terraform config, and search for the AMI ID in "Images" > "AMIs". If the AMI doesn't appear, it's not available in that region.
Replace hardcoded AMI IDs with the aws_ami data source, which automatically selects the correct AMI for your region:
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
}If you require a particular AMI, go to the AWS EC2 Console, find the correct AMI ID in your target region, and update your Terraform code:
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0" # Updated for your region
instance_type = "t3.micro"
}If you're working with aws_launch_configuration and the AMI has been deleted, you may need to remove it from state:
terraform state rm aws_launch_configuration.example
terraform applyThis will cause Terraform to recreate the launch configuration with the new AMI.
AMI IDs follow the format ami-XXXXXXXX and are unique per region. When using Terraform across multiple environments or regions, the aws_ami data source is strongly recommended as it handles region differences transparently. Some organizations use custom AMIs stored in their own AWS accounts; ensure your IAM role has ec2:DescribeImages permissions to access these. For deleted AMIs in terraform state, you can bypass the error temporarily with 'terraform state rm' but should update your configuration to reference valid AMIs. Consider using AWS Systems Manager Parameter Store to centralize AMI ID management across your infrastructure.
Error: Error installing helm release: cannot re-use a name that is still in use
How to fix "release name in use" error in Terraform with Helm
Error: Error creating GKE Cluster: BadRequest
BadRequest error creating GKE cluster in Terraform
Error: External program failed to produce valid JSON
External program failed to produce valid JSON
Error: Unsupported argument in child module call
How to fix "Unsupported argument in child module call" in Terraform
Error: network is unreachable
How to fix "network is unreachable" in Terraform