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

Conversion from Typescript template literals and concatenated strings to other languages is broken #2643

Closed mutahhir closed 11 months ago

mutahhir commented 1 year ago

Python

Converted

  1. Reference a property via fqn better

            availability_zone=f"\\${{dataAwsAvailabilityZonesChangemeAzListEbsSnapshot.names.fqn}[0]}",
  2. Should not be using terraform escape here?

        aws_iam_role_changeme_spot_and_fargate_iam_role_node_group = aws.iam_role.IamRole(self, "changeme_spot_and_fargate_iam_role_node_group",
    # ...
            name=f"\\${{changemeSpotAndFargateName.value}}-eks-node-group-role"
        )
  3. Just generally using the hcl literal escapes wrong

            availability_zone=f"\\${element({changemeSpotAndFargateAvailabilityZones.value}, count.index)}",
  4. More complex example

    source_code_hash=f"${{filebase64sha256(\}"\\${{changemeLambdaFunctionZip.value}}\")}"

Error


    Traceback (most recent call last):
      File "/Users/mutahhir/src/scratchpad/terraform-convert-examples/converted-langs/python/aws/main.py", line 11, in <module>
        from aws_ebs_volume.ebs_snapshot.main import MyStack as aws_ebs_volume_ebs_snapshot
      File "/Users/mutahhir/src/scratchpad/terraform-convert-examples/converted-langs/python/aws/aws_ebs_volume/ebs_snapshot/main.py", line 26
        availability_zone=f"\\${{dataAwsAvailabilityZonesChangemeAzListEbsSnapshot.names.fqn}[0]}",
                                                                                                  ^
    SyntaxError: f-string: single '}' is not allowed

Correct way

            availability_zone=f"${{{dataAwsAvailabilityZonesChangemeAzListEbsSnapshot.fqn}.names[0]}}",

CSharp

            awsRouteTableChangemeSpotAndFargateRouteTablePrivate.AddOverride("count", $"\\${length({changemeSpotAndFargatePrivateSubnets.value})}");

When it should be:

            awsRouteTableChangemeSpotAndFargateRouteTablePrivate.AddOverride("count", $"\\${{length({changemeSpotAndFargatePrivateSubnets.value})}}");

It shouldn't even need to escape the dollar sign, so even $"${{length... should work too?

mutahhir commented 1 year ago

JSII Issue: https://github.com/aws/jsii/issues/3967

mutahhir commented 1 year ago

Going to mark this as a non-blocker since the release of #2744 as that PR moves away from using string templates and on to string concatenation instead.

ansgarm commented 1 year ago

Wellllll, there's a now string concatenation problem in Python 😂

      File "/Users/ansgar/projects/playground/test-cdktf-convert/main.py", line 30, in __init__
        "description": "changeme-aws-security-group-dynamic-ingress-${" + aws_security_group_changeme_aws_security_group_dynamic_dynamic_iterator0.key + "}",
    TypeError: can only concatenate str (not "_LazyBaseProxy") to str

(more context in the original comment: https://github.com/hashicorp/terraform-cdk/issues/2637#issuecomment-1505210351)

edit: the code above works in TypeScript because the Lazy class has an toString method. This would work in other languages too, if JSII would implement this feature request: https://github.com/aws/jsii/issues/380

github-actions[bot] commented 10 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.