boltops-tools / terraspace

Terraspace: The Terraform Framework
https://terraspace.cloud
Apache License 2.0
678 stars 46 forks source link

[Documentation needed] Need help around using env variable for backend.tf for terraspace init #160

Closed yisyang closed 2 years ago

yisyang commented 2 years ago

The quick start documentations are pretty good about getting started with AWS and the like, likely due to the ability to use ~/.aws/credentials and vendor-related cli tools and env variables.

Here I'd like to use the http backend (specifically the Gitlab backend), and I've gotten this far.

config/terraform/backend.tf

  backend "http" {
    address = "<%= expansion('https://gitlab.com/api/v4/projects/[PROJECT_ID]/terraform/state/:ENV') %>"
    lock_address = "<%= expansion('https://gitlab.com/api/v4/projects/[PROJECT_ID]/terraform/state/:ENV/lock') %>"
    unlock_address = "<%= expansion('https://gitlab.com/api/v4/projects/[PROJECT_ID]/terraform/state/:ENV/lock') %>"
    username = "[GITLAB_USER]"
    password = "[GITLAB_PERSONAL_ACCESS_TOKEN]"
  }

Having all values populated, TS_ENV=prod terraspace init [STACK] works, but for obvious reasons I can't have the password field pre-populated here.

Once the password field is removed, I see the following error after running terraspace init:

2021/11/24 14:11:27 [DEBUG] GET https://gitlab.com/api/v4/projects/[PROJECT_ID]/terraform/state/prod
Error refreshing state: HTTP remote state endpoint requires auth
Error running command: terraform init -get -input=false

Official Gitlab documentations (for terraform) suggests calling init with -backend-config="password=[GITLAB_PAT]", but terraspace init doesn't seem to accept extra parameters:

ERROR: "terraspace init" was called with arguments ["lke", "-backend-config=password=123"]

Any clarity around this issue would be helpful.

tongueroo commented 2 years ago

Oh interesting. Haven’t tested the http backend yet. So don’t know if it works. Interested to find out. For env vars though:

<%= ENV[" GITLAB_USER"] %>
yisyang commented 2 years ago

@tongueroo Thank you for responding so quickly, and editing the responses too! Not to mention it's right before everyone take off for the holidays!

I'm sure your solution <%= ENV["VAR"] %> is enough for my use case storing init configs as masked vars on Gitlab, though unfortunately I'm blocked from testing at the moment.

In my hurry I tried to upgrade terraspace to the latest version to see if somehow that'd help. I used bundle update as the docs suggested, and ran into gem faraday 0.17.4 dependency issue that others have previous reported. Despite trying to uninstall/reinstall/try-diff-versions-of ruby w-and-w/o rbenv, and clearing cache dir and project dir and everything I can find, I've still not resolved this issue - the next thing to try is to probably have a dockerized env. Never seen issues this persistent before with npm or python.

Anyways, please don't reply this week. Enjoy your holidays!

tongueroo commented 2 years ago

Thanks for the holiday wishes! It’s no big deal. Got a little time right now.

For the dependencies issue. I dislike having to prepend “bundle exec” myself. Try a shim: https://terraspace.cloud/docs/misc/shim/

yisyang commented 2 years ago

Fresh gem install terraspace worked!

Also ran into https://github.com/boltops-tools/terraspace/issues/131, but found a very ugly workaround for the time being...

Tested and the "<%= ENV["FOO"] %>" works fine. 👍

backend.tf

terraform {
  backend "http" {
    address         = "<%= expansion('https://gitlab.com/api/v4/projects/31586394/terraform/state/:ENV') %>"
    lock_address    = "<%= expansion('https://gitlab.com/api/v4/projects/31586394/terraform/state/:ENV/lock') %>"
    unlock_address  = "<%= expansion('https://gitlab.com/api/v4/projects/31586394/terraform/state/:ENV/lock') %>"
    username        = "<%= ENV["CI_USER"] %>"
    password        = "<%= ENV["CI_PAT"] %>"
    lock_method     = "POST"            # <-- Required for Gitlab
    unlock_method   = "DELETE"
  }
}
tongueroo commented 2 years ago

Released Terraspace v1 https://community.boltops.com/t/terraspace-v1-release/803

GitLab http backend support has been added. Docs: https://terraspace.cloud/docs/config/backend/examples/gitlab/

RE: but terraspace init doesn't seem to accept extra parameters:

This has also been addressed in the v1 release. PR https://github.com/boltops-tools/terraspace/pull/172