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

Unable to set variable from environment variable #13123

Closed
DarrenDotmatics opened this issue Jul 25, 2024 · 3 comments
Closed

Unable to set variable from environment variable #13123

DarrenDotmatics opened this issue Jul 25, 2024 · 3 comments
Labels

Comments

@DarrenDotmatics
Copy link

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Overview of the Issue

Following documentation here https://developer.hashicorp.com/packer/docs/templates/hcl_templates/functions/contextual/env to try to set a variable with the value of a local environment variable.

Eventually found this discussion https://discuss.hashicorp.com/t/hcl2-environment-variables/9290 which did work, but is not what is in the documentation.

Reproduction Steps

Run the template using PKR_VAR_AWS_REGION="eu-west-1" PACKER_LOG=1 packer build ./
Out put should be:
null.nullbuilder: [eu-west-1] [] []

Packer version

Packer v1.11.1

Simplified Packer Template

variable "AWS_REGION" {
type = string
}

variable "test_aws_region" {
type = string
default = env("AWS_REGION")
}

variable "test_aws_region2" {
type = string
default = "${env("AWS_REGION")}"
}

source "null" "nullbuilder" {
communicator = "none"
}

build {
sources = ["source.null.nullbuilder"]
provisioner "shell-local" {
inline = ["echo [${var.AWS_REGION}] [${var.test_aws_region}] [${var.test_aws_region2}]"]
}
}

Operating system and Environment details

MacOs(x64) and also ran in hashicorp/packer docker container on docker desktop.

Log Fragments and crash.log files

https://gist.github.com/DarrenDotmatics/6b42ea10b55ef8ef478d1c432d5ad2b0

@lbajolet-hashicorp
Copy link
Contributor

Hi @DarrenDotmatics,

This seems like a usage error in this case? If the variable is undefined, the env("AWS_REGION") will be empty, which I suspect is what happens here.

If you define AWS_REGION in your environment, that does what you'd expect

$ PKR_VAR_AWS_REGION="eu-west-1" AWS_REGION="us-east-1" packer build ./
null.nullbuilder: output will be in this color.

==> null.nullbuilder: Running local shell script: /tmp/packer-shell2326167547
    null.nullbuilder: [eu-west-1] [us-east-1] [us-east-1]
Build 'null.nullbuilder' finished after 3 milliseconds 989 microseconds.

==> Wait completed after 4 milliseconds 6 microseconds

==> Builds finished. The artifacts of successful builds are:
--> null.nullbuilder: Did not export anything. This is the null builder

@DarrenDotmatics
Copy link
Author

OK thank you.
Not sure why this was not working in my production script, will have to look at my script again.

Copy link

github-actions bot commented Sep 3, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants