1

Background context / End goal:

I am trying to use cdk to create a target group that consists of the ip addresses that are associated with a vpc endpoint (for apigateway) as per this AWS blog. Ideally, I would like to be able to just lookup the associated ips using just the fact that the vpce is for the service of apigateway OR potentially using the vpce id.

Attempts

  1. I tried to use the cdk InterfaceVpcEndpoint construct static method using the fromInterfaceVpcEndpointAttributes (filtering by service). It did return the desired vpce, but unfortunately it returns in the format of IInterfaceVpcEndpoint which does not have the vpceNetworkInterfaceIds attribute that the InterfaceVpcEndpoint construct has
  2. I was able to use AwsCustomResource (after consulting a stack overflow post that referenced this example) to look up the ip addresses for a given array of vpce network interface ids:
const vpceNetworkInterfaceIds = =['eniId1', 'eniId2'];
const getEniIps = new AwsCustomResource(scope, `GetEndpointIps`, {
          onUpdate: {
            service: "EC2",
            action: "describeNetworkInterfaces",
            parameters: {
               NetworkInterfaceIds: vpceNetworkInterfaceIds
              },
            physicalResourceId: PhysicalResourceId.of(Date.now().toString())
          },
          policy: AwsCustomResourcePolicy.fromSdkCalls({
            resources: AwsCustomResourcePolicy.ANY_RESOURCE
          }),
        });

        const privateIpAddresses: string[] = [];
        for(let i = 0; i< vpceNetworkInterfaceIds.length; i++){
          const privateIpAddress: string = getNetworkInterfaceIpAddresses.getResponseField(`NetworkInterfaces.${i}.PrivateIpAddress`).toString();
          privateIpAddresses.push(privateIpAddress);
        }
        return privateIpAddresses;
}

  1. I tried to make a similar sdk call (describeVpcEndpoints), but then I encountered issues retrieving the array of NetworkInterfaceIds.
    const getNetworkInterfaceIpAddresses = new AwsCustomResource(scope, `GetVpceNetworkInterfaceIds`, {
        onUpdate: {
          service: "EC2",
          action: "describeVpcEndpoints",
          parameters: {
             Filters: [
              { 
                Name: "service-name",
                Values: ["com.amazonaws.us-east-1.execute-api"]
              }
             ]
            },
          physicalResourceId: PhysicalResourceId.of(Date.now().toString())
        },
        policy: AwsCustomResourcePolicy.fromSdkCalls({
          resources: AwsCustomResourcePolicy.ANY_RESOURCE
        }),
      });

      return getNetworkInterfaceIpAddresses.getResponseFieldReference(`VpcEndpoints.0.NetworkInterfaceIds`).toJSON();
   

I tried variations of using the Reference methods of toJson and toString but was not able to figure out how to get the array of values from this custom resource.

Questions

  1. How can you get an array from the sdk call of a aws custom resource?
  2. Is there a more straight forward way to get the vpceNetworkInterfaceIds of a given vpce?
  3. Is there a more straight forward way to get the ip addresses for a given vpce?
Sarah Ganci
  • 209
  • 1
  • 10

0 Answers0