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

ReplaceTriggeredBy: Still not working in Golang #3546

Open eahrend opened 3 months ago

eahrend commented 3 months ago

Expected Behavior

I should be able to create a replaceTriggeredBy lifecycle rule by specifying the FQN of the resource that I want to cause a replacement trigger for, like:

    triggersMap := map[string]*string{
        "databases": jsii.String(strings.Join(dbNames, ",")), // trigger on database changes
    }
    resourceId := jsii.Sprintf("cloud-sql-user-%s-%s-%s-trigger", h.ProjectName, h.Struct.Name, userName)
    userReplacementTrigger := nr.NewResource(h.Stack, resourceId, &nr.ResourceConfig{Triggers: &triggersMap})
    lifecycle := cdktf.TerraformResourceLifecycle{
        ReplaceTriggeredBy: &[]interface{}{
            userReplacementTrigger.Fqn(),
        },
    }

Actual Behavior

I get an invalid expression. The cdktf.json file shows:

    "google_sql_user": {
        ###REDACTED###
        "lifecycle": {
          "replace_triggered_by": [
            "${null_resource.cloud-sql-user-my-instance-default-user-trigger}"
          ]
        },
        ###REDACTED###
      },

Steps to Reproduce

Apply the stack with the relevant code.

Versions

Couldn't get the info from cdktf debug

npm list -g /Users/erik/.local/lib ├── cdktf-cli@0.20.1 └── cdktf@0.20.1 go version go version go1.21.6 darwin/arm64 terraform version Terraform v1.7.0 on darwin_arm64 Node.js v20.11.0

Running this command fails with this error:

Error: Pre-built provider information not found
    at Bnr (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:171:2225)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async zU (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:171:1716)
    at async lde (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:171:4004)
    at async /Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:176:3945
    at async Promise.all (index 2)
    at async Kk.allProviders (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:176:3854)
    at async Object.L8r (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:551:1832)
    at async JZ.handleCommand (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cdktf.js:85:887)
    at async Object.handler (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cdktf.js:84:15344)

Providers

Running this command fails with this error:

Error: Pre-built provider information not found
    at Bnr (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:171:2225)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async zU (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:171:1716)
    at async lde (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:171:4004)
    at async /Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:176:3945
    at async Promise.all (index 2)
    at async Kk.allProviders (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:176:3854)
    at async Object.L8r (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:551:1832)
    at async JZ.handleCommand (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cdktf.js:85:887)
    at async Object.handler (/Users/erik/.local/lib/node_modules/cdktf-cli/bundle/bin/cdktf.js:84:15344)

Gist

No response

Possible Solutions

No response

Workarounds

Workaround is to write it like this:

    lifecycle := cdktf.TerraformResourceLifecycle{
        ReplaceTriggeredBy: &[]interface{}{
            // Workaround for https://github.com/hashicorp/terraform-cdk/issues/3196
            fmt.Sprintf("%s.%s", *userReplacementTrigger.TerraformResourceType(), *userReplacementTrigger.FriendlyUniqueId()),
        },
    }

Anything Else?

No response

References

https://github.com/hashicorp/terraform-cdk/issues/3196 https://github.com/hashicorp/terraform-cdk/issues/1866#issuecomment-1155160808

Help Wanted

Community Note

TylerJGabb commented 3 months ago

I am interested in working on this issue.

MWS-TAI commented 3 months ago

I am also experiencing this issue with Python, have tried providing just the resource variable and using .fqn as 3322 code changes suggest.

The workaround by @eahrend seems to work

lifecycle=TerraformResourceLifecycle(
    replace_triggered_by=[
        f"{self.__redshift_namespace.terraform_resource_type}.{self.__redshift_namespace.friendly_unique_id}",
    ]
)
"lifecycle": {
      "replace_triggered_by": [
          "aws_redshiftserverless_namespace.redshift__redshift_namespace"
      ]
    },

I believe the underlying issue is that the fqn property includes the hcl templating syntax as shown below in the cdktf.json when I use fqn:

lifecycle=TerraformResourceLifecycle(
    replace_triggered_by=[
        f"{self.__redshift_namespace.fqn}",
    ]
)
"lifecycle": {
      "replace_triggered_by": [
          "${aws_redshiftserverless_namespace.redshift__redshift_namespace}"
      ]
    },

I am not sure if fqn including the syntax is by design, but that seems to be the underlying issue.

benkeil commented 1 week ago

any progress on this?