cdktf synth --hcl generates invalid remote backend config with "workspaces = " instead of a block
Fixes the HCL synth bug where cdktf synth --hcl emits workspaces as an attribute (workspaces = { name = ... }) instead of a block, which makes terraform init fail. Covers the manual workaround of removing the equals sign. Use when terraform init fails on HCL generated by cdktf synth --hcl with a remote backend. Not for JSON synth output.
TL;DR
After cdktf synth --hcl, edit the generated backend config: change workspaces = { to workspaces { (remove the =). Then terraform init will accept it.
The error
cdktf synth --hcl generates:
workspaces = {
name = "test-stack"
}terraform init fails because workspaces must be a block, not an attribute:
workspaces {
name = "test-stack"
}Fix it
- Run
cdktf synth --hclas usual. - Open the generated
.tfbackend configuration. - Change
workspaces = {toworkspaces {(delete the equals sign). - Run
terraform initand confirm it initializes.
Expected result: init succeeds against the remote backend.
When to use this
- You use
cdktf synth --hclwith a remote backend using workspaces terraform initfails on the generated backend config
When NOT to use this
- You use the default JSON synth (unaffected)
- The backend uses
prefixinstead of workspaces (different syntax)
Root cause
The HCL generator does not distinguish block syntax from attribute syntax for workspaces, emitting it as an attribute assignment. Terraform's backend schema requires the block form, so init rejects the generated file. The JSON synth path is unaffected because JSON has no block/attribute distinction.
Edge cases
- Re-synth overwrites your edit; script the sed replacement if you synth often:
sed -i 's/workspaces = {/workspaces {/' [file].
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.