Home > Enterprise >  Packer Error while creating ami using hcl2: "Error querying AMI: InvalidAMIID.Malformed: Invali
Packer Error while creating ami using hcl2: "Error querying AMI: InvalidAMIID.Malformed: Invali

Time:11-03

I am working to build the packer pipeline which would use the market place ami to install certain softwares and create an ami. I had created the json template which are working fine but as per the packer recommendation, I am working to upgrade it to the hcl2 template.
When i run the hcl2_upgrade command. I see a the json template is converted to the .pkr.hcl template but while running it. I have done some customization to template as per the recommded in packer documentation.It gives me below error.

data "amazon-ami" "autogenerated_1"{
  access_key = "${var.aws_access_key}"
  filters = {
    root-device-type    = "ebs"
    virtualization-type = "hvm"
    name                = "**** Linux *"
  }
  most_recent = true
  region      = "${var.aws_region}" 
  owners      = ["${var.owner_id}"]
  secret_key  = "${var.aws_secret_key}"
}

when I am trying to consume this ami id in the source block It gives me error.
  ami_name                    = "${var.ami_name}"
  associate_public_ip_address = false
  force_deregister            = true
  iam_instance_profile        = "abc"
  instance_type               = "****"
  region                      = "${var.aws_region}"
  source_ami    = "{data.amazon-ami.autogenerated_1.id}"
  ssh_interface = "private_ip"
  ssh_username  = "user"
  subnet_id     = "subnet-********"
  vpc_id        = "vpc-***********"
}

Error details are below:

amazon-ebs.pqr_ami:     status code: 400, request id: *********
Build 'amazon-ebs.pqr_ami' errored after 1 second 49 milliseconds: Error querying AMI: InvalidAMIID.Malformed: Invalid id: "{data.amazon-ami.autogenerated_1.id}" (expecting "ami-...")
    status code: 400, request id: ************

CodePudding user response:

Your AMI is literally a string source_ami = "{data.amazon-ami.autogenerated_1.id}". It should be:

source_ami    = "${data.amazon-ami.autogenerated_1.id}"

or for HCL2:

source_ami    = data.amazon-ami.autogenerated_1.id
  • Related