-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
lambda-api.ts
98 lines (86 loc) · 3.27 KB
/
lambda-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// import * as cdk from '../../core';
import { Construct } from 'constructs';
import { LambdaIntegration, LambdaIntegrationOptions } from './integrations';
import { Method } from './method';
import { ProxyResource, Resource } from './resource';
import { RestApi, RestApiProps } from './restapi';
import * as lambda from '../../aws-lambda';
export interface LambdaRestApiProps extends RestApiProps {
/**
* The default Lambda function that handles all requests from this API.
*
* This handler will be used as a the default integration for all methods in
* this API, unless specified otherwise in `addMethod`.
*/
readonly handler: lambda.IFunction;
/**
* Specific Lambda integration options.
*
* @default see defaults defined in `LambdaIntegrationOptions`.
*/
readonly integrationOptions?: LambdaIntegrationOptions;
/**
* If true, route all requests to the Lambda Function
*
* If set to false, you will need to explicitly define the API model using
* `addResource` and `addMethod` (or `addProxy`).
*
* @default true
*/
readonly proxy?: boolean;
/**
* @deprecated the `LambdaRestApiProps` now extends `RestApiProps`, so all
* options are just available here. Note that the options specified in
* `options` will be overridden by any props specified at the root level.
*
* @default - no options.
*/
readonly options?: RestApiProps;
}
/**
* Defines an API Gateway REST API with AWS Lambda proxy integration.
*
* Use the `proxy` property to define a greedy proxy ("{proxy+}") and "ANY"
* method from the specified path. If not defined, you will need to explicity
* add resources and methods to the API.
*/
export class LambdaRestApi extends RestApi {
constructor(scope: Construct, id: string, props: LambdaRestApiProps) {
if (props.options?.defaultIntegration || props.defaultIntegration) {
throw new Error('Cannot specify "defaultIntegration" since Lambda integration is automatically defined');
}
super(scope, id, {
defaultIntegration: new LambdaIntegration(props.handler, props.integrationOptions),
...props.options, // deprecated, but we still support
...props,
});
if (props.proxy !== false) {
this.root.addProxy();
// Make sure users cannot call any other resource adding function
this.root.addResource = addResourceThrows;
this.root.addMethod = addMethodThrows;
this.root.addProxy = addProxyThrows;
}
this.node.addValidation({
validate() {
for (const value of Object.values(props.deployOptions?.variables ?? {})) {
// Checks that variable Stage values match regex
const regexp = /[A-Za-z0-9-._~:/?#&=,]+/;
if (value.match(regexp) === null) {
return ['Stage variable value ' + value + ' does not match the regex.'];
}
}
return [];
},
});
}
}
function addResourceThrows(): Resource {
throw new Error('Cannot call \'addResource\' on a proxying LambdaRestApi; set \'proxy\' to false');
}
function addMethodThrows(): Method {
throw new Error('Cannot call \'addMethod\' on a proxying LambdaRestApi; set \'proxy\' to false');
}
function addProxyThrows(): ProxyResource {
throw new Error('Cannot call \'addProxy\' on a proxying LambdaRestApi; set \'proxy\' to false');
}