Passing SourceAMI into script

Im trying to pass the result of source_ami_filter (the ami id) as a var into a script that is later run in one of my providers. I cant seem to get anything to work correctly. Other vars are being passed as I would expect except this one.

My filter:

"source_ami_filter": {
            "filters": {
                "virtualization-type": "hvm",
                "name": "*ubuntu/images/hvm-ssd/ubuntu-*-{{user `ubuntu_version`}}-amd64-server*",
                "root-device-type": "ebs"
            },
            "owners": [
                "099720109477"
            ],
            "most_recent": true
        }

I have tried adding "vendorAMIid={{.SourceAMI}}" in my environment_vars for the provisioner. Ive tried adding this in my inline before I kick off the bash script. Ive also tried

{{build `SourceAMI`}}

But that wont even validate correctly…

$ packer validate base_ami_ubuntu_001_partition.json
Error: Failed to prepare build: "amazon-ebs"

render 'inline': template: root:1:14: executing "root" at <build `SourceAMI`>:
error calling build: loaded data, but couldnt find SourceAMI in it. in:

vendorAMIid={{build `SourceAMI`}}

What am I missing here??
Im using Packer 1.6.0

Hi there, this sounds like a bug and possibly some documentation that needs to be clarified.

There is currently a difference between special template engines, which only work in the builder configuration portion of the template, and the “build” function, which works in provisioners. Currently, the AWS builder only has one special “build” function value: SourceAMIName. But it has several variables that can be used inside the builder but not in the provisioners: https://www.packer.io/docs/builders/amazon-ebs#build-template-data

We’ll make sure those lists match to make your user experience better. Right now, the closest you’ll get is:

  "provisioners": [
    {
        "type": "shell-local",
        "inline": ["echo Packer source AMI is '{{ build `SourceAMIName` }}'"]
    }
  ]

Ahhh…gotcha. Thanks