Python escape hatching in 0.9.x

I upgraded from 0.7.0 to 0.9.4 and my python escape hatch for some Azure array references (lbfe_id in snippet below) is no longer working. I’ve searched the existing issues and tried the Fn lookup from #1108], but get an unresolved-token error with that approach. Not sure if I just missed something in the docs about the changes between 0.7.0 and 0.9.4.

Appreciate any guidance. Thank you.

        ...
        from imports.azurerm import Lb, LbFrontendIpConfiguration, PrivateLinkService

        lbfe = LbFrontendIpConfiguration(name=_mkname(ns,"lbfe-001"),
                private_ip_address_allocation="Dynamic", subnet_id=bsnet.id)
        lb = Lb(self, "lb-001", name=_mkname(ns,"lb-001"), location=region_name,
                resource_group_name=rg, frontend_ip_configuration=[lbfe], sku="Standard")

        """ this worked in 0.7.0, errors with following with 0.9.4
              "${${azurerm_lb.lb-001}.frontend_ip_configuration.0.id}"
             │ 
             │ Expected the start of an expression, but found an invalid expression token.
        """
        lbfe_id =f"${{{lb.fqn}.frontend_ip_configuration.0.id}}"

        """ from issue #1108, errors with:
             jsii.errors.JSIIError: Expected array type, got "<unresolved-token>"
        """
        lbfe_id = Fn.lookup(Fn.element(lb.frontend_ip_configuration, 0), "id")

        pls = PrivateLinkService(self, "pls-001", location=region_name,
            load_balancer_frontend_ip_configuration_ids=[lbfe_id],...

I also tried add_override I noticed in a few other posts, but get error noted below:

        """
          errors with:
          "${azurerm_lb.lb-001}.frontend_ip_configuration.0.id"
         │     ├────────────────
         │     │ azurerm_lb.lb-001 is object with 11 attributes
        """
        pls.add_override("load_balancer_frontend_ip_configuration_ids",
                [lb.fqn + ".frontend_ip_configuration.0.id"])

I think your first approach (using string interpolation) was pretty close. If rather than putting lbfe_id into a list you instead use Token.as_list(lbfe_id) when passing to pls, it may work correctly. May also be able to do something similar when using the override.

I believe Use `ComplexList` classes for references to list attributes of resources · Issue #1610 · hashicorp/terraform-cdk · GitHub would make this a better experience.

Thanks @jsteinich , the as_list is accepted but I get the same invalid expression error. Appears the string interpolation for fqn post 0.9.0 changed where when you append resources you get this “double interpolation”.

    #pre 0.9.0 cdk.tf.json 
    "load_balancer_frontend_ip_configuration_ids": [
      "${azurerm_lb.lb-001.frontend_ip_configuration.0.id}"
    ],

    #post 0.9.0 cdk.tf.json 
    "load_balancer_frontend_ip_configuration_ids": [
      "${${azurerm_lb.lb-001}.frontend_ip_configuration.0.id}"
    ],

I believe fqn was changed so that references could be tracked between resources when using it; however, it does seem to not quite resolve correctly.

One workaround that found was to replace resource.fqn with resource.terraform_resource_type + "." + resource.friendly_unique_id.

Thanks @jsteinich! That works. I hadn’t even considered fqn may have changed to support cross stack. Thanks again.