Skip to content

Support TF_VAR_ default from environment #4529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dcarrion87 opened this issue Oct 13, 2022 · 12 comments
Closed

Support TF_VAR_ default from environment #4529

dcarrion87 opened this issue Oct 13, 2022 · 12 comments

Comments

@dcarrion87
Copy link
Contributor

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?

@matifali
Copy link
Member

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

@dcarrion87
Copy link
Contributor Author

@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!

@bpmct
Copy link
Member

bpmct commented Oct 13, 2022

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.

@dcarrion87
Copy link
Contributor Author

@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"

@matifali
Copy link
Member

@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

@bpmct
Copy link
Member

bpmct commented Oct 13, 2022

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

@dcarrion87
Copy link
Contributor Author

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

By the way, the proposed UI inputs look great!

@dcarrion87
Copy link
Contributor Author

@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
}

@github-actions
Copy link

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.

@github-actions github-actions bot added the stale This issue is like stale bread. label Dec 14, 2022
@dcarrion87
Copy link
Contributor Author

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

@kylecarbs
Copy link
Member

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

@dcarrion87
Copy link
Contributor Author

Cool beans. I guess OK to close off then.

@github-actions github-actions bot removed the stale This issue is like stale bread. label Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants