Getting an Environment Variable in Terraform configuration?

后端 未结 4 1441
被撕碎了的回忆
被撕碎了的回忆 2020-12-31 02:09

I have two environment variables. One is TF_VAR_UN and another is TF_VAR_PW. Then I have a terraform file that looks like this.

res         


        
相关标签:
4条回答
  • 2020-12-31 02:15

    The use of interpolation syntax throws warning with terraform v0.12.18. Now you don't need to use the interpolation syntax. You can just reference it as var.hello.

    Caution : One important thing to understand from a language standpoint is that, you cannot declare variables using environment variables. You can only assign values for declared variables in the script using environment varibles. For example, let's say you have the following .tf script

    variable "hello" { type=string }

    Now if the environment has a variable TF_VAR_hello="foobar", during runtime the variable hello will have the value "foobar". If you assign the variable without the declaration of the variable there will not be any effect.

    0 讨论(0)
  • 2020-12-31 02:19

    Most of the providers use:

    DefaultFunc: schema.EnvDefaultFunc("

    • https://github.com/terraform-providers/terraform-provider-infoblox/blob/master/infoblox/provider.go
    • https://github.com/terraform-providers/terraform-provider-openstack/blob/master/openstack/provider.go
    • ...
    0 讨论(0)
  • 2020-12-31 02:20

    in order to use a variable it needs to be wrapped with "" for example:

    username = "${var.UN}"

    0 讨论(0)
  • 2020-12-31 02:27

    I would try something more like this, which seems closer to the documentation.

    variable "UN" {}
    variable "PW" {}
    
    resource "google_container_cluster" "primary" {
    name = "marcellus-wallace"
    zone = "us-central1-a"
    initial_node_count = 3
    
    master_auth {
        username = "${var.UN}"
        password = "${var.PW}"
    }
    
    node_config {
        oauth_scopes = [
            "https://www.googleapis.com/auth/compute",
            "https://www.googleapis.com/auth/devstorage.read_only",
            "https://www.googleapis.com/auth/logging.write",
            "https://www.googleapis.com/auth/monitoring"
        ]
    }
    

    With the CLI command being the below.

    TF_VAR_UN=foo TF_VAR_PW=bar terraform apply
    
    0 讨论(0)
提交回复
热议问题