How to have a template attribute be optional

I am making AWS AMIs and I want to optinally supply values for the “ami_users” option for the amazon-ebs builder. I tried setting it to null by default, but that causes an error:

Build ‘amazon-ebs’ errored: Error modify AMI attributes: InvalidAMIAttributeItemValue: Invalid attribute item value " " for userId item type.

What’s the best way to do what I am trying to do?

Hi Mark,

Have you tried defining a user variable which has an empty string as the default value? For example:

{
  "variables": {
    "ami_users": ""
  },

  "builders": [{
    "type": "amazon-ebs",
    "ami_users": "{{user `ami_users`}}",
    // ...
  }]
}

I have not personally tested this template with that specific builder. Could you report back whether this is usable for you?

Same error whether ami_users is null or empty string.

Hello,

From my point of view, a missing feature in Packer is the possibility to have a default value for a variable.

On my side, I overcome this limitation by using a Makefile and environment variables to run packer but I plan to open a feature request to have support for default values.

@grimm26: it looks like this feature will be available in a future release of Packer using HCL syntax, see https://github.com/hashicorp/packer/issues/3457