Let's say I want to read $HOME in Terraform. I could create a variable called HOME and set it with TF_HOME, ie.,

TF_HOME=$HOME terraform apply

But is there a way to read $HOME directly in Terraform?

I know about pathexpand which solves this issue for HOME, I'm just wanting more information as to whether or not Terraform can access any environmental variable and used it as an example.

2 Answers 2


From Terraform's perspective, environment variables are just another kind of data from outside of Terraform and so if you want to read from them then you'd do so using a data source designed for that purpose.

Using data sources for external data means that there's a well-defined model for when Terraform will access the data and a guarantee that Terraform will access it only once per plan and apply, either during the plan phase if possible or during the apply phase if the data source configuration depends on information not available until the apply step.

There is not an official provider for accessing environment variables because in most cases a Terraform configuration should not directly depend on anything about the specific system where it's running -- a typical Terraform configuration is for managing remote objects via network APIs -- but there are some third party providers in the registry whose documentation suggests that they might give you what you need:

I have not directly used either so I cannot specifically recommend either of these. I suggest reviewing the documentation and the source code to see if they can do what you need.


Workaround (official solution)

You can not access the environmental variables through an official API. However, you can access the output of a script as a workaround. To access environmental variables external to the TF_ prefix, you must

  • Create a script to output the variable, you can wrap that output in JSON.
  • Run that script as an external data source
  • Read from the output of the script.

For more information on this approach check out,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.