Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HCL templates cannot call functions for variable defaults #9430

Closed
jen20 opened this issue Jun 16, 2020 · 3 comments
Closed

HCL templates cannot call functions for variable defaults #9430

jen20 opened this issue Jun 16, 2020 · 3 comments
Labels
core Core components of Packer hcl2 question

Comments

@jen20
Copy link
Collaborator

jen20 commented Jun 16, 2020

Overview of the Issue

Using the new HCL template format, there is no way to define a variable which calls a function, which is a regression from the JSON template format.

This is useful when determining a consistent value for (as an example) the time at which an image was built to be used in both the AMI name and also an AMI and snapshot tag.

In the JSON syntax, the following works:

	"variables": {
		"buildtime": "{{isotime \"2006-01-02-1504\"}}"
	},

The buildtime variable can then be interpolated at other points in the template.

Reproduction Steps

In an HCL template:

variable "buildtime" {
	default = formatdate("YYYY-MM-DD-hhmm", timestamp())
}

# Rest of the template
$ packer validate template.pkr.hcl
Error: Function calls not allowed

  on template.pkr.hcl line 2, in variable "buildtime":
   2: 	default = formatdate("YYYY-MM-DD-hhmm", timestamp())

Functions may not be called here.

Packer version

Packer v1.6.0

Operating system and Environment details

macOS 10.15.5, Packer v1.6.0, AWS

@jen20 jen20 added the bug label Jun 16, 2020
@jen20 jen20 changed the title HCL templates cannot call functions in variables HCL templates cannot call functions for variable defaults Jun 16, 2020
jen20 added a commit to jen20/packer-ubuntu-zfs that referenced this issue Jun 16, 2020
Packer does not yet support defaults in variables, so it's not possible
to get a consistent timestamp to use as part of the AMI and a tag. Since
AMI name must be unique, it's better to put the build timestamp there
rather than in a tag.

This bug is tracked upstream as hashicorp/packer#9430.
@nywilken nywilken added hcl2 core Core components of Packer labels Jun 16, 2020
@nywilken
Copy link
Member

Hi @jen20 thanks for bubbling this up. There is work being done to improve support for HCL2 variables that may already have this in mind. Linking to #9176 for visibility. Thanks again!

@azr
Copy link
Contributor

azr commented Jun 17, 2020

Hey @jen20, quoting this message here hashicorp/terraform#17229 (comment)

Input variables are constant values [...] and so they cannot contain interpolations or other expressions that do not yield a constant value. [...]
This separation makes it clear what is expected to be provided by the caller vs. what is generated privately [...].

For this purpose I would recommend instead using a local variable:

locals {
  packerstarttime = formatdate("YYYY-MM-DD-hhmm", timestamp())
  # Also here I believe naming this variable `buildtime` could lead to 
  # confusion mainly because this is evaluated a 'parsing-time'.
}

I'm going to go ahead and close this issue, please do not hesitate asking more questions, thanks for trying Packer HCL2 !!

@azr azr closed this as completed Jun 17, 2020
@azr azr added question and removed bug labels Jun 17, 2020
@ghost
Copy link

ghost commented Jul 18, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jul 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
core Core components of Packer hcl2 question
Projects
None yet
Development

No branches or pull requests

3 participants