'Cannot include the given value in a string template: string required

Does someone have experience in building a Packer ec2 with Terraform and can assist me? Basically, I am getting this error for a "null_resource" that represent Packer

   │ Error: Invalid template interpolation value
│
│   on asg.tf line 14, in resource "null_resource" "packer":
│    7:     command = <<EOF
│    8: packer build \
│    9:   -var region=${var.region} \
│   10:   -var vpc_id=${module.vpc.vpc_id} \
│   11:   -var subnet_id="${module.vpc.private_subnets[0]}" \
│   12:   -var instance_type=${var.instance_type} \
│   13:   -var ami_name=${var.ami_name} \
│   14:   -var source_ami=${data.aws_ami.search} \
│   15:   -var image_path=${local.image_complete_path} \
│   16:   template.json
│   17: EOF
│     ├────────────────
│     │ data.aws_ami.search is object with 34 attributes
│
│ Cannot include the given value in a string template: string required.

Would love some help here, thank you all!



Solution 1:[1]

The error message is pretty expressive:

data.aws_ami.search is object with 34 attributes

You need a single value, so you need a way to filter the attributes.

As per Terraform documentation for the aws_ami data source [1], you probably want the name attribute so the value you should look for is:

data.aws_ami.search.name

If you need the AMI ID, then you should use:

data.aws_ami.search.id

This is the same as using data.aws_ami.search.image_id [2].


[1] https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami#name

[2] https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami#image_id

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marko E