@gradientedge/cdk-utils 6.10.3 → 6.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/lib/construct/api-to-eventbridge-target/main.d.ts +3 -3
- package/dist/src/lib/construct/api-to-eventbridge-target/main.js +4 -3
- package/dist/src/lib/construct/api-to-lambda-target/main.d.ts +35 -2
- package/dist/src/lib/construct/api-to-lambda-target/main.js +127 -15
- package/dist/src/lib/helper/api-to-lambda-target-rest-api.d.ts +2 -0
- package/dist/src/lib/helper/api-to-lambda-target-rest-api.js +1 -0
- package/dist/src/lib/types/aws/index.d.ts +5 -0
- package/package.json +1 -1
- package/src/lib/construct/api-to-eventbridge-target/main.ts +4 -3
- package/src/lib/construct/api-to-lambda-target/main.ts +140 -19
- package/src/lib/helper/api-to-lambda-target-rest-api.ts +2 -0
- package/src/lib/types/aws/index.ts +5 -0
|
@@ -131,17 +131,17 @@ export declare class ApiToEventBridgeTarget extends CommonConstruct {
|
|
|
131
131
|
*/
|
|
132
132
|
protected createApiToEventBridgeTargetResourceMethod(): void;
|
|
133
133
|
/**
|
|
134
|
-
* @summary Method to create custom restApi domain for Api
|
|
134
|
+
* @summary Method to create custom restApi domain for Api
|
|
135
135
|
* @protected
|
|
136
136
|
*/
|
|
137
137
|
protected createApiDomain(): void;
|
|
138
138
|
/**
|
|
139
|
-
* @summary Method to create base path mappings for Api
|
|
139
|
+
* @summary Method to create base path mappings for Api
|
|
140
140
|
* @protected
|
|
141
141
|
*/
|
|
142
142
|
protected createApiBasePathMapping(): void;
|
|
143
143
|
/**
|
|
144
|
-
* @summary Method to create route53 records for Api
|
|
144
|
+
* @summary Method to create route53 records for Api
|
|
145
145
|
* @protected
|
|
146
146
|
*/
|
|
147
147
|
protected createApiRouteAssets(): void;
|
|
@@ -364,6 +364,7 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
|
|
|
364
364
|
this.apiToEventBridgeTargetRestApi.methodErrorResponse,
|
|
365
365
|
],
|
|
366
366
|
},
|
|
367
|
+
deploy: this.props.api.restApi?.deploy ?? true,
|
|
367
368
|
deployOptions: {
|
|
368
369
|
tracingEnabled: this.props.api.restApi?.deployOptions?.tracingEnabled,
|
|
369
370
|
dataTraceEnabled: this.props.api.restApi?.deployOptions?.dataTraceEnabled,
|
|
@@ -472,7 +473,7 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
|
|
|
472
473
|
});
|
|
473
474
|
}
|
|
474
475
|
/**
|
|
475
|
-
* @summary Method to create custom restApi domain for Api
|
|
476
|
+
* @summary Method to create custom restApi domain for Api
|
|
476
477
|
* @protected
|
|
477
478
|
*/
|
|
478
479
|
createApiDomain() {
|
|
@@ -483,7 +484,7 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
|
|
|
483
484
|
: `${this.props.apiSubDomain}-${this.props.stage}.${this.fullyQualifiedDomainName}`, this.apiToEventBridgeTargetRestApi.certificate);
|
|
484
485
|
}
|
|
485
486
|
/**
|
|
486
|
-
* @summary Method to create base path mappings for Api
|
|
487
|
+
* @summary Method to create base path mappings for Api
|
|
487
488
|
* @protected
|
|
488
489
|
*/
|
|
489
490
|
createApiBasePathMapping() {
|
|
@@ -497,7 +498,7 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
|
|
|
497
498
|
});
|
|
498
499
|
}
|
|
499
500
|
/**
|
|
500
|
-
* @summary Method to create route53 records for Api
|
|
501
|
+
* @summary Method to create route53 records for Api
|
|
501
502
|
* @protected
|
|
502
503
|
*/
|
|
503
504
|
createApiRouteAssets() {
|
|
@@ -7,6 +7,7 @@ export declare class ApiToLambdaTarget extends CommonConstruct {
|
|
|
7
7
|
id: string;
|
|
8
8
|
applicationSecrets: secretsmanager.ISecret[];
|
|
9
9
|
apiToLambdaTargetRestApi: types.ApiToLambdaTargetRestApiType;
|
|
10
|
+
apiResource: string;
|
|
10
11
|
constructor(parent: Construct, id: string, props: types.ApiToLambdaTargetProps);
|
|
11
12
|
protected initResources(): void;
|
|
12
13
|
/**
|
|
@@ -25,18 +26,50 @@ export declare class ApiToLambdaTarget extends CommonConstruct {
|
|
|
25
26
|
* @protected
|
|
26
27
|
*/
|
|
27
28
|
protected resolveCertificate(): void;
|
|
29
|
+
/**
|
|
30
|
+
* @summary Method to create api integration method response
|
|
31
|
+
* @protected
|
|
32
|
+
*/
|
|
33
|
+
protected createApiToLambdaTargetMethodResponse(): void;
|
|
34
|
+
/**
|
|
35
|
+
* @summary Method to create api integration method error response
|
|
36
|
+
* @protected
|
|
37
|
+
*/
|
|
38
|
+
protected createApiToLambdaTargetMethodErrorResponse(): void;
|
|
39
|
+
protected resolveApiToLambdaTargetFunction(): void;
|
|
28
40
|
/**
|
|
29
41
|
* @summary Method to create rest restApi for Api
|
|
30
42
|
* @protected
|
|
31
43
|
*/
|
|
32
44
|
protected createApiToLambdaTargetRestApi(): void;
|
|
33
45
|
/**
|
|
34
|
-
* @summary Method to create
|
|
46
|
+
* @summary Method to create api integration resource
|
|
47
|
+
* @protected
|
|
48
|
+
*/
|
|
49
|
+
protected createApiToLambdaTargetResource(): void;
|
|
50
|
+
protected createApiToLambdaTargetPolicy(): void;
|
|
51
|
+
/**
|
|
52
|
+
* @summary Method to create a role for api integration
|
|
53
|
+
* @protected
|
|
54
|
+
*/
|
|
55
|
+
protected createApiToLambdaTargetRole(): void;
|
|
56
|
+
/**
|
|
57
|
+
* @summary Method to create api integration resource method
|
|
58
|
+
* @protected
|
|
59
|
+
*/
|
|
60
|
+
protected createApiToLambdaTargetIntegration(): void;
|
|
61
|
+
/**
|
|
62
|
+
* @summary Method to create api integration resource method
|
|
63
|
+
* @protected
|
|
64
|
+
*/
|
|
65
|
+
protected createApiToLambdaTargetResourceMethod(): void;
|
|
66
|
+
/**
|
|
67
|
+
* @summary Method to create custom restApi domain for Api
|
|
35
68
|
* @protected
|
|
36
69
|
*/
|
|
37
70
|
protected createApiDomain(): void;
|
|
38
71
|
/**
|
|
39
|
-
* @summary Method to create base path mappings for
|
|
72
|
+
* @summary Method to create base path mappings for Api
|
|
40
73
|
* @protected
|
|
41
74
|
*/
|
|
42
75
|
protected createApiBasePathMapping(): void;
|
|
@@ -26,6 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.ApiToLambdaTarget = void 0;
|
|
27
27
|
const cdk = __importStar(require("aws-cdk-lib"));
|
|
28
28
|
const apig = __importStar(require("aws-cdk-lib/aws-apigateway"));
|
|
29
|
+
const iam = __importStar(require("aws-cdk-lib/aws-iam"));
|
|
29
30
|
const lambda = __importStar(require("aws-cdk-lib/aws-lambda"));
|
|
30
31
|
const common_1 = require("../../common");
|
|
31
32
|
const helper = __importStar(require("../../helper"));
|
|
@@ -36,6 +37,7 @@ class ApiToLambdaTarget extends common_1.CommonConstruct {
|
|
|
36
37
|
applicationSecrets;
|
|
37
38
|
/* rest restApi related resources */
|
|
38
39
|
apiToLambdaTargetRestApi;
|
|
40
|
+
apiResource;
|
|
39
41
|
constructor(parent, id, props) {
|
|
40
42
|
super(parent, id, props);
|
|
41
43
|
this.props = props;
|
|
@@ -49,7 +51,15 @@ class ApiToLambdaTarget extends common_1.CommonConstruct {
|
|
|
49
51
|
this.resolveHostedZone();
|
|
50
52
|
this.resolveCertificate();
|
|
51
53
|
/* restApi related resources */
|
|
54
|
+
this.createApiToLambdaTargetMethodResponse();
|
|
55
|
+
this.createApiToLambdaTargetMethodErrorResponse();
|
|
56
|
+
this.resolveApiToLambdaTargetFunction();
|
|
52
57
|
this.createApiToLambdaTargetRestApi();
|
|
58
|
+
this.createApiToLambdaTargetResource();
|
|
59
|
+
this.createApiToLambdaTargetPolicy();
|
|
60
|
+
this.createApiToLambdaTargetRole();
|
|
61
|
+
this.createApiToLambdaTargetIntegration();
|
|
62
|
+
this.createApiToLambdaTargetResourceMethod();
|
|
53
63
|
this.createApiDomain();
|
|
54
64
|
this.createApiBasePathMapping();
|
|
55
65
|
this.createApiRouteAssets();
|
|
@@ -83,6 +93,47 @@ class ApiToLambdaTarget extends common_1.CommonConstruct {
|
|
|
83
93
|
}
|
|
84
94
|
this.apiToLambdaTargetRestApi.certificate = this.acmManager.resolveCertificate(`${this.id}-certificate`, this, this.props.api.certificate);
|
|
85
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* @summary Method to create api integration method response
|
|
98
|
+
* @protected
|
|
99
|
+
*/
|
|
100
|
+
createApiToLambdaTargetMethodResponse() {
|
|
101
|
+
if (!this.props.api.withResource)
|
|
102
|
+
return;
|
|
103
|
+
this.apiToLambdaTargetRestApi.methodResponse = {
|
|
104
|
+
...{
|
|
105
|
+
statusCode: '200',
|
|
106
|
+
responseParameters: {
|
|
107
|
+
'method.response.header.Content-Type': true,
|
|
108
|
+
'method.response.header.Access-Control-Allow-Origin': true,
|
|
109
|
+
'method.response.header.Access-Control-Allow-Credentials': true,
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
...this.props.api.methodResponse,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* @summary Method to create api integration method error response
|
|
117
|
+
* @protected
|
|
118
|
+
*/
|
|
119
|
+
createApiToLambdaTargetMethodErrorResponse() {
|
|
120
|
+
if (!this.props.api.withResource)
|
|
121
|
+
return;
|
|
122
|
+
this.apiToLambdaTargetRestApi.methodErrorResponse = {
|
|
123
|
+
...{
|
|
124
|
+
statusCode: '400',
|
|
125
|
+
responseParameters: {
|
|
126
|
+
'method.response.header.Content-Type': true,
|
|
127
|
+
'method.response.header.Access-Control-Allow-Origin': true,
|
|
128
|
+
'method.response.header.Access-Control-Allow-Credentials': true,
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
...this.props.api.methodErrorResponse,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
resolveApiToLambdaTargetFunction() {
|
|
135
|
+
this.apiToLambdaTargetRestApi.lambda = lambda.Function.fromFunctionName(this, `${this.id}-lambda`, this.props.lambdaFunctionName);
|
|
136
|
+
}
|
|
86
137
|
/**
|
|
87
138
|
* @summary Method to create rest restApi for Api
|
|
88
139
|
* @protected
|
|
@@ -99,17 +150,88 @@ class ApiToLambdaTarget extends common_1.CommonConstruct {
|
|
|
99
150
|
this.props.api.restApi = {
|
|
100
151
|
...this.props.api.restApi,
|
|
101
152
|
...{
|
|
153
|
+
defaultMethodOptions: {
|
|
154
|
+
methodResponses: [
|
|
155
|
+
this.apiToLambdaTargetRestApi.methodResponse,
|
|
156
|
+
this.apiToLambdaTargetRestApi.methodErrorResponse,
|
|
157
|
+
],
|
|
158
|
+
},
|
|
102
159
|
deployOptions: {
|
|
103
160
|
accessLogDestination: new apig.LogGroupLogDestination(accessLogGroup),
|
|
104
161
|
},
|
|
105
162
|
},
|
|
106
163
|
};
|
|
107
|
-
this.apiToLambdaTargetRestApi.api = this.apiManager.createLambdaRestApi(`${this.id}-lambda-rest-api`, this, this.props.api.restApi,
|
|
164
|
+
this.apiToLambdaTargetRestApi.api = this.apiManager.createLambdaRestApi(`${this.id}-lambda-rest-api`, this, this.props.api.restApi, this.apiToLambdaTargetRestApi.lambda);
|
|
108
165
|
this.addCfnOutput(`${this.id}-restApiId`, this.apiToLambdaTargetRestApi.api.restApiId);
|
|
109
166
|
this.addCfnOutput(`${this.id}-restApiRootResourceId`, this.apiToLambdaTargetRestApi.api.root.resourceId);
|
|
110
167
|
}
|
|
111
168
|
/**
|
|
112
|
-
* @summary Method to create
|
|
169
|
+
* @summary Method to create api integration resource
|
|
170
|
+
* @protected
|
|
171
|
+
*/
|
|
172
|
+
createApiToLambdaTargetResource() {
|
|
173
|
+
if (!this.props.api.withResource)
|
|
174
|
+
return;
|
|
175
|
+
let rootResource;
|
|
176
|
+
if (this.props.api.withResource && this.props.api.importedRestApiRootResourceRef) {
|
|
177
|
+
rootResource = apig.Resource.fromResourceAttributes(this, `${this.id}-root-resource`, {
|
|
178
|
+
resourceId: cdk.Fn.importValue(this.props.api.importedRestApiRootResourceRef),
|
|
179
|
+
restApi: this.apiToLambdaTargetRestApi.api,
|
|
180
|
+
path: '/',
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
rootResource = this.apiToLambdaTargetRestApi.api.root;
|
|
185
|
+
}
|
|
186
|
+
this.apiToLambdaTargetRestApi.resource = rootResource.addResource(this.props.api.resource ?? this.apiResource);
|
|
187
|
+
}
|
|
188
|
+
createApiToLambdaTargetPolicy() {
|
|
189
|
+
this.apiToLambdaTargetRestApi.policy = new iam.PolicyDocument({
|
|
190
|
+
statements: [
|
|
191
|
+
this.iamManager.statementForPutEvents(),
|
|
192
|
+
this.iamManager.statementForInvokeLambda([this.apiToLambdaTargetRestApi.lambda.functionArn]),
|
|
193
|
+
],
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* @summary Method to create a role for api integration
|
|
198
|
+
* @protected
|
|
199
|
+
*/
|
|
200
|
+
createApiToLambdaTargetRole() {
|
|
201
|
+
if (!this.apiToLambdaTargetRestApi.policy)
|
|
202
|
+
throw 'Policy undefined';
|
|
203
|
+
this.apiToLambdaTargetRestApi.role = new iam.Role(this, `${this.id}-rest-api-role`, {
|
|
204
|
+
assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com'),
|
|
205
|
+
inlinePolicies: { policy: this.apiToLambdaTargetRestApi.policy },
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* @summary Method to create api integration resource method
|
|
210
|
+
* @protected
|
|
211
|
+
*/
|
|
212
|
+
createApiToLambdaTargetIntegration() {
|
|
213
|
+
this.apiToLambdaTargetRestApi.integration = new apig.LambdaIntegration(this.apiToLambdaTargetRestApi.lambda, {
|
|
214
|
+
allowTestInvoke: true,
|
|
215
|
+
credentialsRole: this.apiToLambdaTargetRestApi.role,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* @summary Method to create api integration resource method
|
|
220
|
+
* @protected
|
|
221
|
+
*/
|
|
222
|
+
createApiToLambdaTargetResourceMethod() {
|
|
223
|
+
if (!this.props.api.withResource)
|
|
224
|
+
return;
|
|
225
|
+
this.apiToLambdaTargetRestApi.method = this.apiToLambdaTargetRestApi.resource.addMethod('POST', this.apiToLambdaTargetRestApi.integration, {
|
|
226
|
+
authorizer: this.apiToLambdaTargetRestApi.authoriser,
|
|
227
|
+
methodResponses: [
|
|
228
|
+
this.apiToLambdaTargetRestApi.methodResponse,
|
|
229
|
+
this.apiToLambdaTargetRestApi.methodErrorResponse,
|
|
230
|
+
],
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* @summary Method to create custom restApi domain for Api
|
|
113
235
|
* @protected
|
|
114
236
|
*/
|
|
115
237
|
createApiDomain() {
|
|
@@ -120,24 +242,14 @@ class ApiToLambdaTarget extends common_1.CommonConstruct {
|
|
|
120
242
|
: `${this.props.apiSubDomain}-${this.props.stage}.${this.fullyQualifiedDomainName}`, this.apiToLambdaTargetRestApi.certificate);
|
|
121
243
|
}
|
|
122
244
|
/**
|
|
123
|
-
* @summary Method to create base path mappings for
|
|
245
|
+
* @summary Method to create base path mappings for Api
|
|
124
246
|
* @protected
|
|
125
247
|
*/
|
|
126
248
|
createApiBasePathMapping() {
|
|
127
|
-
|
|
128
|
-
if (apiRootPaths && apiRootPaths.length > 0) {
|
|
129
|
-
apiRootPaths.forEach((apiRootPath) => {
|
|
130
|
-
this.apiToLambdaTargetRestApi.basePathMappings.push(new apig.BasePathMapping(this, `${this.id}-base-bath-mapping-${apiRootPath}`, {
|
|
131
|
-
basePath: apiRootPath,
|
|
132
|
-
domainName: this.apiToLambdaTargetRestApi.domain,
|
|
133
|
-
restApi: this.apiToLambdaTargetRestApi.api,
|
|
134
|
-
stage: this.apiToLambdaTargetRestApi.api.deploymentStage,
|
|
135
|
-
}));
|
|
136
|
-
});
|
|
249
|
+
if (this.props.api.useExisting)
|
|
137
250
|
return;
|
|
138
|
-
}
|
|
139
|
-
// add default mapping if apiRootPaths not set
|
|
140
251
|
new apig.BasePathMapping(this, `${this.id}-base-bath-mapping`, {
|
|
252
|
+
basePath: '',
|
|
141
253
|
domainName: this.apiToLambdaTargetRestApi.domain,
|
|
142
254
|
restApi: this.apiToLambdaTargetRestApi.api,
|
|
143
255
|
stage: this.apiToLambdaTargetRestApi.api.deploymentStage,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as apig from 'aws-cdk-lib/aws-apigateway';
|
|
2
2
|
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
|
|
3
3
|
import * as iam from 'aws-cdk-lib/aws-iam';
|
|
4
|
+
import * as lambda from 'aws-cdk-lib/aws-lambda';
|
|
4
5
|
import * as route53 from 'aws-cdk-lib/aws-route53';
|
|
5
6
|
import * as sns from 'aws-cdk-lib/aws-sns';
|
|
6
7
|
import * as types from '../types/aws';
|
|
@@ -26,6 +27,7 @@ export declare class ApiToLambdaTargetRestApi implements types.ApiToLambdaTarget
|
|
|
26
27
|
[p: string]: string;
|
|
27
28
|
};
|
|
28
29
|
integrationResponse: apig.IntegrationResponse;
|
|
30
|
+
lambda: lambda.IFunction;
|
|
29
31
|
method: apig.Method;
|
|
30
32
|
methodErrorResponse: apig.MethodResponse;
|
|
31
33
|
methodResponse: apig.MethodResponse;
|
|
@@ -293,6 +293,7 @@ export interface ApiToLambdaTargetRestApiType {
|
|
|
293
293
|
[p: string]: string;
|
|
294
294
|
};
|
|
295
295
|
integrationResponse: apig.IntegrationResponse;
|
|
296
|
+
lambda: lambda.IFunction;
|
|
296
297
|
method: apig.Method;
|
|
297
298
|
methodErrorResponse: apig.MethodResponse;
|
|
298
299
|
methodResponse: apig.MethodResponse;
|
|
@@ -380,11 +381,15 @@ export interface ApiToEventBridgeTargetProps extends CommonStackProps {
|
|
|
380
381
|
* @subcategory Properties
|
|
381
382
|
*/
|
|
382
383
|
export interface ApiToLambdaTargetRestApiProps {
|
|
384
|
+
resource: string;
|
|
383
385
|
certificate: AcmProps;
|
|
384
386
|
restApi: apig.LambdaRestApiProps;
|
|
385
387
|
importedRestApiRef?: string;
|
|
386
388
|
importedRestApiRootResourceRef?: string;
|
|
387
389
|
useExisting: boolean;
|
|
390
|
+
withResource?: boolean;
|
|
391
|
+
methodErrorResponse: apig.MethodResponse;
|
|
392
|
+
methodResponse: apig.MethodResponse;
|
|
388
393
|
}
|
|
389
394
|
/**
|
|
390
395
|
* @category cdk-utils.api-to-lambda-target
|
package/package.json
CHANGED
|
@@ -391,6 +391,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
|
|
|
391
391
|
this.apiToEventBridgeTargetRestApi.methodErrorResponse,
|
|
392
392
|
],
|
|
393
393
|
},
|
|
394
|
+
deploy: this.props.api.restApi?.deploy ?? true,
|
|
394
395
|
deployOptions: {
|
|
395
396
|
tracingEnabled: this.props.api.restApi?.deployOptions?.tracingEnabled,
|
|
396
397
|
dataTraceEnabled: this.props.api.restApi?.deployOptions?.dataTraceEnabled,
|
|
@@ -505,7 +506,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
|
|
|
505
506
|
}
|
|
506
507
|
|
|
507
508
|
/**
|
|
508
|
-
* @summary Method to create custom restApi domain for Api
|
|
509
|
+
* @summary Method to create custom restApi domain for Api
|
|
509
510
|
* @protected
|
|
510
511
|
*/
|
|
511
512
|
protected createApiDomain() {
|
|
@@ -521,7 +522,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
|
|
|
521
522
|
}
|
|
522
523
|
|
|
523
524
|
/**
|
|
524
|
-
* @summary Method to create base path mappings for Api
|
|
525
|
+
* @summary Method to create base path mappings for Api
|
|
525
526
|
* @protected
|
|
526
527
|
*/
|
|
527
528
|
protected createApiBasePathMapping() {
|
|
@@ -535,7 +536,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
|
|
|
535
536
|
}
|
|
536
537
|
|
|
537
538
|
/**
|
|
538
|
-
* @summary Method to create route53 records for Api
|
|
539
|
+
* @summary Method to create route53 records for Api
|
|
539
540
|
* @protected
|
|
540
541
|
*/
|
|
541
542
|
protected createApiRouteAssets() {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as cdk from 'aws-cdk-lib'
|
|
2
2
|
import * as apig from 'aws-cdk-lib/aws-apigateway'
|
|
3
|
+
import * as iam from 'aws-cdk-lib/aws-iam'
|
|
3
4
|
import * as lambda from 'aws-cdk-lib/aws-lambda'
|
|
4
5
|
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager'
|
|
5
6
|
import { Construct } from 'constructs'
|
|
@@ -16,6 +17,7 @@ export class ApiToLambdaTarget extends CommonConstruct {
|
|
|
16
17
|
|
|
17
18
|
/* rest restApi related resources */
|
|
18
19
|
apiToLambdaTargetRestApi: types.ApiToLambdaTargetRestApiType
|
|
20
|
+
apiResource: string
|
|
19
21
|
|
|
20
22
|
constructor(parent: Construct, id: string, props: types.ApiToLambdaTargetProps) {
|
|
21
23
|
super(parent, id, props)
|
|
@@ -35,7 +37,15 @@ export class ApiToLambdaTarget extends CommonConstruct {
|
|
|
35
37
|
this.resolveCertificate()
|
|
36
38
|
|
|
37
39
|
/* restApi related resources */
|
|
40
|
+
this.createApiToLambdaTargetMethodResponse()
|
|
41
|
+
this.createApiToLambdaTargetMethodErrorResponse()
|
|
42
|
+
this.resolveApiToLambdaTargetFunction()
|
|
38
43
|
this.createApiToLambdaTargetRestApi()
|
|
44
|
+
this.createApiToLambdaTargetResource()
|
|
45
|
+
this.createApiToLambdaTargetPolicy()
|
|
46
|
+
this.createApiToLambdaTargetRole()
|
|
47
|
+
this.createApiToLambdaTargetIntegration()
|
|
48
|
+
this.createApiToLambdaTargetResourceMethod()
|
|
39
49
|
this.createApiDomain()
|
|
40
50
|
this.createApiBasePathMapping()
|
|
41
51
|
this.createApiRouteAssets()
|
|
@@ -88,6 +98,52 @@ export class ApiToLambdaTarget extends CommonConstruct {
|
|
|
88
98
|
)
|
|
89
99
|
}
|
|
90
100
|
|
|
101
|
+
/**
|
|
102
|
+
* @summary Method to create api integration method response
|
|
103
|
+
* @protected
|
|
104
|
+
*/
|
|
105
|
+
protected createApiToLambdaTargetMethodResponse() {
|
|
106
|
+
if (!this.props.api.withResource) return
|
|
107
|
+
this.apiToLambdaTargetRestApi.methodResponse = {
|
|
108
|
+
...{
|
|
109
|
+
statusCode: '200',
|
|
110
|
+
responseParameters: {
|
|
111
|
+
'method.response.header.Content-Type': true,
|
|
112
|
+
'method.response.header.Access-Control-Allow-Origin': true,
|
|
113
|
+
'method.response.header.Access-Control-Allow-Credentials': true,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
...this.props.api.methodResponse,
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @summary Method to create api integration method error response
|
|
122
|
+
* @protected
|
|
123
|
+
*/
|
|
124
|
+
protected createApiToLambdaTargetMethodErrorResponse() {
|
|
125
|
+
if (!this.props.api.withResource) return
|
|
126
|
+
this.apiToLambdaTargetRestApi.methodErrorResponse = {
|
|
127
|
+
...{
|
|
128
|
+
statusCode: '400',
|
|
129
|
+
responseParameters: {
|
|
130
|
+
'method.response.header.Content-Type': true,
|
|
131
|
+
'method.response.header.Access-Control-Allow-Origin': true,
|
|
132
|
+
'method.response.header.Access-Control-Allow-Credentials': true,
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
...this.props.api.methodErrorResponse,
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
protected resolveApiToLambdaTargetFunction() {
|
|
140
|
+
this.apiToLambdaTargetRestApi.lambda = lambda.Function.fromFunctionName(
|
|
141
|
+
this,
|
|
142
|
+
`${this.id}-lambda`,
|
|
143
|
+
this.props.lambdaFunctionName
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
|
|
91
147
|
/**
|
|
92
148
|
* @summary Method to create rest restApi for Api
|
|
93
149
|
* @protected
|
|
@@ -110,6 +166,12 @@ export class ApiToLambdaTarget extends CommonConstruct {
|
|
|
110
166
|
this.props.api.restApi = {
|
|
111
167
|
...this.props.api.restApi,
|
|
112
168
|
...{
|
|
169
|
+
defaultMethodOptions: {
|
|
170
|
+
methodResponses: [
|
|
171
|
+
this.apiToLambdaTargetRestApi.methodResponse,
|
|
172
|
+
this.apiToLambdaTargetRestApi.methodErrorResponse,
|
|
173
|
+
],
|
|
174
|
+
},
|
|
113
175
|
deployOptions: {
|
|
114
176
|
accessLogDestination: new apig.LogGroupLogDestination(accessLogGroup),
|
|
115
177
|
},
|
|
@@ -120,7 +182,7 @@ export class ApiToLambdaTarget extends CommonConstruct {
|
|
|
120
182
|
`${this.id}-lambda-rest-api`,
|
|
121
183
|
this,
|
|
122
184
|
this.props.api.restApi,
|
|
123
|
-
|
|
185
|
+
this.apiToLambdaTargetRestApi.lambda
|
|
124
186
|
)
|
|
125
187
|
|
|
126
188
|
this.addCfnOutput(`${this.id}-restApiId`, this.apiToLambdaTargetRestApi.api.restApiId)
|
|
@@ -128,7 +190,80 @@ export class ApiToLambdaTarget extends CommonConstruct {
|
|
|
128
190
|
}
|
|
129
191
|
|
|
130
192
|
/**
|
|
131
|
-
* @summary Method to create
|
|
193
|
+
* @summary Method to create api integration resource
|
|
194
|
+
* @protected
|
|
195
|
+
*/
|
|
196
|
+
protected createApiToLambdaTargetResource() {
|
|
197
|
+
if (!this.props.api.withResource) return
|
|
198
|
+
|
|
199
|
+
let rootResource
|
|
200
|
+
if (this.props.api.withResource && this.props.api.importedRestApiRootResourceRef) {
|
|
201
|
+
rootResource = apig.Resource.fromResourceAttributes(this, `${this.id}-root-resource`, {
|
|
202
|
+
resourceId: cdk.Fn.importValue(this.props.api.importedRestApiRootResourceRef),
|
|
203
|
+
restApi: this.apiToLambdaTargetRestApi.api,
|
|
204
|
+
path: '/',
|
|
205
|
+
})
|
|
206
|
+
} else {
|
|
207
|
+
rootResource = this.apiToLambdaTargetRestApi.api.root
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
this.apiToLambdaTargetRestApi.resource = rootResource.addResource(this.props.api.resource ?? this.apiResource)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
protected createApiToLambdaTargetPolicy() {
|
|
214
|
+
this.apiToLambdaTargetRestApi.policy = new iam.PolicyDocument({
|
|
215
|
+
statements: [
|
|
216
|
+
this.iamManager.statementForPutEvents(),
|
|
217
|
+
this.iamManager.statementForInvokeLambda([this.apiToLambdaTargetRestApi.lambda.functionArn]),
|
|
218
|
+
],
|
|
219
|
+
})
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* @summary Method to create a role for api integration
|
|
224
|
+
* @protected
|
|
225
|
+
*/
|
|
226
|
+
protected createApiToLambdaTargetRole() {
|
|
227
|
+
if (!this.apiToLambdaTargetRestApi.policy) throw 'Policy undefined'
|
|
228
|
+
|
|
229
|
+
this.apiToLambdaTargetRestApi.role = new iam.Role(this, `${this.id}-rest-api-role`, {
|
|
230
|
+
assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com'),
|
|
231
|
+
inlinePolicies: { policy: this.apiToLambdaTargetRestApi.policy },
|
|
232
|
+
})
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* @summary Method to create api integration resource method
|
|
237
|
+
* @protected
|
|
238
|
+
*/
|
|
239
|
+
protected createApiToLambdaTargetIntegration() {
|
|
240
|
+
this.apiToLambdaTargetRestApi.integration = new apig.LambdaIntegration(this.apiToLambdaTargetRestApi.lambda, {
|
|
241
|
+
allowTestInvoke: true,
|
|
242
|
+
credentialsRole: this.apiToLambdaTargetRestApi.role,
|
|
243
|
+
})
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* @summary Method to create api integration resource method
|
|
248
|
+
* @protected
|
|
249
|
+
*/
|
|
250
|
+
protected createApiToLambdaTargetResourceMethod() {
|
|
251
|
+
if (!this.props.api.withResource) return
|
|
252
|
+
this.apiToLambdaTargetRestApi.method = this.apiToLambdaTargetRestApi.resource.addMethod(
|
|
253
|
+
'POST',
|
|
254
|
+
this.apiToLambdaTargetRestApi.integration,
|
|
255
|
+
{
|
|
256
|
+
authorizer: this.apiToLambdaTargetRestApi.authoriser,
|
|
257
|
+
methodResponses: [
|
|
258
|
+
this.apiToLambdaTargetRestApi.methodResponse,
|
|
259
|
+
this.apiToLambdaTargetRestApi.methodErrorResponse,
|
|
260
|
+
],
|
|
261
|
+
}
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* @summary Method to create custom restApi domain for Api
|
|
132
267
|
* @protected
|
|
133
268
|
*/
|
|
134
269
|
protected createApiDomain() {
|
|
@@ -144,27 +279,13 @@ export class ApiToLambdaTarget extends CommonConstruct {
|
|
|
144
279
|
}
|
|
145
280
|
|
|
146
281
|
/**
|
|
147
|
-
* @summary Method to create base path mappings for
|
|
282
|
+
* @summary Method to create base path mappings for Api
|
|
148
283
|
* @protected
|
|
149
284
|
*/
|
|
150
285
|
protected createApiBasePathMapping() {
|
|
151
|
-
|
|
152
|
-
if (apiRootPaths && apiRootPaths.length > 0) {
|
|
153
|
-
apiRootPaths.forEach((apiRootPath: string) => {
|
|
154
|
-
this.apiToLambdaTargetRestApi.basePathMappings.push(
|
|
155
|
-
new apig.BasePathMapping(this, `${this.id}-base-bath-mapping-${apiRootPath}`, {
|
|
156
|
-
basePath: apiRootPath,
|
|
157
|
-
domainName: this.apiToLambdaTargetRestApi.domain,
|
|
158
|
-
restApi: this.apiToLambdaTargetRestApi.api,
|
|
159
|
-
stage: this.apiToLambdaTargetRestApi.api.deploymentStage,
|
|
160
|
-
})
|
|
161
|
-
)
|
|
162
|
-
})
|
|
163
|
-
return
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// add default mapping if apiRootPaths not set
|
|
286
|
+
if (this.props.api.useExisting) return
|
|
167
287
|
new apig.BasePathMapping(this, `${this.id}-base-bath-mapping`, {
|
|
288
|
+
basePath: '',
|
|
168
289
|
domainName: this.apiToLambdaTargetRestApi.domain,
|
|
169
290
|
restApi: this.apiToLambdaTargetRestApi.api,
|
|
170
291
|
stage: this.apiToLambdaTargetRestApi.api.deploymentStage,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as apig from 'aws-cdk-lib/aws-apigateway'
|
|
2
2
|
import * as acm from 'aws-cdk-lib/aws-certificatemanager'
|
|
3
3
|
import * as iam from 'aws-cdk-lib/aws-iam'
|
|
4
|
+
import * as lambda from 'aws-cdk-lib/aws-lambda'
|
|
4
5
|
import * as route53 from 'aws-cdk-lib/aws-route53'
|
|
5
6
|
import * as sns from 'aws-cdk-lib/aws-sns'
|
|
6
7
|
import * as types from '../types/aws'
|
|
@@ -23,6 +24,7 @@ export class ApiToLambdaTargetRestApi implements types.ApiToLambdaTargetRestApiT
|
|
|
23
24
|
integrationRequestParameters: { [p: string]: string }
|
|
24
25
|
integrationRequestTemplates: { [p: string]: string }
|
|
25
26
|
integrationResponse: apig.IntegrationResponse
|
|
27
|
+
lambda: lambda.IFunction
|
|
26
28
|
method: apig.Method
|
|
27
29
|
methodErrorResponse: apig.MethodResponse
|
|
28
30
|
methodResponse: apig.MethodResponse
|
|
@@ -305,6 +305,7 @@ export interface ApiToLambdaTargetRestApiType {
|
|
|
305
305
|
integrationRequestParameters: { [p: string]: string }
|
|
306
306
|
integrationRequestTemplates: { [p: string]: string }
|
|
307
307
|
integrationResponse: apig.IntegrationResponse
|
|
308
|
+
lambda: lambda.IFunction
|
|
308
309
|
method: apig.Method
|
|
309
310
|
methodErrorResponse: apig.MethodResponse
|
|
310
311
|
methodResponse: apig.MethodResponse
|
|
@@ -398,11 +399,15 @@ export interface ApiToEventBridgeTargetProps extends CommonStackProps {
|
|
|
398
399
|
* @subcategory Properties
|
|
399
400
|
*/
|
|
400
401
|
export interface ApiToLambdaTargetRestApiProps {
|
|
402
|
+
resource: string
|
|
401
403
|
certificate: AcmProps
|
|
402
404
|
restApi: apig.LambdaRestApiProps
|
|
403
405
|
importedRestApiRef?: string
|
|
404
406
|
importedRestApiRootResourceRef?: string
|
|
405
407
|
useExisting: boolean
|
|
408
|
+
withResource?: boolean
|
|
409
|
+
methodErrorResponse: apig.MethodResponse
|
|
410
|
+
methodResponse: apig.MethodResponse
|
|
406
411
|
}
|
|
407
412
|
|
|
408
413
|
/**
|