Repository: hashicorp/terraform-cdk

Issue #1447: Using the same reference in different locations (in and outside a TF Expression) causes issues
Created: 2022-01-03T13:22:30Z
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


cdktf & Language Versions


cdktf 0.8
all languages

Affected Resource(s)

Debug Output

Expected Behavior

When I use the same reference twice but in different settings (in or outside a TF Expression) the reference gets rendered in the same way (depending on the order of the references).
const myS3Bucket = new S3Bucket(this, "mybucket", {});
const arn = myS3Bucket.arn;


const a = Fn.join(",", [arn, `this is the arn: ${arn}`]) 
const b = Fn.join(",", [`this is the arn: ${arn}`, arn]) 
both a and b should be valid terraform but they are not
Actual Behavior

The order of the arns leads either to one ${} part outside of a string or a missing ${} block inside the synthesised json.

Duplicate issue #1684: Nested tokens are not escaped when `Fn.format` is used on them
Created: 2022-04-04T15:58:47Z
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


cdktf & Language Versions

NodeJS 16
cdktf 0.10.1

Affected Resource(s)
Probably any, but I just tested with StringResource from @cdktf/provider-random
Debug Output
debug output
yarn run v1.22.18
$ CDKTF_LOG_LEVEL=debug cdktf synth
[2022-04-04T17:41:21.284] [DEBUG] default - {
  "terraform_version": "1.1.7",
  "platform": "darwin_amd64",
  "provider_selections": {},
  "terraform_outdated": false
}

[2022-04-04T17:41:21.320] [DEBUG] default - v17.8.0

[2022-04-04T17:41:21.357] [DEBUG] default - Terraform v1.1.7
on darwin_amd64

Generated Terraform code for the stacks: stage
✨  Done in 8.33s.


Expected Behavior
Outputs should escape all tokens:
  "output": {
    "additional_output": {
      "value": "${random_string.id.result}"
    },
    "direct_output": {
      "value": "${random_string.id.result}"
    },
    "formatted_output": {
      "value": "${format(\"%s\", random_string.id.result)}"
    }
  },
Actual Behavior
One token is not escaped (see line 3):
  "output": {
    "additional_output": {
      "value": "random_string.id.result"
    },
    "direct_output": {
      "value": "${random_string.id.result}"
    },
    "formatted_output": {
      "value": "${format(\"%s\", random_string.id.result)}"
    }
  },
Steps to Reproduce
Here's a snippet for typescript:
// @ts-ignore
import { Fn, TerraformOutput, TerraformStack } from 'cdktf';
import { Construct } from 'constructs';
import { RandomProvider, StringResource } from '@cdktf/provider-random';

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

    new RandomProvider(this, 'random');

    const resource = new StringResource(this, 'id', { length: 1 });
    const nested = { value: resource.result };
    new TerraformOutput(this, 'direct_output', {
      value: resource.result,
    });
    new TerraformOutput(this, 'additional_output', {
      value: nested.value,
    });
    new TerraformOutput(this, 'formatted_output', {
      // if you replace the Fn.format call with a static string, everything works
      // value: 'static',
      value: Fn.format('%s', [nested.value]),
    });
  }
}
If it is run as above, the additional_output will not be escaped in the json output. Interestingly this happens only for the mapped/nested variable (i.e. the one that's wrapped in an object). It probably has something to do with the new token handling since 0.9.0, because the exact same code works with 0.8.*. I hope I didn't overlook something obvious when I attempted to upgrade our code to the newer version. Also, if the title doesn't fit, please feel free to adjust it.

Closing comment (2022-04-05T08:16:39Z): I'm going to close this as it is a duplicate of #1447. But feel free to comment here or on the other issue with any further feedback 🙂