hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.88k stars 456 forks source link

lifecycle: replaceTriggeredBy lifecycle - Invalid character and Invalid expression errors in cdk.tf.json #3645

Closed sidlinux22 closed 4 months ago

sidlinux22 commented 5 months ago

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

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

Versions

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

Community Note

DJAlPee commented 5 months ago

Have the same issue. Seems to be a duplicate of #3532

mutahhir commented 4 months ago

Closing in favor of #3532

github-actions[bot] commented 3 months ago

I'm going to lock this issue because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.