Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cdktf: computed object key leads to broken terraform code #3525

Open
1 task
elruwen opened this issue Feb 27, 2024 · 2 comments
Open
1 task

cdktf: computed object key leads to broken terraform code #3525

elruwen opened this issue Feb 27, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@elruwen
Copy link

elruwen commented Feb 27, 2024

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 elruwen added bug Something isn't working new Un-triaged issue labels Feb 27, 2024
@DanielMSchmidt
Copy link
Contributor

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("").

@ansgarm ansgarm added waiting-on-answer and removed new Un-triaged issue labels Mar 4, 2024
@elruwen
Copy link
Author

elruwen commented Mar 4, 2024

Argh I realised I actually didn't past the error message 🤦‍♂️

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants