Lukasz Dynowski
Lukasz Dynowski

Reputation: 13570

Terraform: Error launching source instance: InvalidAMIID.Malformed

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"
}

Upvotes: 20

Views: 31218

Answers (9)

Suraj Moon
Suraj Moon

Reputation: 11

Cross check the AMI with the region you are taking AMI from. AMI versions varies with regions and version.

Upvotes: 1

aleb
aleb

Reputation: 2552

You can get a list of available Amazon EC2 images like this:

$ aws ec2 describe-images

Note it returns the images in your default region!

To get the details about specific images:

$ aws --region eu-central-1 ec2 describe-images --include-deprecated --image-ids ami-002c3687c157a3668 ami-0154a0c4f87fc839d

Upvotes: 0

Sarthak Raval
Sarthak Raval

Reputation: 1291

check your Amazon console region sometime is default selected differently.

As per region, your AMI ID is different

Upvotes: 3

Naveen
Naveen

Reputation: 1

check aws credentails using aws configure command, check wheater secretkey,accesskey and region are matching with your configuration code. if the region is different try changing the region based on where ami is presen

Upvotes: 0

hi_xavier
hi_xavier

Reputation: 185

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".

Upvotes: 4

Lukasz Dynowski
Lukasz Dynowski

Reputation: 13570

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.

Upvotes: 48

Dharni Singh
Dharni Singh

Reputation: 41

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

Upvotes: 3

Promise Preston
Promise Preston

Reputation: 28800

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

Upvotes: 0

ydaetskcoR
ydaetskcoR

Reputation: 56839

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.

Upvotes: 13

Related Questions