Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apigatewayv2): multiple http integrations are created for each route #12528

Merged
merged 6 commits into from
Jan 22, 2021
Merged

fix(apigatewayv2): multiple http integrations are created for each route #12528

merged 6 commits into from
Jan 22, 2021

Conversation

ayush987goyal
Copy link
Contributor

@ayush987goyal ayush987goyal commented Jan 15, 2021

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

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:

   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

@gitpod-io
Copy link

gitpod-io bot commented Jan 15, 2021

@github-actions github-actions bot added the @aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 label Jan 15, 2021
Copy link
Contributor

@nija-at nija-at left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for submitting this PR but I don't the follow problem being fixed here.

In the PR description, can you provide a brief description of the error faced, the root cause and fix.
This will also ensure healthy commit messages for changes in our repo history.

@ayush987goyal
Copy link
Contributor Author

@nija-at Sorry for not specifying the details of the problem upfront. I have tried to provide as much details as possible now. Please take a look and let me know if we need to refine the same :)

@ayush987goyal ayush987goyal requested a review from nija-at January 15, 2021 16:45
@informity
Copy link

informity commented Jan 15, 2021

Thank you for submitting this PR, @ayush987goyal. This is the behavior I encountered while executing the code above. Please let me know if you need any additional information from me. I would think, creating integration beforehand and the use its handler in subsequent routes additions would make sense. And if creation of a new integration for each route is needed, it could be achieved by just creating new object right there… in other words:

const apiGateway = new apigw.HttpApi(this, apiLableApiGateway);

// FOR EXISTING INTEGRATION:
const apiIntegration = new apigwint.HttpAlbIntegration({
  listener       : apiListener,
  vpcLink        : apiVpcLink
});

apiGateway.addRoutes({
  methods        : [apigw.HttpMethod.GET],
  path           : apiRoutePath,
  integration    : apiIntegration
});

// FOR NEW INTEGRATION FOR EACH ROUTE:
apiGateway.addRoutes({
  methods        : [apigw.HttpMethod.GET],
  path           : apiRoutePath,
  integration    : new apigwint.HttpAlbIntegration({
    listener     : apiListener,
    vpcLink      : apiVpcLink
  });
});

@ayush987goyal
Copy link
Contributor Author

@nija-at The PR is still showing changes requested since no commit was pushed post the ask. Could you please take a look at the details now?

@nija-at nija-at dismissed their stale review January 18, 2021 15:34

addressed

@nija-at nija-at changed the title fix(apigatewayv2): multiple http integrations getting created for each route fix(apigatewayv2): multiple http integrations are created for each route Jan 20, 2021
Copy link
Contributor

@nija-at nija-at left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Let's try this out and see how it works. Since this is all internal APIs, we can always change later, if we find a better approach.

Some small comments below.

@ayush987goyal ayush987goyal requested a review from nija-at January 21, 2021 10:50
@mergify mergify bot dismissed nija-at’s stale review January 21, 2021 10:50

Pull request has been modified.

nija-at
nija-at previously approved these changes Jan 21, 2021
@mergify
Copy link
Contributor

mergify bot commented Jan 21, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot dismissed nija-at’s stale review January 21, 2021 17:49

Pull request has been modified.

@ayush987goyal
Copy link
Contributor Author

Hi @nija-at , another approval would probably be needed since there was a new release in b/w and I had to manually merge.

@ayush987goyal ayush987goyal requested a review from nija-at January 22, 2021 02:53
Copy link
Contributor

@nija-at nija-at left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You got it.

@mergify
Copy link
Contributor

mergify bot commented Jan 22, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: f669f85
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Jan 22, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 855ce59 into aws:master Jan 22, 2021
@ayush987goyal ayush987goyal deleted the pr/api-integ-fix branch January 22, 2021 16:33
@informity
Copy link

Thank you for addressing this, guys! Very much appreciate it. Any idea when this change will get into the official release?

mohanrajendran pushed a commit to mohanrajendran/aws-cdk that referenced this pull request Jan 24, 2021
…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*
@informity
Copy link

I just redeployed my API Gateway and... the issue with multiple integrations has been fixed! Awesome, thanks guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants