Skip to content

Commit

Permalink
fix(apigateway): Lambda integration for imported functions (#8870)
Browse files Browse the repository at this point in the history
Use `instanceof` to identify real functions vs imported ones and appropriately use whats needed to extract the function name.

Fixes #8869

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
iliapolo authored Jul 2, 2020
1 parent 202b329 commit 8420f96
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/@aws-cdk/aws-apigateway/lib/integrations/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
...bindResult,
Expand Down
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/test.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},

};

0 comments on commit 8420f96

Please sign in to comment.