Skip to content

Support TF_VAR_ default from environment #4529

Closed

Description

@dcarrion87

We want TF_VAR_ variables that are assigned to the Coder instance to always apply to some variables in the template.

At the moment it seems that coder insists on asking and overriding these when uploading from CLI.

Any thoughts on this?

Activity

  1. matifali commented on Oct 13, 2022

    @matifali
    Contributor

    you can fix them in main.tf and it will not ask

  2. dcarrion87 commented on Oct 13, 2022

    @dcarrion87
    ContributorAuthor

    @matifali can you clarify what you mean by "fix them". I was trying combinations and could not get it to not ask / throw an error. I'm sure it's a simple fix and I'm just not getting it. Thanks!

  3. bpmct commented on Oct 13, 2022

    @bpmct
    Member

    We're reworking some of our parameter behavior in #4311 and plan on slowly deprecating the prompts for Terrraform variables.

    In the meantime, you can avoid user prompts by setting sensitive = true and default = "value". I believe the TF_VAR_ variables will be respected as long as they are set before the Coder server is running.

  4. dcarrion87 commented on Oct 13, 2022

    @dcarrion87
    ContributorAuthor

    @bpmct is that in a particular version?

    If I do as you suggest it detects a default value as well as the environment variable...So far so good:

    ✔ Queued [809ms]
    ✔ Setting up [61ms]
    ✔ Adding README.md... [63ms]
    ✔ Parsing template parameters [140ms]
    ⧗  Detecting persistent resources 
      terraform environment variable: TF_VAR_namespace=<value redacted>
      terraform environment variable: TF_VAR_venture=<value redacted>
      Terraform 1.3.0
      data.coder_workspace.me: Refreshing...
      data.coder_workspace.me: Refresh complete after 0s [id=7995ccd3-0294-40e2-b446-02659c94f692]
      coder_agent.main: Plan to create
    

    But when I start a workspace it's not honouring the environment variable. Note how it's just used value instead of my environment variable.

    terraform environment variable: TF_VAR_namespace=<value redacted>
    terraform environment variable: TF_VAR_venture=<value redacted>
    Initializing the backend...
    Initializing provider plugins...
    ...
    ...
    Error: serviceaccounts is forbidden: User "system:serviceaccount:REDACTED:coder" cannot create resource "serviceaccounts" in API group "" in the namespace "value"
    Error: configmaps is forbidden: User "system:serviceaccount:REDACTED:coder" cannot create resource "configmaps" in API group "" in the namespace "value"
    
  5. matifali commented on Oct 13, 2022

    @matifali
    Contributor

    @matifali can you clarify what you mean by "fix them". I was trying combinations and could not get it to not ask / throw an error. I'm sure it's a simple fix and I'm just not getting it. Thanks!

    I think I misunderstood your question and I thought it is related to some terraform variables so I said fix/initialize them in main.tf

  6. bpmct commented on Oct 13, 2022

    @bpmct
    Member

    Ah gotcha. I guess the default value is consumed instead of the TF_VAR_ value.

    @kylecarbs any idea if we could work around this before the new parameter stuff is in and variables are only consumable via Terraform?

    The only thing I can think of is using an external data source instead of using variables: https://support.hashicorp.com/hc/en-us/articles/4547786359571-Reading-and-using-environment-variables-in-Terraform-runs

  7. dcarrion87 commented on Oct 13, 2022

    @dcarrion87
    ContributorAuthor

    Thanks @bpmct I will look at a workaround like that for the moment.

    By the way, the proposed UI inputs look great!

  8. dcarrion87 commented on Oct 14, 2022

    @dcarrion87
    ContributorAuthor

    @bpmct this worked a charm and is an acceptable workaround for us for now:

    data "external" "env" {
      program = ["sh", "-c",
        <<-EOF
        cat <<-EOT
        {
        "venture": "$TF_VAR_venture",
        "namespace": "$TF_VAR_namespace"
        }
        EOT
    EOF
      ]
    }
    
    locals {
      namespace      = data.external.env.result.namespace
      venture        = data.external.env.result.venture
    }
    
  9. github-actions commented on Dec 14, 2022

    @github-actions
    Contributor

    This issue is becoming stale. In order to keep the tracker readable and actionable, I'm going close to this issue in 7 days if there isn't more activity.

  10. dcarrion87 commented on Dec 14, 2022

    @dcarrion87
    ContributorAuthor

    We're still doing this hack (if you'd call it that).

  11. kylecarbs commented on Dec 14, 2022

    @kylecarbs
    Member

    I think it's fine to do this! Once we have the new parameter system, this won't be a thing anyways!

  12. dcarrion87 commented on Dec 14, 2022

    @dcarrion87
    ContributorAuthor

    Cool beans. I guess OK to close off then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions