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.83k stars 448 forks source link

CLI: HCL synth generates incorrect remote configuration with workspaces #3698

Open Hi-Fi opened 1 month ago

Hi-Fi commented 1 month ago

Expected Behavior

Terraform can be initialized with CDKTF synthesized stack in HCL format. Remote configuration would look like:

terraform {
  backend "remote" {
    organization = "test-organization"
    workspaces {
      name = "test-stack"
    }
    hostname = "test.artifactory.local"
  }
}

Actual Behavior

CDKTF synthesized incorrect remove configuration

terraform {
  backend "remote" {
    organization = "test-organization"
    workspaces = {
      name = "test-stack"
    }
    hostname = "test.artifactory.local"
  }
}

When manually removing that = after workspaces, init works fine.

Steps to Reproduce

  1. Initialize empty CDKTF project
  2. Add following remote backend to it
  3. Synthesize stack to HCL (cdktf synth --hcl)
  4. Go to output directory of stack and run terraform init

Versions

language: typescript cdktf-cli: 0.20.8 node: v20.13.1 cdktf: 0.20.8 constructs: 10.3.0 jsii: null terraform: 1.9.4 arch: arm64 os: darwin 23.6.0

Providers

No response

Gist

No response

Possible Solutions

Probably some specific handling needed for that part. Didn't had any other places with similar issues. Datasource to remote works different way and it requires that = sign.

Workarounds

Don't use HCL (not working for me as HCL is needed for Sonarqube analysis)

Anything Else?

No response

References

No response

Help Wanted

Community Note

mutahhir commented 1 month ago

Another alternative is to use the cloud block. (Docs), which is recommended by terraform anyway over the remote backend. From my understanding, that doesn't have this problem.

The remote backend is a bug nonetheless. We will be fixing it in a future release.

Hi-Fi commented 1 month ago

We use Artifactory as state backend, I understood that Cloud backend is reserved for HCP Terraform (ex-Terraform Cloud) (and it's Enterprise version) and remote is for that