HCL2 Environment Variables

I am trying to use an env var AWS_REGION but not too sure how to do it using HCL2. I am looking at the guide here https://www.packer.io/guides/hcl/variables/

I am tried using “${var.PKR_VAR_AWS_REGION}” and PKR_VAR_AWS_REGION in my hcl script, but it doesn’t seem to recognize the variable. Can someone help?

Hi @brianpham thanks for reaching out. The use of environment variables with HCL can be a little tricky because they are referenced differently from what they are named. More specifically, the PKR_VAR prefix is needed to tell Packer that it is an environment variable it should know about, but when you reference the variable you have to drop the PKR_VAR prefix. Below is an example HCL file to illustrate the usage.

variable "AWS_REGION" {                                                                                                                            
  type = string                                                                                                                                    
}                                                                                                                                                  
                                                                                                                                                   
source "null" "nullbuilder" {                                                                                                                      
  communicator = "none"                                                                                                                            
}                                                                                                                                                  
                                                                                                                                                   
build {                                                                                                                                            
  sources = ["source.null.nullbuilder"]                                                                                                            
  provisioner "shell-local" {                                                                                                                      
    inline = ["echo ${var.AWS_REGION}"]                                                                                                            
  }                                                                                                                                                
}

To set the AWS_REGION variable within the HCL file you can then run (using Shell env variable reference)

PKR_VAR_AWS_REGION=$AWS_REGION packer build template.pkr.hcl

Please let me know if this helps you move forward with the build or if you have any other questions. In the meantime, I’ll see about adding a few more examples to the HCL variables documentation. Cheers!

1 Like

It would be really nice to have the ‘env’ functionality back in HCL2. having to pass environment variables this way is a pain and the idea of storing things like secrets in flat text files isn’t great, especially when you’re using AWS STS. It reminds me of early Terraform with TF_VAR for your creds before it supported AWS_PROFILE.

1 Like

Agreed, this syntax to add prefix and then remove prefix is ridiculous.

We heard your feedback and added an HCL “env” function – https://github.com/hashicorp/packer/pull/10240 it’ll be available in the next release, and in the next “nightly” build

1 Like

Hi @SwampDragons.
Could you tell me, Is it possible at this moment to use ‘env’ function with the default value (if env var is absent)? Can env var have another than PKR_VAR_<SOME_VAR> to override the default value in the hcl file?
Many thanks in advance.

I think that should be possible, and if it isn’t you could probably use locals and a conditional for that.

Good idea. I’ve tried but without success. Maybe you have some code snippets with the examples? It could help very much.