Skip to content

Commit

Permalink
fix(apigatewayv2): multiple http integrations are created for each ro…
Browse files Browse the repository at this point in the history
…ute (aws#12528)

Currently when we define any HTTP integration (like ALB, NLB etc.) explicitly and use it for multiple routes, the `HttpRoute` construct will create a new `HttpIntegration` resource for each of these routes. [Ref](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-apigatewayv2/lib/http/route.ts#L128)

These extra HttpIntegration (and hence `CfnIntegration`) are not required since the same integration can be used for all the routes. This feature is supported in console as well (i.e. selecting same integration for multiple routes).  A sample snippet:

```ts
   const apiGateway     = new apigw.HttpApi(this, apiLableApiGateway);
    const apiIntegration = new apigwint.HttpAlbIntegration({
      listener         : apiListener,
      vpcLink          : apiVpcLink
    });
    const apiRoutes = [
      { method: apigw.HttpMethod.ANY, path: '/' },
      { method: apigw.HttpMethod.GET, path: '/status' },
      { method: apigw.HttpMethod.GET, path: '/whateverelse' },
    ];
    for (const apiRouteIndex in apiRoutes) {
      const apiRouteMethod = apiRoutes[apiRouteIndex].method;
      const apiRoutePath   = apiRoutes[apiRouteIndex].path;
      apiGateway.addRoutes({
        methods        : [apiRouteMethod],
        path           : apiRoutePath,
        integration    : apiIntegration,
      });
    }
```

The root cause of this issue is that the current api resource does not keep track of any existing integrations with the same integration config. 

We are solving this issue by keeping track of these integrations in an internal map with key as stringified value of `HttpRouteIntegrationConfig` (which we get by calling the `bind` function) and value as the `HttpIntegration`. This ensures that we reuse any existing integration (with same config) instead of creating a new one for every route.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
ayush987goyal authored and Mohan Rajendran committed Jan 24, 2021
1 parent e74f61b commit d873d51
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -608,24 +608,6 @@
"ProtocolType": "HTTP"
}
},
"HttpProxyPrivateApiDefaultRouteDefaultRouteIntegration0AE210B0": {
"Type": "AWS::ApiGatewayV2::Integration",
"Properties": {
"ApiId": {
"Ref": "HttpProxyPrivateApiA55E154D"
},
"IntegrationType": "HTTP_PROXY",
"ConnectionId": {
"Ref": "HttpProxyPrivateApiVpcLink190366CAE"
},
"ConnectionType": "VPC_LINK",
"IntegrationMethod": "ANY",
"IntegrationUri": {
"Ref": "lblistener657ADDEC"
},
"PayloadFormatVersion": "1.0"
}
},
"HttpProxyPrivateApiDefaultRoute1BDCA252": {
"Type": "AWS::ApiGatewayV2::Route",
"Properties": {
Expand All @@ -639,7 +621,7 @@
[
"integrations/",
{
"Ref": "HttpProxyPrivateApiDefaultRouteDefaultRouteIntegration0AE210B0"
"Ref": "HttpProxyPrivateApiHttpIntegrationa4f5237945b4bba2a6a4cd8c5a7be8839B686B4E"
}
]
]
Expand All @@ -664,6 +646,24 @@
"SecurityGroupIds": []
}
},
"HttpProxyPrivateApiHttpIntegrationa4f5237945b4bba2a6a4cd8c5a7be8839B686B4E": {
"Type": "AWS::ApiGatewayV2::Integration",
"Properties": {
"ApiId": {
"Ref": "HttpProxyPrivateApiA55E154D"
},
"IntegrationType": "HTTP_PROXY",
"ConnectionId": {
"Ref": "HttpProxyPrivateApiVpcLink190366CAE"
},
"ConnectionType": "VPC_LINK",
"IntegrationMethod": "ANY",
"IntegrationUri": {
"Ref": "lblistener657ADDEC"
},
"PayloadFormatVersion": "1.0"
}
},
"HttpProxyPrivateApiDefaultStage18B3706E": {
"Type": "AWS::ApiGatewayV2::Stage",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
"Code": {
"ZipFile": "exports.handler = async function(event, context) { return { statusCode: 200, body: \"success\" }; };"
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"AlwaysSuccessServiceRole6DB8C2F6",
"Arn"
]
},
"Handler": "index.handler",
"Runtime": "nodejs12.x"
},
"DependsOn": [
Expand Down Expand Up @@ -94,22 +94,6 @@
}
}
},
"LambdaProxyApiDefaultRouteDefaultRouteIntegration97D5250B": {
"Type": "AWS::ApiGatewayV2::Integration",
"Properties": {
"ApiId": {
"Ref": "LambdaProxyApi67594471"
},
"IntegrationType": "AWS_PROXY",
"IntegrationUri": {
"Fn::GetAtt": [
"AlwaysSuccess099EAB05",
"Arn"
]
},
"PayloadFormatVersion": "2.0"
}
},
"LambdaProxyApiDefaultRoute1EB30A46": {
"Type": "AWS::ApiGatewayV2::Route",
"Properties": {
Expand All @@ -123,13 +107,29 @@
[
"integrations/",
{
"Ref": "LambdaProxyApiDefaultRouteDefaultRouteIntegration97D5250B"
"Ref": "LambdaProxyApiHttpIntegration5fc3e065dcc18f77be1ff86cabb5294a98B12A6C"
}
]
]
}
}
},
"LambdaProxyApiHttpIntegration5fc3e065dcc18f77be1ff86cabb5294a98B12A6C": {
"Type": "AWS::ApiGatewayV2::Integration",
"Properties": {
"ApiId": {
"Ref": "LambdaProxyApi67594471"
},
"IntegrationType": "AWS_PROXY",
"IntegrationUri": {
"Fn::GetAtt": [
"AlwaysSuccess099EAB05",
"Arn"
]
},
"PayloadFormatVersion": "2.0"
}
},
"LambdaProxyApiDefaultStage07C38681": {
"Type": "AWS::ApiGatewayV2::Stage",
"Properties": {
Expand All @@ -147,7 +147,27 @@
"ProtocolType": "HTTP"
}
},
"HttpProxyApiDefaultRouteDefaultRouteIntegrationF2E17850": {
"HttpProxyApiDefaultRoute8AF66B5C": {
"Type": "AWS::ApiGatewayV2::Route",
"Properties": {
"ApiId": {
"Ref": "HttpProxyApiD0217C67"
},
"RouteKey": "$default",
"Target": {
"Fn::Join": [
"",
[
"integrations/",
{
"Ref": "HttpProxyApiHttpIntegration54d3201aa1691a2307067d718d9dea776A187065"
}
]
]
}
}
},
"HttpProxyApiHttpIntegration54d3201aa1691a2307067d718d9dea776A187065": {
"Type": "AWS::ApiGatewayV2::Integration",
"Properties": {
"ApiId": {
Expand Down Expand Up @@ -178,26 +198,6 @@
"PayloadFormatVersion": "1.0"
}
},
"HttpProxyApiDefaultRoute8AF66B5C": {
"Type": "AWS::ApiGatewayV2::Route",
"Properties": {
"ApiId": {
"Ref": "HttpProxyApiD0217C67"
},
"RouteKey": "$default",
"Target": {
"Fn::Join": [
"",
[
"integrations/",
{
"Ref": "HttpProxyApiDefaultRouteDefaultRouteIntegrationF2E17850"
}
]
]
}
}
},
"HttpProxyApiDefaultStageA88F9DE3": {
"Type": "AWS::ApiGatewayV2::Stage",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
"Code": {
"ZipFile": "exports.handler = async function(event, context) { return { statusCode: 200, body: \"success\" }; };"
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"AlwaysSuccessServiceRole6DB8C2F6",
"Arn"
]
},
"Handler": "index.handler",
"Runtime": "nodejs12.x"
},
"DependsOn": [
Expand Down Expand Up @@ -94,22 +94,6 @@
}
}
},
"LambdaProxyApiDefaultRouteDefaultRouteIntegration97D5250B": {
"Type": "AWS::ApiGatewayV2::Integration",
"Properties": {
"ApiId": {
"Ref": "LambdaProxyApi67594471"
},
"IntegrationType": "AWS_PROXY",
"IntegrationUri": {
"Fn::GetAtt": [
"AlwaysSuccess099EAB05",
"Arn"
]
},
"PayloadFormatVersion": "2.0"
}
},
"LambdaProxyApiDefaultRoute1EB30A46": {
"Type": "AWS::ApiGatewayV2::Route",
"Properties": {
Expand All @@ -123,13 +107,29 @@
[
"integrations/",
{
"Ref": "LambdaProxyApiDefaultRouteDefaultRouteIntegration97D5250B"
"Ref": "LambdaProxyApiHttpIntegration5fc3e065dcc18f77be1ff86cabb5294a98B12A6C"
}
]
]
}
}
},
"LambdaProxyApiHttpIntegration5fc3e065dcc18f77be1ff86cabb5294a98B12A6C": {
"Type": "AWS::ApiGatewayV2::Integration",
"Properties": {
"ApiId": {
"Ref": "LambdaProxyApi67594471"
},
"IntegrationType": "AWS_PROXY",
"IntegrationUri": {
"Fn::GetAtt": [
"AlwaysSuccess099EAB05",
"Arn"
]
},
"PayloadFormatVersion": "2.0"
}
},
"LambdaProxyApiDefaultStage07C38681": {
"Type": "AWS::ApiGatewayV2::Stage",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,24 +573,6 @@
"ProtocolType": "HTTP"
}
},
"HttpProxyPrivateApiDefaultRouteDefaultRouteIntegration0AE210B0": {
"Type": "AWS::ApiGatewayV2::Integration",
"Properties": {
"ApiId": {
"Ref": "HttpProxyPrivateApiA55E154D"
},
"IntegrationType": "HTTP_PROXY",
"ConnectionId": {
"Ref": "HttpProxyPrivateApiVpcLink190366CAE"
},
"ConnectionType": "VPC_LINK",
"IntegrationMethod": "ANY",
"IntegrationUri": {
"Ref": "lblistener657ADDEC"
},
"PayloadFormatVersion": "1.0"
}
},
"HttpProxyPrivateApiDefaultRoute1BDCA252": {
"Type": "AWS::ApiGatewayV2::Route",
"Properties": {
Expand All @@ -604,7 +586,7 @@
[
"integrations/",
{
"Ref": "HttpProxyPrivateApiDefaultRouteDefaultRouteIntegration0AE210B0"
"Ref": "HttpProxyPrivateApiHttpIntegration392420cf56fd6784275525566e09a727DDE15AB1"
}
]
]
Expand All @@ -629,6 +611,24 @@
"SecurityGroupIds": []
}
},
"HttpProxyPrivateApiHttpIntegration392420cf56fd6784275525566e09a727DDE15AB1": {
"Type": "AWS::ApiGatewayV2::Integration",
"Properties": {
"ApiId": {
"Ref": "HttpProxyPrivateApiA55E154D"
},
"IntegrationType": "HTTP_PROXY",
"ConnectionId": {
"Ref": "HttpProxyPrivateApiVpcLink190366CAE"
},
"ConnectionType": "VPC_LINK",
"IntegrationMethod": "ANY",
"IntegrationUri": {
"Ref": "lblistener657ADDEC"
},
"PayloadFormatVersion": "1.0"
}
},
"HttpProxyPrivateApiDefaultStage18B3706E": {
"Type": "AWS::ApiGatewayV2::Stage",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,27 @@
"ProtocolType": "HTTP"
}
},
"HttpProxyPrivateApiDefaultRouteDefaultRouteIntegration0AE210B0": {
"HttpProxyPrivateApiDefaultRoute1BDCA252": {
"Type": "AWS::ApiGatewayV2::Route",
"Properties": {
"ApiId": {
"Ref": "HttpProxyPrivateApiA55E154D"
},
"RouteKey": "$default",
"Target": {
"Fn::Join": [
"",
[
"integrations/",
{
"Ref": "HttpProxyPrivateApiHttpIntegration763b273f031b9eb3c58b2fa36954470072FBC87C"
}
]
]
}
}
},
"HttpProxyPrivateApiHttpIntegration763b273f031b9eb3c58b2fa36954470072FBC87C": {
"Type": "AWS::ApiGatewayV2::Integration",
"Properties": {
"ApiId": {
Expand All @@ -595,26 +615,6 @@
"PayloadFormatVersion": "1.0"
}
},
"HttpProxyPrivateApiDefaultRoute1BDCA252": {
"Type": "AWS::ApiGatewayV2::Route",
"Properties": {
"ApiId": {
"Ref": "HttpProxyPrivateApiA55E154D"
},
"RouteKey": "$default",
"Target": {
"Fn::Join": [
"",
[
"integrations/",
{
"Ref": "HttpProxyPrivateApiDefaultRouteDefaultRouteIntegration0AE210B0"
}
]
]
}
}
},
"HttpProxyPrivateApiDefaultStage18B3706E": {
"Type": "AWS::ApiGatewayV2::Stage",
"Properties": {
Expand Down
Loading

0 comments on commit d873d51

Please sign in to comment.