Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Manually set terraform env variable prevents template creation #1359

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
Tracked by #1939
mafredri opened this issue May 10, 2022 · 4 comments Β· Fixed by #2264
Closed
Tracked by #1939

Manually set terraform env variable prevents template creation #1359

mafredri opened this issue May 10, 2022 · 4 comments Β· Fixed by #2264
Assignees
Labels
api Area: HTTP API
Milestone

Comments

@mafredri
Copy link
Member

mafredri commented May 10, 2022

OS Information

  • OS: Linux
  • coder --version: Coder v0.5.5-devel+2d3dc43 Tue May 10 07:44:09 UTC 2022

Steps to Reproduce

  1. Set a terraform environment variable: export TF_LOG=1
  2. Run coder server
  3. Create first template: coder templates create docker-local -d ~/src/cdr/coder/examples/docker-local

There should be no issue creating the template, or detailed hints on how to fix the problem (e.g. coder server is running with these environment variables set, please restart it without).

Actual

Template creation fails:

> Create and upload "~/src/cdr/coder/examples/docker-local"? (yes/no) yes
βœ” Queued [139ms]
βœ” Setting up [6ms]
βœ” Parse parameters [17ms]
✘ Detecting persistent resources [420ms]
template import provision for start: recv import provision: set terraform env: manual setting of env var "TF_LOG" detected

Notes

Considering this comment I wanted to see if I can get more information out of terraform (a naive approach). I think it's fine the value can't be changed but I would have expected this value to either propagate to terraform or for the server to either inform of a way to fix it or filter out the variable.

We could for instance filter out some incompatible TF_ env vars here, potentially logging the exclusion as a warning:

terraformEnv := map[string]string{}
// Required for "terraform init" to find "git" to
// clone Terraform modules.
for _, env := range os.Environ() {
parts := strings.SplitN(env, "=", 2)
if len(parts) < 2 {
continue
}
terraformEnv[parts[0]] = parts[1]
}
// Only Linux reliably works with the Terraform plugin
// cache directory. It's unknown why this is.
if t.cachePath != "" && runtime.GOOS == "linux" {
terraformEnv["TF_PLUGIN_CACHE_DIR"] = t.cachePath
}
err = terraform.SetEnv(terraformEnv)
if err != nil {
return xerrors.Errorf("set terraform env: %w", err)
}

AC

Replace the terraform exec command with directly executing terraform
Test for the steps to repro this bug to validate this case is covered
Expose in the logs that the env vars are set

@mafredri mafredri changed the title Bug: Manually set terraform env variable prevent template creation Bug: Manually set terraform env variable prevents template creation May 10, 2022
@kylecarbs
Copy link
Member

Ahh we should probably not use terraform-exec. I've found it to be quite janky in edge cases like this!

@tjcran
Copy link

tjcran commented May 16, 2022

@kylecarbs @mafredri how detrimental is this? It seems like we should definitely make this change, but that it's probably okay to punt until after launch. LMK if you feel otherwise and I'll reconsider.

@johnstcn
Copy link
Member

Note that if you are using the embedded Terraform version you will get this message instead:

> Create and upload "/tmp/docker-local"? (yes/no) 
βœ” Queued [469ms]
βœ” Setting up [1ms]
βœ” Adding README.md... [0ms]
βœ” Parse parameters [1ms]
✘ Detecting persistent resources [511ms]
template import provision for start: recv import provision: get terraform version: signal: killed

@kylecarbs kylecarbs changed the title Bug: Manually set terraform env variable prevents template creation Manually set terraform env variable prevents template creation Jun 7, 2022
@Emyrk
Copy link
Member

Emyrk commented Jun 7, 2022

The tfexec library has a Clean function to remove these env vars:

https://github.com/hashicorp/terraform-exec/blob/main/tfexec/cmd.go#L82-L89

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Area: HTTP API
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants