# HCL2 Environment Variables

**URL:** <https://discuss.hashicorp.com/t/hcl2-environment-variables/9290>\
**Category:** Packer\
**Created:** [May 27, 2020, 11:47pm UTC](https://discuss.hashicorp.com/t/hcl2-environment-variables/9290 "2020-05-27T23:47:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianpham](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/brianpham/32/6599_2.png) [@brianpham](https://discuss.hashicorp.com/u/brianpham)\
**Post date:** [May 27, 2020, 11:47pm UTC](https://discuss.hashicorp.com/t/hcl2-environment-variables/9290/1 "2020-05-27T23:47:28Z")

</div>

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/](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?

---

<div class="post-metadata">

**Author:** ![nywilken](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/nywilken/32/2500_2.png) [@nywilken](https://discuss.hashicorp.com/u/nywilken)\
**Post date:** [May 28, 2020, 7:36pm UTC](https://discuss.hashicorp.com/t/hcl2-environment-variables/9290/2 "2020-05-28T19:36:39Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![TheM0ng00se](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/them0ng00se/32/14092_2.png) [@TheM0ng00se](https://discuss.hashicorp.com/u/TheM0ng00se)\
**Post date:** [October 30, 2020, 12:47am UTC](https://discuss.hashicorp.com/t/hcl2-environment-variables/9290/3 "2020-10-30T00:47:21Z")

</div>

> [@nywilken](#):
>
> `KR_VAR`

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.

---

<div class="post-metadata">

**Author:** ![kidbrax](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/kidbrax/32/14587_2.png) [@kidbrax](https://discuss.hashicorp.com/u/kidbrax)\
**Post date:** [November 12, 2020, 2:01am UTC](https://discuss.hashicorp.com/t/hcl2-environment-variables/9290/4 "2020-11-12T02:01:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SwampDragons](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/swampdragons/32/1296_2.png) [@SwampDragons](https://discuss.hashicorp.com/u/SwampDragons)\
**Post date:** [November 16, 2020, 6:40pm UTC](https://discuss.hashicorp.com/t/hcl2-environment-variables/9290/5 "2020-11-16T18:40:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fireman777](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/fireman777/32/15892_2.png) [@fireman777](https://discuss.hashicorp.com/u/fireman777)\
**Post date:** [December 17, 2020, 7:32pm UTC](https://discuss.hashicorp.com/t/hcl2-environment-variables/9290/6 "2020-12-17T19:32:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SwampDragons](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/swampdragons/32/1296_2.png) [@SwampDragons](https://discuss.hashicorp.com/u/SwampDragons)\
**Post date:** [December 17, 2020, 9:15pm UTC](https://discuss.hashicorp.com/t/hcl2-environment-variables/9290/7 "2020-12-17T21:15:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fireman777](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/fireman777/32/15892_2.png) [@fireman777](https://discuss.hashicorp.com/u/fireman777)\
**Post date:** [December 21, 2020, 8:19pm UTC](https://discuss.hashicorp.com/t/hcl2-environment-variables/9290/8 "2020-12-21T20:19:26Z")

</div>

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