Terraform: Error launching source instance: InvalidAMIID.Malformed

Viewed 10466

Going through terraform tutorial I stumbled upon this error.

Error: Error launching source instance: InvalidAMIID.NotFound: The image id '[ami-830c94e3]' does not exist
    status code: 400, request id: 4c3e0252-c3a5-471e-8b57-3f6e349628af

This is my code. The only change that I did was was region change from us-west-2 to eu-central-1

provider "aws" {
  profile = "default"
  region = "eu-central-1"
}

resource "aws_instance" "example" {
  ami = "ami-830c94e3"
  instance_type = "t2.micro"
}
5 Answers

It was simple. Apparently, AMI for Amazon Images of each region is different. I had to copy the AMI of the image that was present in my region. For example ami-07dfba995513840b5 is the id for Red Hat Enterprise Linux 8 (HVM), SSD Volume Type in eu-central-1 region. Go to AWS console, click EC2 from all services list, next click launch instance and find the AMI of an image of your interest.

Instead of hardcoding AMI IDs you should consider using the aws_ami data source instead.

This allows you to more easily specify the type of AMI you want and have Terraform automatically use that AMI, including the option to have it automatically update the AMI in use when newer AMIs matching your criteria are available. It will also make it easier to manage using the same AMI in different regions as the AMI ID is different for each region it is copied to.

The aws_instance resource documentation has a good example of using the most recent Ubuntu 20.04 AMI published by Canonical in the 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"

  tags = {
    Name = "HelloWorld"
  }
}

If you run the following in different regions it will automatically use the correct Ubuntu 20.04 AMI for the region. It will also recreate the instance with the latest AMI when a newer Ubuntu 20.04 AMI is published by Canonical.

Make sure that you the Zone is registered for you in which you are creating instance. Verify it: EC2 Dashboard -> Service Health -> Zones

Zone name Zone ID us-east-2a use2-az1 us-east-2b use2-az2 us-east-2c use2-az3

I had a similar error when trying to create an Ubuntu 20.04 AWS EC2 instance using Terraform.

I was running into this error when I run the terraform apply command:

Error: Error launching source instance: InvalidAMIID.Malformed: Invalid id: "data.aws_ami.ubuntu.id" (expecting "ami-...") status code: 400, request id: 9cb0ddbc-1f5e-43e7-bef2-541832aa002e

My code looks like this:

provider "aws" {
  region = "us-east-1"
}

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" "ec2" {
  ami = "data.aws_ami.ubuntu.id"
  instance_type = "t2.micro"

  tags = {
    Name = "HelloWorld"
  }
}

Here's how I fixed it:

The issue was that I put data.aws_ami.ubuntu.id in quotes which is a call/invocation operation of the data function::

resource "aws_instance" "ec2" {
  ami = "data.aws_ami.ubuntu.id"
  instance_type = "t2.micro"

I had to remove the quotes from data.aws_ami.ubuntu.id:

resource "aws_instance" "web" {
  ami = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"

So my code looked like this afterwards:

provider "aws" {
  region = "us-east-1"
}

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" "ec2" {
  ami = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"

  tags = {
    Name = "HelloWorld"
  }
}

This time when I ran terraform apply command,it printed the correct ami id for the ubuntu 20.04 aws ec2 instance in my specified region:

data.aws_ami.ubuntu: Refreshing state... [id=ami-0885b1f6bd170450c]

and then created the aws instance resource.

Note: The specified name for the resource which is ec2 can be of any value of your choice. You can name it web or whatever name you desire:

resource "aws_instance" "web" {
  ami = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"

That's all.

I hope this helps

Just ran into this issue last night. Make sure your provider region, in your .tf file, matches your AWS Management console region. My provider region was "us-east-1", but the region in the Management Console was "us-east-2".

Related