boltops-tools / terraspace

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

improve autodetection for plugin expander for backend like remote #195

Closed tongueroo closed 2 years ago

tongueroo commented 2 years ago

This is a 🐞 bug fix. This is a 🙋‍♂️ feature or enhancement.

Summary

Fixes expander auto-detection bug with remote backend and a terraspace cloud plugin like aws. IE:

config/teraform/backend.tf

terraform {
  backend "remote" {
    organization = "<%= ENV['TFC_ORG'] %>" 
    workspaces {
      name = "<%= expansion('my-engine-:MOD_NAME-:ENV-:REGION-:ACCOUNT') %>"
    }
  }
}

When on-prem support was added, it changed the auto-detection to be only based what was parsed from config/terraform/backend.tf. So a remote backend would result in on-prem being detected, and the expansion variables would not be expanded correctly. Even though users would have terraspace_plugin_aws in their Gemfile and want to use the aws plugin expander, it would use the on-prem generic expander.

Before on-prem support, it was assumed that if the aws terraspace plugin was installed it would just use the aws plugin expander. That's why it previously worked.

But with on-prem support, the backend can be http, remote, etc and the terraspace plugin and its expander cannot be detected based on that.

Instead, this fix improves auto-detection so that Terraspace auto-detects based on:

  1. config.autodetect.expander - config setting that allows override of the auto-detection entirely.
  2. backend.tf parsing - will try to find the s3, azurerm, gcs backend and use the right terraspace plugin based on that.
  3. Gemfile: check what plugins are loaded in the Gemfile and auto-detect based on that.

3 will solve the issue for users who were on 0.6 and had everything working. So there is no need to configure #1. It should just work.

1 is provided as another configuration option if you need to override default auto-detection behavior.

Context

How to Test

Try a remote backend like so:

config/teraform/backend.tf

terraform {
  backend "remote" {
    organization = "<%= ENV['TFC_ORG'] %>" 
    workspaces {
      name = "<%= expansion('my-engine-:MOD_NAME-:ENV-:REGION-:ACCOUNT') %>"
    }
  }
}

Confirm your Gemfile has:

Gemfile:

gem "terraspace_plugin_aws"

Run

terraspace build

It should expand out the variables using the aws terrspace plugin expander. Example of something it'll look like:

$ cat .terraspace-cache/us-west-2/dev/stacks/demo/backend.tf
terraform {
  backend "remote" {
    organization = "boltops"
    workspaces {
      name = "demo-dev-us-west-2"
    }
  }
}

Version Changes

Patch

tongueroo commented 2 years ago

Released in 1.0.6