-1

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: ************
knowledge20
  • 1,006
  • 3
  • 14
  • 25

2 Answers2

1

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
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • This is Packer and not TF, so you may want to modify e.g. "modern TF" --> "HCL2". – Matthew Schuchard Nov 02 '21 at 10:33
  • 1
    It worked but when we deploy the template with the jenkins it gives another error packer validate template_abc.json.pkr.hcl Failed to parse template: Error parsing JSON: invalid character 'v' looking for beginning of value At line 1, column 1 (offset 1): 1: v – knowledge20 Nov 02 '21 at 19:07
  • the starting of code is a variable definition. I am not sure if packer validates the hcl2 template or not. @Marcin – knowledge20 Nov 02 '21 at 19:09
  • @MattSchuchard Posted another question https://stackoverflow.com/questions/69815969/packer-unable-to-validate-the-hcl2-template-in-jenkins-job. Can u pls help – knowledge20 Nov 02 '21 at 19:29
  • @Marcin Pls. help with https://stackoverflow.com/questions/69861082/data-block-not-supported-with-packer-version-1-6-1-in-hcl2-templates – knowledge20 Nov 06 '21 at 02:50
  • @MattSchuchard posted question https://stackoverflow.com/questions/69861082/data-block-not-supported-with-packer-version-1-6-1-in-hcl2-templates – knowledge20 Nov 06 '21 at 02:50
0

Ensure that the AMI ID is in the same region as what was specified in the script

Slycreator
  • 1,144
  • 11
  • 18