<h1>Workaround (official solution)</h1>
<p>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 <code>TF_</code> prefix, you must</p>
<ul>
<li>Create a script to output the variable, you can wrap that output in JSON.</li>
<li>Run that script as an <a href="https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/data_source" rel="nofollow noreferrer">external data source</a></li>
<li>Read from the output of the script.</li>
</ul>
<p>For more information on this approach check out,</p>
<ul>
<li><a href="https://support.hashicorp.com/hc/en-us/articles/4547786359571-Reading-and-using-environment-variables-in-Terraform-runs" rel="nofollow noreferrer">https://support.hashicorp.com/hc/en-us/articles/4547786359571-Reading-and-using-environment-variables-in-Terraform-runs</a></li>
</ul>

CC BY-SA 4.0
2023-02-01T16:42:41.773Z
2023-02-01T16:42:41.773Z
Evan Carroll
<p>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 <a href="https://developer.hashicorp.com/terraform/language/data-sources" rel="nofollow noreferrer">data source</a> designed for that purpose.</p>
<p>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.</p>
<p>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:</p>
<ul>
<li><a href="https://registry.terraform.io/providers/EppO/environment/latest/docs/data-sources/variables" rel="nofollow noreferrer"><code>environment_variables</code></a> from the provider <code>EppO/environment</code>.</li>
<li><a href="https://registry.terraform.io/providers/MorganPeat/environment/latest/docs/data-sources/variable" rel="nofollow noreferrer"><code>environment_variable</code></a> from the provider <code>MorganPeat/environment</code>.</li>
</ul>
<p>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.</p>

CC BY-SA 4.0
2023-02-01T19:35:15.730Z
2023-02-01T19:35:15.730Z
Martin Atkins
<p>Let's say I want to read <code>$HOME</code> in Terraform. I could create a variable called <code>HOME</code> and set it with <code>TF_HOME</code>, ie.,</p>
<pre><code>TF_HOME=$HOME terraform apply
</code></pre>
<p>But is there a way to read <code>$HOME</code> directly in Terraform?</p>
<hr />
<p>I know about <a href="https://developer.hashicorp.com/terraform/language/functions/pathexpand" rel="nofollow noreferrer"><code>pathexpand</code></a> which solves this issue for <code>HOME</code>, I'm just wanting more information as to whether or not Terraform can access any environmental variable and used it as an example.</p>

<terraform>
How can I use non-TF environmental variables in Terraform?
Evan Carroll
# How can I use non-TF environmental variables in Terraform?

(Asked by: username_0 on 2023-02-01)

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.

**Tags:** terraform


---

## 2 answers

### Answer by username_0 on 2023-02-01. Score: 0 | Accepted answer

# 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](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/data_source)
* Read from the output of the script.

For more information on this approach check out,

* <https://support.hashicorp.com/hc/en-us/articles/4547786359571-Reading-and-using-environment-variables-in-Terraform-runs>


---

### Answer by username_1 on 2023-02-01. Score: 1

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](https://developer.hashicorp.com/terraform/language/data-sources) 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:

* [`environment_variables`](https://registry.terraform.io/providers/EppO/environment/latest/docs/data-sources/variables) from the provider `EppO/environment`.
* [`environment_variable`](https://registry.terraform.io/providers/MorganPeat/environment/latest/docs/data-sources/variable) from the provider `MorganPeat/environment`.

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.


---

