terraform-cdk icon indicating copy to clipboard operation
terraform-cdk copied to clipboard

cdktf: computed object key leads to broken terraform code

Open elruwen opened this issue 5 months ago • 2 comments

Expected Behavior

Given the following short example:

import {Construct} from "constructs";
import {App, Fn, TerraformStack} from "cdktf";
import {File} from "@cdktf/provider-local/lib/file";
import {LocalProvider} from "@cdktf/provider-local/lib/provider";

class MyStack extends TerraformStack {
    constructor(scope: Construct, id: string) {
        super(scope, id);


        new LocalProvider(this, "localprovider")

        new File(this, "ruwentest", {
            filename: "ruwentest.json",
            content: Fn.jsonencode({
                    [Fn.join("", ["foo", "bar"])]: "baz"
                }
            )
        })

    }
}

I would expect to have a json file

{"foobar":"baz"}

since the Fn.join function should join foo and bar together.

The relevant line in the cdk json should look like this:

"content": "${jsonencode({join(\"\", [\"foo\", \"bar\"]) = \"baz\"})}",

Actual Behavior

When I run cdktf synth, it is successful. But the generated json looks like this:

{
  "resource": {
    "local_file": {
      "ruwentest": {
        "//": {
          "metadata": {
            "path": "cdktf/ruwentest",
            "uniqueId": "ruwentest"
          }
        },
        "content": "${jsonencode({\"join(\"\", [\"foo\", \"bar\"])\" = undefined})}",
        "filename": "ruwentest.json"
      }
    }
  }
}

When I run it with --hcl, it is also broken, but slightly worse:

resource "local_file" "ruwentest" {
  content  = "${jsonencode({ "join(" ", [" foo ", " bar "])" = undefined })}"
  filename = "ruwentest.json"
}

Steps to Reproduce

  1. Create a new typescript project.
  2. Make sure you got the local provider installed.
  3. Make this the main.ts
import {Construct} from "constructs";
import {App, Fn, TerraformStack} from "cdktf";
import {File} from "@cdktf/provider-local/lib/file";
import {LocalProvider} from "@cdktf/provider-local/lib/provider";

class MyStack extends TerraformStack {
    constructor(scope: Construct, id: string) {
        super(scope, id);


        new LocalProvider(this, "localprovider")

        new File(this, "ruwentest", {
            filename: "ruwentest.json",
            content: Fn.jsonencode({
                    [Fn.join("", ["foo", "bar"])]: "baz"
                }
            )
        })

    }
}

const app = new App();
new MyStack(app, "cdktf");
app.synth();

Versions

language: typescript cdktf-cli: 0.20.4 node: v18.15.0 cdktf: 0.20.4 constructs: 10.3.0 jsii: null terraform: 1.7.2 arch: x64 os: linux 6.1.0-17-amd64 providers @cdktf/provider-aws (PREBUILT) terraform provider version: 5.33.0 prebuilt provider version: 19.2.0 cdktf version: ^0.20.0 @cdktf/provider-kubernetes (PREBUILT) terraform provider version: 2.25.2 prebuilt provider version: 11.0.0 cdktf version: ^0.20.0 @cdktf/provider-local (PREBUILT) terraform provider version: 2.4.1 prebuilt provider version: 10.0.0 cdktf version: ^0.20.0

Providers

┌───────────────┬──────────────────┬─────────┬────────────┬────────────────────────────┬─────────────────┐ │ Provider Name │ Provider Version │ CDKTF │ Constraint │ Package Name │ Package Version │ ├───────────────┼──────────────────┼─────────┼────────────┼────────────────────────────┼─────────────────┤ │ aws │ 5.33.0 │ ^0.20.0 │ │ @cdktf/provider-aws │ 19.2.0 │ ├───────────────┼──────────────────┼─────────┼────────────┼────────────────────────────┼─────────────────┤ │ kubernetes │ 2.25.2 │ ^0.20.0 │ │ @cdktf/provider-kubernetes │ 11.0.0 │ ├───────────────┼──────────────────┼─────────┼────────────┼────────────────────────────┼─────────────────┤ │ local │ 2.4.1 │ ^0.20.0 │ │ @cdktf/provider-local │ 10.0.0 │ └───────────────┴──────────────────┴─────────┴────────────┴────────────────────────────┴─────────────────┘

Gist

No response

Possible Solutions

No response

Workarounds

No response

Anything Else?

No response

References

No response

Help Wanted

  • [ ] I'm interested in contributing a fix myself

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

elruwen avatar Feb 27 '24 11:02 elruwen

I would expect to have a json file

{"foobar":"baz"}

That expectation is off, by using Fn.join you are invoking the terraform function join that you can see rendered in your output. If you want foobar you need to use the programming language join, so e.g. ["foo", "bar"].join("").

DanielMSchmidt avatar Mar 04 '24 08:03 DanielMSchmidt

Argh I realised I actually didn't past the error message :man_facepalming:

Let me put the steps again:

  1. On the above typescript code, run terraform synth
  2. cd cdktf.out/stacks/cdktf
  3. cat cdk.tf.json { "//": { "metadata": { "backend": "local", "stackName": "cdktf", "version": "0.20.4" }, "outputs": { } }, "provider": { "local": [ { } ] }, "resource": { "local_file": { "ruwentest": { "//": { "metadata": { "path": "cdktf/ruwentest", "uniqueId": "ruwentest" } }, "content": "${jsonencode({\"join(\"\", [\"foo\", \"bar\"])\" = undefined})}", "filename": "ruwentest.json" } } }, "terraform": { "backend": { "local": { "path": "xxx/cdktf/terraform.cdktf.tfstate" } }, "required_providers": { "local": { "source": "hashicorp/local", "version": "2.4.1" } } } } (See the undefined there?)
  4. terraform apply ╷ │ Error: Missing key/value separator │ │ on cdk.tf.json line 26, in resource.local_file.ruwentest: │ 26: "content": "${jsonencode({\"join(\"\", [\"foo\", \"bar\"])\" = undefined})}", │ │ Expected an equals sign ("=") to mark the beginning of the attribute value. ╵

Let me add a bit of context:

I am using cdktf as a glorified terraform code generator. I just generate the code and then use the generated terraform json in my various environments.

As part of that I am generating IAM policies from various outputs of other resources.

In general the join works. If I use Fn.join to generate the value side of a json object, it works. Just not on the keyside.

        new File(this, "ruwentest", {
            filename: "ruwentest.json",
            content: Fn.jsonencode({
                    "baz": [Fn.join("", ["foo", "bar"])]
                }
            )
        })

works as expected

elruwen avatar Mar 04 '24 10:03 elruwen