hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.85k stars 450 forks source link

CloudBackend: support environment variables like TF_CLOUD_ORGANIZATION #3586

Open gmeligio opened 5 months ago

gmeligio commented 5 months ago

Expected Behavior

I expected cdktf to follow the cloud block documentation

TF_CLOUD_ORGANIZATION - The name of the organization. Terraform reads this variable when organization omitted from the cloud block`. If both are specified, the configuration takes precedence.

Hopefully the other environment variables mentioned in those docs can be supported as well. The current list is:

Actual Behavior

It turns out that organization is a required prop for CloudBackend.

Property 'organization' is missing in type '{ workspaces: NamedCloudWorkspace; }' but required in type 'CloudBackendConfig'.ts(2345)

Steps to Reproduce

  1. Create a CloudBackend.
  2. Don't declare the prop organization.

Versions

cdktf debug language: typescript cdktf-cli: 0.20.6 node: v20.10.0 cdktf: 0.20.6 constructs: 10.3.0 jsii: null terraform: 1.8.0 arch: x64 os: linux 6.5.0-27-generic providers @cdktf/provider-tfe (PREBUILT) terraform provider version: 0.53.0 prebuilt provider version: 11.2.0 cdktf version: ^0.20.0

Providers

──────────┬────────────┬──────────────┬──────────────┬───────────┐ ───── ────── ─── ──── ─────── ───── Provider │ Provider │ CDKTF Constra│ Package Name │ Package │ Name Version int Version ──────────┼────────────┼──────────────┼──────────────┼───────────┤ ───── ────── ─── ──── ─────── ───── tfe │ 0.53.0 │ ^0.20 │ @cdktf/provid│ 11.2.0 │ .0 er-tfe ──────────┴────────────┴──────────────┴──────────────┴───────────┘ ───── ────── ─── ──── ─────── ─────

Gist

No response

Possible Solutions

  1. Set fields that are supported to be passed as environment variables to optional in the TypeScript declaration.

Workarounds

  1. Set organization to "hardcoded-name-of-organization".

Anything Else?

No response

References

Help Wanted

Community Note

ansgarm commented 5 months ago

Hi @gmeligio 👋

This probably stems from the times when the CDKTF CLI would create workspaces for you and thus required these to be set.

In the meantime you can use an override to empty the cloud block:

const stack = new MyStack(app, "test");
new CloudBackend(stack, {organization: "dummy", workspaces: new NamedCloudWorkspace("dummy")});
stack.addOverride("terraform.cloud", {organization: undefined, workspaces: undefined});

will result in:

// ...
"terraform": {
    "cloud": {
    }
  }
// ...
gmeligio commented 5 months ago

Hi @ansgarm . Thank you so much for providing a workaround! I'll use it.