aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

(apigatewayv2): AddRoutes caches lambda integrations incorrectly

Open ShadowDog007 opened this issue 5 years ago • 11 comments

When adding multiple routes with lambda integrations on a single http api, all routes end up sharing the same integration, instead of being correctly bound to the lambda which was provided.

Reproduction Steps

httpApi.AddRoutes({
  path: '/service1',
  integration: new LambdaProxyIntegration({
    handler: lambda1,
  })
});
httpApi.AddRoutes({
  path: '/service2',
  integration: new LambdaProxyIntegration({
    handler: lambda2,
  })
});

What did you expect to happen?

Two routes with their own integrations created

What actually happened?

Correct routes/lambda permissions, but both routes share the same lambda integration (the first one which was added)

Environment

  • CDK CLI Version : 1.90.1
  • Framework Version: 1.90.1
  • Node.js Version: 14.7.0
  • OS : Windows
  • Language (Version): dotnetcore (3.1)

Other

It appears that the Lambda arn doesn't become part of the cache key? https://github.com/aws/aws-cdk/blob/v1.90.1/packages/@aws-cdk/aws-apigatewayv2/lib/http/api.ts#L281


This is :bug: Bug Report

ShadowDog007 avatar Feb 23 '21 12:02 ShadowDog007

@ayush987goyal - can you take a look?

nija-at avatar Feb 25 '21 10:02 nija-at

Hi @ShadowDog007 ,

I am not able to reproduce the above issue with the below code:

import * as cdk from '@aws-cdk/core';
import { HttpApi } from '@aws-cdk/aws-apigatewayv2';
import * as integrations from '@aws-cdk/aws-apigatewayv2-integrations';
import * as lambda from '@aws-cdk/aws-lambda';

export class ApiIntegStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const lambda1 = new lambda.Function(this, 'AlwaysSuccess1', {
      runtime: lambda.Runtime.NODEJS_12_X,
      handler: 'index.handler',
      code: new lambda.InlineCode(
        'exports.handler = async function(event, context) { return { statusCode: 200, body: "success1" }; };'
      ),
    });
    const lambda2 = new lambda.Function(this, 'AlwaysSuccess2', {
      runtime: lambda.Runtime.NODEJS_12_X,
      handler: 'index.handler',
      code: new lambda.InlineCode(
        'exports.handler = async function(event, context) { return { statusCode: 200, body: "success2" }; };'
      ),
    });

    const httpApi = new HttpApi(this, 'LambdaProxyApi');
    httpApi.addRoutes({
      path: '/service1',
      integration: new integrations.LambdaProxyIntegration({
        handler: lambda1,
      }),
    });
    httpApi.addRoutes({
      path: '/service2',
      integration: new integrations.LambdaProxyIntegration({
        handler: lambda2,
      }),
    });

    new cdk.CfnOutput(this, 'Endpoint', {
      value: httpApi.url!,
    });
  }
}

Testing:

❯ curl https://<some-id>.execute-api.us-east-1.amazonaws.com/service1
success1%                                                                                                                                             
❯ curl https://<some-id>.execute-api.us-east-1.amazonaws.com/service2
success2%     

Could you please check if you are using the latest version of AWS CDK and also try out the above code? It would help if you could provide more details on you implementation to dive deep further.

ayush987goyal avatar Feb 25 '21 11:02 ayush987goyal

Hey @ayush987goyal I did some testing and struggled for a bit to reproduce it in a brand new application.

The specific reason why it was happening in my application was because the id of my Function was the same in two stacks.

The id of my functions were both 'Function', but in different stacks So the cache key doesn't consider the stack id??

class CdkAg2TestStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props: cdk.StackProps, lambdaStack1: LambdaStack, lambdaStack2: LambdaStack) {
    super(scope, id, props);

    const api = new apigateway.HttpApi(this, 'api');

    api.addRoutes({
      path: '/lambda1',
      integration: new apigatewayIntegrations.LambdaProxyIntegration({
        handler: lambdaStack1.lambda,
        payloadFormatVersion: apigateway.PayloadFormatVersion.VERSION_2_0
      })
    });

    api.addRoutes({
      path: '/lambda2',
      integration: new apigatewayIntegrations.LambdaProxyIntegration({
        handler: lambdaStack2.lambda,
        payloadFormatVersion: apigateway.PayloadFormatVersion.VERSION_2_0
      })
    });
  }
}

class LambdaStack extends cdk.Stack {
  lambda: lambda.Function;
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    this.lambda = new lambda.Function(this, 'lambda', {
      runtime: lambda.Runtime.NODEJS_12_X,
      handler: 'index.handler',
      code: lambda.Code.fromInline('exports.handler = async function(event) { };'),
    });
  }
}

const app = new cdk.App();
const lambdaStack1 = new LambdaStack(app, 'LambdaStack1');
const lambdaStack2 = new LambdaStack(app, 'LambdaStack2');
new CdkAg2TestStack(app, 'CdkAg2TestStack', {}, lambdaStack1, lambdaStack2);

ShadowDog007 avatar Feb 25 '21 12:02 ShadowDog007

It's mainly driven by the functionArn. So if the ARN for the lambda is same, then it will treat it as same integration.

ayush987goyal avatar Feb 25 '21 12:02 ayush987goyal

Right, but the ARN is based off the function name, but I'm letting CloudFormation generate one (which bases it off the stack name and resource name). So the ARN's are different? (Just not known at synth time)

ShadowDog007 avatar Feb 25 '21 12:02 ShadowDog007

Yeah ideally they should be different if they are in different stacks. I am not really sure what specific scenario you have in your local workspace. If you could provide a reproduction then we could further look into it.

ayush987goyal avatar Feb 25 '21 12:02 ayush987goyal

@ayush987goyal Created this: https://github.com/ShadowDog007/aws-cdk-apigatewayv2-integration-issue-reproduction

Added the output here too, which shows only a single integration where there should be two. https://github.com/ShadowDog007/aws-cdk-apigatewayv2-integration-issue-reproduction/blob/master/bad-cdk.out/CdkAg2TestStack.template.json#L64

ShadowDog007 avatar Feb 25 '21 12:02 ShadowDog007

I have reproduced it locally and can confirm this is indeed a bug. We will look into the same.

ayush987goyal avatar Feb 25 '21 13:02 ayush987goyal

To confirm, this happens when the function name is the same but defined in different stacks. Is that right?

nija-at avatar Feb 25 '21 14:02 nija-at

Yes just confirmed that this only happens when the function name is same and defined in different stacks.

ayush987goyal avatar Feb 25 '21 15:02 ayush987goyal

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Nov 26 '21 14:11 github-actions[bot]