Home > Back-end >  AWS CDK: How to find/fetch 'IntegrationId' of Integrations in API Gateway
AWS CDK: How to find/fetch 'IntegrationId' of Integrations in API Gateway

Time:12-15

I'm not sure and struggling to fetch Integration property 'Integration ID' of HTTP API Gateway. Below is the code (in C#) to create an Integration, however, since 'Integration ID' is not available, I'm not able to attach it to the 'Route' using CDK.

var cfnIntegration = new CfnIntegration(this, "Intergration", new CfnIntegrationProps()
{
   ApiId = cfnAPI.AttrApiId,
   IntegrationType = "AWS_PROXY",
   IntegrationUri = "arn:aws:lambda:us-east-1:123456789:function:TestFunction",
   IntegrationMethod = "POST",
   PayloadFormatVersion = "2.0",
});

The above code creates Integration, successfully, but not yet attached to the Route, shown below:

CfnRoute cfnRoute = new CfnRoute(this, "Routes", new CfnRouteProps()
{
  ApiId = cfnAPI.AttrApiId,
  RouteKey = "POST /webhook/digitalChannel,
  Target = $"integrations/{integration_id}" // <--- This is where the problem is, can't attach the above created Integration since integration_id is not known
});

enter image description here

CodePudding user response:

Target = $"integrations/{cfnIntegration.Ref}" 

A L1 construct's Ref property will be rendered at synth-time as a CloudFormation Ref intrinsic function. At deploy-time, as the AWS::ApiGatewayV2::Integration CloudFormation docs say, "Ref returns the Integration resource ID, such as abcd123".

CodePudding user response:

The integration ID is the return value from the CloudFormation resource. You can use CFN intrinsic functions to reference it (or access the Ref member on the CDK resource).

Additionally, string interpolation is not useable in this case because the value is only resolved during deployment. You'll have to use the Join intrinsic function to get around this.

I'm not all that familiar with C# but it should look something like the following:

CfnRoute cfnRoute = new CfnRoute(this, "Routes", new CfnRouteProps()
{
  ApiId = cfnAPI.AttrApiId,
  RouteKey = "POST /webhook/digitalChannel",
  Target = Fn.Join("/", {"integrations", cfnIntegration.Ref})
});
  • Related