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

Overriding getLogicalId causes disallowed characters in the cross-stack-output ID #3512

Open MWS-TAI opened 4 months ago

MWS-TAI commented 4 months ago

Expected Behavior

When overriding getLogicalId (get_logical_id py func) to simply return the node id (as per 3433's solution), I would expect for cross-stack output's to also have correctly formatted resource IDs just like the rest of the resources.

Override function is below:

def get_logical_id(self, tf_element: Union[Construct, TerraformElement]) -> str:
        return tf_element.node.id

Actual Behavior

The outputted cdk.tf.json correctly has a passable resource name, however, the following error is produced.

cdk.tf.json:

Screenshot 2024-02-20 at 1 03 08 pm

terminal output:

Screenshot 2024-02-20 at 1 02 17 pm

Steps to Reproduce

  1. Create cross stack dependency by reference a resource from a seperate stack
  2. Override getLogicalId as per #4334
  3. Execute a cdktf command that invokes backend initialisation (diff, deploy)

Versions

language: python cdktf-cli: 0.19.2 node: v21.5.0 cdktf: 0.20.3 constructs: 10.3.0 jsii: 1.94.0 terraform: 1.6.5 arch: arm64 os: darwin 23.2.0 python: Python 3.11.7 pip: pip 24.0 from /Users/mikeyscott/Documents/traffyk-ai-data-platform/.venv/lib/python3.11/site-packages/pip (python 3.11) pipenv: null

Providers

cdktf-cdktf-provider-archive (PREBUILT) terraform provider version: 2.4.2 prebuilt provider version: 10.0.1 cdktf version: ^0.20.0 cdktf-cdktf-provider-aws (PREBUILT) terraform provider version: 5.37.0 prebuilt provider version: 19.6.0 cdktf version: ^0.20.0 cdktf-cdktf-provider-tls (PREBUILT) terraform provider version: 4.0.5 prebuilt provider version: 10.0.0 cdktf version: ^0.20.0

Gist

No response

Possible Solutions

No response

Workarounds

Whilst not the best solution, adding a simple if condition to return the usual allocateLogicalId where the node id contains "cross-stack-output" (this doesn't seem to effect the "cross-stack-reference" id).

def get_logical_id(self, tf_element: Union[Construct, TerraformElement]) -> str:
        if "cross-stack-output" in tf_element.node.id:
            return self._allocate_logical_id(tf_element)
        return tf_element.node.id

Anything Else?

I am using node 21 which throws the "untested node version warning". I don't suspect this would cause this issue.

This is also my first issue here so please let me know if I haven't don't anything correctly.

References

3433

Help Wanted

Community Note

ansgarm commented 4 months ago

Hi @MWS-TAI 👋

It seems like there are dots introduced into the name by cross stack references (which is not valid Terraform). Normally they'd be replaced by the built-in logical id logic I suppose – so that would be something that would need to be replaced in your get_logical_id implementation.

I did not test cross stack references when I looked into how to solve the outputs id request, so it seems that they require some more adjustment there.