Repository: hashicorp/terraform-cdk

Issue #3532: lifecycle: replaceTriggeredBy should not be a string interpolation
Created: 2024-03-03T16:34:56Z
Expected Behavior
When a value is provided for lifecycle.replaceTriggeredBy, it should not use string interpolation in the final terraform code.
For example:
"lifecycle": {
    "replace_triggered_by": [
      "google_firebaserules_ruleset.MainRuleSet"
    ]
  }

Actual Behavior
Currently, it creates a string interpolation
For example:
"lifecycle": {
    "replace_triggered_by": [
      "${google_firebaserules_ruleset.MainRuleSet}"
    ]
  }

This throws an error:
╷
│ Error: Invalid character
│
│   on  line 33:
│   (source code not available)
│
│ This character is not used within the language.
╵

╷
│ Error: Invalid expression
│
│   on  line 33:
│   (source code not available)
│
│ Expected the start of an expression, but found an invalid expression token.

Steps to Reproduce
Create a resource with replaceTriggeredBy
lifecycle: { replaceTriggeredBy: [mainRuleSet] }

I got the error while following the steps for creating firebase rules, which requires replaceTriggeredBy: docs
Versions
language: typescript
cdktf-cli: 0.20.4
node: v20.10.0
cdktf: 0.20.4
constructs: 10.3.0
jsii: null
terraform: 1.7.4
arch: x64
os: MacOS Monterey
Providers
No response
Gist
No response
Possible Solutions
No response
Workarounds
Currently, it can be solved by manually constructing it, as noted at: issue comment
release.addOverride("lifecycle.replace_triggered_by", [
  mainRuleSet.terraformResourceType +
    "." +
    mainRuleSet.friendlyUniqueId,
]);

Anything Else?
No response
References

#3196
#2082
#3322

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

Duplicate issue #3645: lifecycle:  replaceTriggeredBy lifecycle - Invalid character and Invalid expression errors in cdk.tf.json
Created: 2024-06-11T13:58:43Z
Expected Behavior
cdktf deploy should run without errors, properly recognizing the replace_triggered_by lifecycle configuration when using a dynamic value.
Actual Behavior
I'm encountering the following errors when running  terraform init  cdktf deploy
test-stack  Initializing the backend...
test-stack  
            Successfully configured the backend "local"! Terraform will automatically
            use this backend unless the backend configuration changes.
test-stack  Terraform encountered problems during initialisation, including problems
            with the configuration, described below.

            The Terraform configuration must be valid before initialization so that
            Terraform can determine which modules and providers need to be installed.
            ╷
            │ Error: Invalid character
            │ 
            │   on cdk.tf.json line 54, in resource.aws_internet_gateway.VpcConstruct_InternetGateway_962A2FFB (VpcConstruct/InternetGateway/InternetGateway).lifecycle.replace_triggered_by:
            │   54:             "${aws_vpc.VpcConstruct_Vpc_test2-cdktf-app-vpc_576D1A6F (VpcConstruct/Vpc/test2-cdktf-app-vpc).id}"
            │ 
            │ This character is not used within the language.
            ╵
            
test-stack  ╷
            │ Error: Invalid expression
            │ 
            │   on cdk.tf.json line 54, in resource.aws_internet_gateway.VpcConstruct_InternetGateway_962A2FFB (VpcConstruct/InternetGateway/InternetGateway).lifecycle.replace_triggered_by:
            │   54:             "${aws_vpc.VpcConstruct_Vpc_test2-cdktf-app-vpc_576D1A6F (VpcConstruct/Vpc/test2-cdktf-app-vpc).id}"
            │ 
            │ Expected the start of an expression, but found an invalid expression token.
            ╵
            
0 Stacks deploying     0 Stacks done     1 Stack waiting
Error: terraform init failed with exit code 1

This issue occurs only when using the replaceTriggeredBy lifecycle property with a dynamic value. It works fine if a static value is provided, such as replaceTriggeredBy: ["aws_vpc.VpcConstruct_Vpc_test2-cdktf-app-vpc_576D1A6F.id"]. The error seems to be caused by the ${} syntax when using dynamic values.
Code Snippet
Here is the relevant code snippet:
import { Construct } from 'constructs';
import { InternetGateway } from '@cdktf/provider-aws/lib/internet-gateway';

export interface InternetGatewayConstructProps {
  readonly vpcId: string;
  readonly tags?: { [key: string]: string };
  readonly environment: string;
}

export class InternetGatewayConstruct extends Construct {
  public readonly internetGateway: InternetGateway;

  constructor(scope: Construct, id: string, props: InternetGatewayConstructProps) {
    super(scope, id);
    this.internetGateway = new InternetGateway(this, 'InternetGateway', {
      vpcId: props.vpcId,
      lifecycle: {
        //   replaceTriggeredBy: ["aws_vpc.VpcConstruct_Vpc_test2-cdktf-app-vpc_576D1A6F.id"],
        replaceTriggeredBy: [props.vpcId],
      },
      tags: {
        Name: `${id}-igw`,
      },
    });

  }
}


cdk.tf.json Snippet
  "resource": {
    "aws_internet_gateway": {
      "VpcConstruct_InternetGateway_962A2FFB": {
        "//": {
          "metadata": {
            "path": "test-stack/VpcConstruct/InternetGateway/InternetGateway",
            "uniqueId": "VpcConstruct_InternetGateway_962A2FFB"
          }
        },
        "lifecycle": {
          "replace_triggered_by": [
            "${aws_vpc.VpcConstruct_Vpc_test2-cdktf-app-vpc_576D1A6F.id}"
          ]
        },

Steps to Reproduce

Define the InternetGatewayConstruct as shown in the code snippet above.
Add the lifecycle property to the InternetGateway resource with replaceTriggeredBy using a dynamic value (props.vpcId).
Run cdktf deploy.

Versions

typescriptVersion: "~5.3.3"
jsiiVersion: "~5.3.3"
NodeVersion: '20.13.0'
constructsVersion:  '^10.3.0'
cdktfVersion: '0.20.7'
ProductName:  macOS
ProductVersion:  14.3
BuildVersion:   23D56

Providers
provider-aws@^19.12.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

Closing comment (2024-06-26T08:33:28Z): Have the same issue. Seems to be a duplicate of #3532