Indexed Property access of Resource with Count

Hello,

here is my Problem: Not in every AWS AvailabilityZone is a specific ENI. Therefore the Terraform code must react on that situation. If I simply use DataAwsNetworkInterface then there will be an exception, if the ENI is missing in the AVZ (“DataAwsNetworkInterface empty result not allowed”)

Therefore my current workaround:

  • Search with DataAwsNetworkInterfaces (will not fail, if no results)
  • Add a count property to DataAwsNetworkInterface.
  • Means, if no Interfaces are found 0 DataAwsNetworkInterface are created → no exception

But now I have to access the Properties like Ip of ENI with an indexer.
How can this be done? My Soltuion works, but is not nice because the property is specified as string.

Here is my code:

for (const [avz, subnet] of subnets) {
      
      const eniS = new DataAwsNetworkInterfaces(this,
        "enis_" + avz,
        {
          filter: [
            {
              name: "subnet-id",
              values: [subnet.id],
            },
            {
              name: "description",
              values: ["My ENI"],
            },
          ],
        }
      )

      const eni = new DataAwsNetworkInterface(
        this,
        "eni_" + avz,
        {
          count: Fn.lengthOf(eniS.ids),
          filter: [
            {
              name: "subnet-id",
              values: [subnet.id],
            },
            {
              name: "description",
              values: ["My ENI"],
            },
          ],
        }
      );

      new LbTargetGroupAttachment(
        this,
        "lb_target_group_attachment_eni_" + avz,
        {
          count: Fn.lengthOf(eniS.ids),
          targetGroupArn: targetGroup.arn,
          targetId: "${" + eni.fqn + "[0].private_ip}",
          availabilityZone: "${" + eni.fqn + "[0].availability_zone}",
        }
      );
    }

Any recommendation how to change this, so that it uses built-in framework methods to do this?