diff --git a/CHANGELOG.md b/CHANGELOG.md index bc994dad74fda..4d95ed89c4daa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [1.49.1](https://github.com/aws/aws-cdk/compare/v1.49.0...v1.49.1) (2020-07-02) + +### Bug Fixes + +* **apigateway:** Lambda integration for imported functions ([#8870](https://github.com/aws/aws-cdk/issues/8870)) ([c017f88](https://github.com/aws/aws-cdk/commit/c017f887770174437de3b772edf0034604890ac3)), closes [#8869](https://github.com/aws/aws-cdk/issues/8869) + ## [1.49.0](https://github.com/aws/aws-cdk/compare/v1.48.0...v1.49.0) (2020-07-02) diff --git a/lerna.json b/lerna.json index 0c9388ceb22ac..2babe6847f5c3 100644 --- a/lerna.json +++ b/lerna.json @@ -10,5 +10,5 @@ "tools/*" ], "rejectCycles": "true", - "version": "1.49.0" + "version": "1.49.1" } diff --git a/packages/@aws-cdk/aws-apigateway/lib/integrations/lambda.ts b/packages/@aws-cdk/aws-apigateway/lib/integrations/lambda.ts index 715c1d91237fe..60283b75d2fad 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/integrations/lambda.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/integrations/lambda.ts @@ -73,10 +73,20 @@ export class LambdaIntegration extends AwsIntegration { }); } - const cfnFunction = this.handler.node.defaultChild as lambda.CfnFunction; + let functionName; + + if (this.handler instanceof lambda.Function) { + // if not imported, extract the name from the CFN layer to reach + // the literal value if it is given (rather than a token) + functionName = (this.handler.node.defaultChild as lambda.CfnFunction).functionName; + } else { + // imported, just take the function name. + functionName = this.handler.functionName; + } + let deploymentToken; - if (!Token.isUnresolved(cfnFunction.functionName)) { - deploymentToken = JSON.stringify({ functionName: cfnFunction.functionName }); + if (!Token.isUnresolved(functionName)) { + deploymentToken = JSON.stringify({ functionName }); } return { deploymentToken, diff --git a/packages/@aws-cdk/aws-apigateway/test/test.lambda.ts b/packages/@aws-cdk/aws-apigateway/test/test.lambda.ts index afc1a2230e848..b518db5a29bc9 100644 --- a/packages/@aws-cdk/aws-apigateway/test/test.lambda.ts +++ b/packages/@aws-cdk/aws-apigateway/test/test.lambda.ts @@ -259,4 +259,23 @@ export = { test.done(); }, + + 'bind works for integration with imported functions'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const restapi = new apigateway.RestApi(stack, 'RestApi'); + const method = restapi.root.addMethod('ANY'); + const handler = lambda.Function.fromFunctionArn(stack, 'MyFunc', 'arn:aws:lambda:region:account:function:myfunc'); + const integration = new apigateway.LambdaIntegration(handler); + + // WHEN + const bindResult = integration.bind(method); + + // the deployment token should be defined since the function name + // should be a literal string. + test.equal(bindResult?.deploymentToken, JSON.stringify({functionName: 'myfunc'})); + + test.done(); + }, + };