Skip to content

Commit

Permalink
feat(apigateway): support function alias in LambdaIntegration
Browse files Browse the repository at this point in the history
Accept IFunction in LambdaIntegration instead of Function so that a lambda function Alias may be referenced as an API Gateway integration.
  • Loading branch information
Sam Goodwin committed Jan 23, 2019
1 parent 5e91a0a commit 9f8bfa5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-apigateway/lib/lambda-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface LambdaRestApiProps {
* This handler will be used as a the default integration for all methods in
* this API, unless specified otherwise in `addMethod`.
*/
handler: lambda.Function;
handler: lambda.IFunction;

/**
* If true, route all requests to the Lambda Function
Expand Down
67 changes: 67 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/test.lambda-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,73 @@ export = {
test.done();
},

'LambdaRestApi supports function Alias'(test: Test) {
// GIVEN
const stack = new cdk.Stack();

const handler = new lambda.Function(stack, 'handler', {
handler: 'index.handler',
code: lambda.Code.inline('boom'),
runtime: lambda.Runtime.NodeJS610,
});
const alias = new lambda.Alias(stack, 'alias', {
aliasName: 'my-alias',
version: new lambda.Version(stack, 'version', {
lambda: handler
})
});

// WHEN
const api = new apigw.LambdaRestApi(stack, 'lambda-rest-api', { handler: alias });

// THEN -- can't customize further
test.throws(() => {
api.root.addResource('cant-touch-this');
});

// THEN -- template proxies everything
expect(stack).to(haveResource('AWS::ApiGateway::Resource', {
"PathPart": "{proxy+}"
}));

expect(stack).to(haveResource('AWS::ApiGateway::Method', {
"HttpMethod": "ANY",
"ResourceId": {
"Ref": "lambdarestapiproxyE3AE07E3"
},
"RestApiId": {
"Ref": "lambdarestapiAAD10924"
},
"AuthorizationType": "NONE",
"Integration": {
"IntegrationHttpMethod": "POST",
"Type": "AWS_PROXY",
"Uri": {
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":apigateway:",
{
"Ref": "AWS::Region"
},
":lambda:path/2015-03-31/functions/",
{
"Ref": "alias68BF17F5"
},
"/invocations"
]
]
}
}
}));

test.done();
},

'when "proxy" is set to false, users need to define the model'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 9f8bfa5

Please sign in to comment.