Can you pass environment variables directly to provider binary?

Hello! Imagine my awesome terraform provider:

package main

import "os"

func main() {
	value := os.Getenv("I_NEED_THIS_VALUE")

i would like to send that variable in my configuration file:

resource "" "" {
  env {
    I_NEED_THIS_VALUE = "yes!"

Is there any built-in way to do that?

I can’t pass it directly like:

$ I_NEED_THIS_VALUE="yes!" terraform apply
$ terraform apply

I would like to do that from terraform configuration .tf file.

Hi @bukowa,

The environment variables passed to a provider plugin are always those inherited from the Terraform process itself. There is no facility to set them differently for a particular plugin process.

The fact that most providers run as child processes of Terraform Core is an implementation detail from the perspective of the main language, so there are no language features which directly expose that implementation detail.

The supported mechanism for per-instance provider configuration is the provider configuration settings. Terraform will only call the configure function on your provider once per process, so it is okay in principle for your configure function to call os.Setenv to override your process’s own environment based on the provider configuration, as long as whatever is using this environment variable doesn’t run until after the provider is configured.

1 Like

Ok, is there any chance that it could be implemented?

I’m a bit confused about what you are asking for.

Are you asking if an env var can be read within a provider? If so, it can with the value coming from the environment where you run Terraform from. So you can adjust that environment by using export or similar.

Are you asking how configuration can be passed to a provider (not specifically env vars)? If so your provider can define parameters that can be specified in the provider block, set to whatever you want (within the *.tf files).

Are you asking if it is possible to get or set env vars withing the *.tf files? If so, then no as HCL doesn’t directly interact with env vars - you can have a variable pick up its value from an env var (with a specifically formatted name) if wanted.

Could you give a bit more detail on what you are trying to achieve?

No, I don’t expect this would ever be implemented. The provider configuration is the only supported way to vary provider behavior based on settings in the Terraform configuration.

Ok, thank you both <20chars>