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.79k stars 442 forks source link

lifecycle: replaceTriggeredBy should not be a string interpolation #3532

Open wayneslabs opened 4 months ago

wayneslabs commented 4 months ago

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

Help Wanted

Community Note

DJAlPee commented 1 week ago

replaceTriggeredBy should behave the same way like dependsOn: https://github.com/hashicorp/terraform-cdk/blob/4fd0a65ae698e999d3ba2590257fd770793dcf36/packages/cdktf/lib/terraform-resource.ts#L160-L164

vs. https://github.com/hashicorp/terraform-cdk/blob/4fd0a65ae698e999d3ba2590257fd770793dcf36/packages/cdktf/lib/terraform-resource.ts#L78-L88

So would changing from return x.fqdn; to return dependable(x); fix the issue? As a workaround, I'm doing it like this in my code:

import { TerraformResource, dependable } from 'cdktf';

const monitoredResources: TerraformResource = [];

// Add relevant resources to array

new ApiGatewayDeployment(this, 'service_api_deployment', {
  restApiId: apiGateway.id,
  lifecycle: {
    replaceTriggeredBy: monitoredResources.map(dependable),
  },
});