Skip to content

Commit 9f8bfa5

Browse files
author
Sam Goodwin
authored
feat(apigateway): support function alias in LambdaIntegration
Accept IFunction in LambdaIntegration instead of Function so that a lambda function Alias may be referenced as an API Gateway integration.
1 parent 5e91a0a commit 9f8bfa5

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

packages/@aws-cdk/aws-apigateway/lib/lambda-api.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface LambdaRestApiProps {
1212
* This handler will be used as a the default integration for all methods in
1313
* this API, unless specified otherwise in `addMethod`.
1414
*/
15-
handler: lambda.Function;
15+
handler: lambda.IFunction;
1616

1717
/**
1818
* If true, route all requests to the Lambda Function

packages/@aws-cdk/aws-apigateway/test/test.lambda-api.ts

+67
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,73 @@ export = {
7171
test.done();
7272
},
7373

74+
'LambdaRestApi supports function Alias'(test: Test) {
75+
// GIVEN
76+
const stack = new cdk.Stack();
77+
78+
const handler = new lambda.Function(stack, 'handler', {
79+
handler: 'index.handler',
80+
code: lambda.Code.inline('boom'),
81+
runtime: lambda.Runtime.NodeJS610,
82+
});
83+
const alias = new lambda.Alias(stack, 'alias', {
84+
aliasName: 'my-alias',
85+
version: new lambda.Version(stack, 'version', {
86+
lambda: handler
87+
})
88+
});
89+
90+
// WHEN
91+
const api = new apigw.LambdaRestApi(stack, 'lambda-rest-api', { handler: alias });
92+
93+
// THEN -- can't customize further
94+
test.throws(() => {
95+
api.root.addResource('cant-touch-this');
96+
});
97+
98+
// THEN -- template proxies everything
99+
expect(stack).to(haveResource('AWS::ApiGateway::Resource', {
100+
"PathPart": "{proxy+}"
101+
}));
102+
103+
expect(stack).to(haveResource('AWS::ApiGateway::Method', {
104+
"HttpMethod": "ANY",
105+
"ResourceId": {
106+
"Ref": "lambdarestapiproxyE3AE07E3"
107+
},
108+
"RestApiId": {
109+
"Ref": "lambdarestapiAAD10924"
110+
},
111+
"AuthorizationType": "NONE",
112+
"Integration": {
113+
"IntegrationHttpMethod": "POST",
114+
"Type": "AWS_PROXY",
115+
"Uri": {
116+
"Fn::Join": [
117+
"",
118+
[
119+
"arn:",
120+
{
121+
"Ref": "AWS::Partition"
122+
},
123+
":apigateway:",
124+
{
125+
"Ref": "AWS::Region"
126+
},
127+
":lambda:path/2015-03-31/functions/",
128+
{
129+
"Ref": "alias68BF17F5"
130+
},
131+
"/invocations"
132+
]
133+
]
134+
}
135+
}
136+
}));
137+
138+
test.done();
139+
},
140+
74141
'when "proxy" is set to false, users need to define the model'(test: Test) {
75142
// GIVEN
76143
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)