@gradientedge/cdk-utils 4.6.4 → 4.7.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.
@@ -1,4 +1,3 @@
1
- import * as apig from 'aws-cdk-lib/aws-apigateway';
2
1
  import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager';
3
2
  import { Construct } from 'constructs';
4
3
  import { CommonConstruct } from '../../common';
@@ -32,7 +31,6 @@ export declare class ApiToEventBridgeTarget extends CommonConstruct {
32
31
  apiDestinedLambda: types.ApiDestinedLambdaType;
33
32
  apiEvent: types.ApiDestinationEventType;
34
33
  apiDestinedRestApi: types.ApiDestinedRestApiType;
35
- apiDestinedBasePathMappings: apig.BasePathMapping[];
36
34
  apiResource: string;
37
35
  constructor(parent: Construct, id: string, props: types.ApiToEventBridgeTargetProps);
38
36
  protected initResources(): void;
@@ -97,6 +95,13 @@ export declare class ApiToEventBridgeTarget extends CommonConstruct {
97
95
  protected createApiDestinationRuleFailure(): void;
98
96
  protected createApiDestinedTopicRole(): void;
99
97
  protected createApiDestinedTopic(): void;
98
+ protected createApiDestinedIntegrationRequestParameters(): void;
99
+ protected createApiDestinedIntegrationRequestTemplates(): void;
100
+ protected createApiDestinedIntegrationResponse(): void;
101
+ protected createApiDestinedIntegrationErrorResponse(): void;
102
+ protected createApiDestinedIntegration(): void;
103
+ protected createApiDestinedMethodResponse(): void;
104
+ protected createApiDestinedMethodErrorResponse(): void;
100
105
  /**
101
106
  * @summary Method to create rest restApi for Api
102
107
  * @protected
@@ -105,13 +110,6 @@ export declare class ApiToEventBridgeTarget extends CommonConstruct {
105
110
  protected createApiDestinedResponseModel(): void;
106
111
  protected createApiDestinedErrorResponseModel(): void;
107
112
  protected createApiDestinedResource(): void;
108
- protected createApiDestinedIntegrationRequestParameters(): void;
109
- protected createApiDestinedIntegrationRequestTemplates(): void;
110
- protected createApiDestinedIntegrationResponse(): void;
111
- protected createApiDestinedIntegrationErrorResponse(): void;
112
- protected createApiDestinedIntegration(): void;
113
- protected createApiDestinedMethodResponse(): void;
114
- protected createApiDestinedMethodErrorResponse(): void;
115
113
  protected createApiDestinedResourceMethod(): void;
116
114
  /**
117
115
  * @summary Method to create custom restApi domain for Api API
@@ -20,6 +20,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
22
  exports.ApiToEventBridgeTarget = void 0;
23
+ const cdk = __importStar(require("aws-cdk-lib"));
23
24
  const apig = __importStar(require("aws-cdk-lib/aws-apigateway"));
24
25
  const eventstargets = __importStar(require("aws-cdk-lib/aws-events-targets"));
25
26
  const iam = __importStar(require("aws-cdk-lib/aws-iam"));
@@ -61,7 +62,6 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
61
62
  apiEvent;
62
63
  /* rest restApi related resources */
63
64
  apiDestinedRestApi;
64
- apiDestinedBasePathMappings = [];
65
65
  apiResource;
66
66
  constructor(parent, id, props) {
67
67
  super(parent, id, props);
@@ -95,15 +95,15 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
95
95
  /* restApi related resources */
96
96
  this.createApiDestinedTopicRole();
97
97
  this.createApiDestinedTopic();
98
- this.createApiDestinedRestApi();
99
- this.createApiDestinedResponseModel();
100
- this.createApiDestinedErrorResponseModel();
101
- this.createApiDestinedResource();
102
98
  this.createApiDestinedIntegrationRequestParameters();
103
99
  this.createApiDestinedIntegrationRequestTemplates();
104
100
  this.createApiDestinedIntegrationResponse();
105
101
  this.createApiDestinedIntegrationErrorResponse();
106
102
  this.createApiDestinedIntegration();
103
+ this.createApiDestinedRestApi();
104
+ this.createApiDestinedResource();
105
+ this.createApiDestinedResponseModel();
106
+ this.createApiDestinedErrorResponseModel();
107
107
  this.createApiDestinedMethodResponse();
108
108
  this.createApiDestinedMethodErrorResponse();
109
109
  this.createApiDestinedResourceMethod();
@@ -131,6 +131,8 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
131
131
  * @protected
132
132
  */
133
133
  resolveCertificate() {
134
+ if (this.props.api.useExisting)
135
+ return;
134
136
  if (this.props.api.certificate.useExistingCertificate &&
135
137
  this.props.api.certificate.certificateSsmName &&
136
138
  this.props.api.certificate.certificateRegion) {
@@ -143,6 +145,8 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
143
145
  * @protected
144
146
  */
145
147
  createApiDestinedLambdaPolicy() {
148
+ if (this.props.api.useExisting)
149
+ return;
146
150
  this.apiDestinedLambda.policy = new iam.PolicyDocument({
147
151
  statements: [this.iamManager.statementForReadSecrets(this), this.iamManager.statementForPutEvents()],
148
152
  });
@@ -152,6 +156,8 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
152
156
  * @protected
153
157
  */
154
158
  createApiDestinedLambdaRole() {
159
+ if (this.props.api.useExisting)
160
+ return;
155
161
  this.apiDestinedLambda.role = this.iamManager.createRoleForLambda(`${this.id}-lambda-destined-role`, this, this.apiDestinedLambda.policy);
156
162
  }
157
163
  /**
@@ -159,6 +165,8 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
159
165
  * @protected
160
166
  */
161
167
  createApiDestinedLambdaEnvironment() {
168
+ if (this.props.api.useExisting)
169
+ return;
162
170
  this.apiDestinedLambda.environment = {
163
171
  NODE_ENV: this.props.nodeEnv,
164
172
  LOG_LEVEL: this.props.logLevel,
@@ -171,6 +179,8 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
171
179
  * @protected
172
180
  */
173
181
  createApiDestinedLambdaLayers() {
182
+ if (this.props.api.useExisting)
183
+ return;
174
184
  const layers = [];
175
185
  if (this.props.lambda.layerSource) {
176
186
  layers.push(this.lambdaManager.createLambdaLayer(`${this.id}-lambda-destined-layer`, this, this.props.lambda.layerSource));
@@ -182,6 +192,8 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
182
192
  * @protected
183
193
  */
184
194
  createApiDestinedLambdaDestinations() {
195
+ if (this.props.api.useExisting)
196
+ return;
185
197
  this.apiDestinedLambda.destinationSuccess = new destinations.EventBridgeDestination(this.apiEvent.eventBus);
186
198
  this.apiDestinedLambda.destinationFailure = new destinations.EventBridgeDestination(this.apiEvent.eventBus);
187
199
  }
@@ -190,6 +202,8 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
190
202
  * @protected
191
203
  */
192
204
  createApiDestinedLambdaFunction() {
205
+ if (this.props.api.useExisting)
206
+ return;
193
207
  if (!this.props.lambda.source)
194
208
  throw 'Api Destined Lambda props undefined';
195
209
  this.apiDestinedLambda.function = this.lambdaManager.createLambdaFunction(`${this.id}-lambda-destined`, this, {
@@ -201,11 +215,15 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
201
215
  }, this.apiDestinedLambda.role, this.apiDestinedLambda.layers, this.props.lambda.source, this.props.lambda.handler ?? 'lambda.handler', this.apiDestinedLambda.environment);
202
216
  }
203
217
  createApiDestinedEventBus() {
218
+ if (this.props.api.useExisting)
219
+ return;
204
220
  this.apiEvent.eventBus = this.eventManager.createEventBus(`${this.id}-destined-event-bus`, this, {
205
221
  eventBusName: `${this.props.event.eventBusName}`,
206
222
  });
207
223
  }
208
224
  createApiDestinationLogGroupSuccess() {
225
+ if (this.props.api.useExisting)
226
+ return;
209
227
  this.apiEvent.logGroupSuccess = this.logManager.createLogGroup(`${this.id}-destination-success-log`, this, {
210
228
  ...{
211
229
  logGroupName: `/${this.id}/events/api-destination-success`,
@@ -218,6 +236,8 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
218
236
  * @protected
219
237
  */
220
238
  createApiDestinationRuleSuccess() {
239
+ if (this.props.api.useExisting)
240
+ return;
221
241
  this.props.event.ruleSuccess = {
222
242
  ...{
223
243
  ruleName: `${this.id}-api-destination-success`,
@@ -238,6 +258,8 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
238
258
  this.apiEvent.ruleSuccess = this.eventManager.createRule(`${this.id}-api-destination-rule-success`, this, this.props.event.ruleSuccess, this.apiEvent.eventBus, [new eventstargets.CloudWatchLogGroup(this.apiEvent.logGroupSuccess)]);
239
259
  }
240
260
  createApiDestinationLogGroupFailure() {
261
+ if (this.props.api.useExisting)
262
+ return;
241
263
  this.apiEvent.logGroupFailure = this.logManager.createLogGroup(`${this.id}-destination-failure-log`, this, {
242
264
  ...{
243
265
  logGroupName: `/${this.id}/events/api-destination-failure`,
@@ -250,6 +272,8 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
250
272
  * @protected
251
273
  */
252
274
  createApiDestinationRuleFailure() {
275
+ if (this.props.api.useExisting)
276
+ return;
253
277
  this.props.event.ruleFailure = {
254
278
  ...{
255
279
  ruleName: `${this.id}-api-destination-failure`,
@@ -271,76 +295,13 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
271
295
  });
272
296
  }
273
297
  createApiDestinedTopic() {
298
+ if (!this.props.api.withResource)
299
+ return;
274
300
  this.apiDestinedRestApi.topic = this.snsManager.createLambdaNotificationService(`${this.id}-destined-topic`, this, {
275
301
  topicName: `${this.id}-destined-topic`,
276
302
  }, this.apiDestinedLambda.function);
277
303
  this.apiDestinedRestApi.topic.grantPublish(this.apiDestinedRestApi.topicRole);
278
304
  }
279
- /**
280
- * @summary Method to create rest restApi for Api
281
- * @protected
282
- */
283
- createApiDestinedRestApi() {
284
- this.apiDestinedRestApi.api = new apig.RestApi(this, `${this.id}-sns-rest-api`, {
285
- ...{
286
- deployOptions: {
287
- dataTraceEnabled: true,
288
- description: `${this.id} - ${this.props.stage} stage`,
289
- loggingLevel: apig.MethodLoggingLevel.INFO,
290
- metricsEnabled: true,
291
- stageName: this.props.stage,
292
- },
293
- endpointConfiguration: {
294
- types: [apig.EndpointType.REGIONAL],
295
- },
296
- defaultCorsPreflightOptions: {
297
- allowOrigins: apig.Cors.ALL_ORIGINS,
298
- allowMethods: ['POST'],
299
- allowHeaders: apig.Cors.DEFAULT_HEADERS,
300
- },
301
- restApiName: `${this.id}-destined-rest-api-${this.props.stage}`,
302
- },
303
- ...this.props.api,
304
- });
305
- }
306
- createApiDestinedResponseModel() {
307
- this.apiDestinedRestApi.responseModel = this.apiDestinedRestApi.api.addModel(`${this.id}-response-model`, {
308
- ...{
309
- contentType: 'application/json',
310
- modelName: 'ResponseModel',
311
- schema: {
312
- schema: apig.JsonSchemaVersion.DRAFT4,
313
- title: 'pollResponse',
314
- type: apig.JsonSchemaType.OBJECT,
315
- properties: { message: { type: apig.JsonSchemaType.STRING } },
316
- },
317
- },
318
- ...this.props.api.responseModel,
319
- });
320
- }
321
- createApiDestinedErrorResponseModel() {
322
- this.apiDestinedRestApi.errorResponseModel = this.apiDestinedRestApi.api.addModel(`${this.id}-error-response-model`, {
323
- ...{
324
- contentType: 'application/json',
325
- modelName: 'ErrorResponseModel',
326
- schema: {
327
- schema: apig.JsonSchemaVersion.DRAFT4,
328
- title: 'errorResponse',
329
- type: apig.JsonSchemaType.OBJECT,
330
- properties: {
331
- state: { type: apig.JsonSchemaType.STRING },
332
- message: { type: apig.JsonSchemaType.STRING },
333
- },
334
- },
335
- },
336
- ...this.props.api.errorResponseModel,
337
- });
338
- }
339
- createApiDestinedResource() {
340
- if (!this.props.api.withResource)
341
- return;
342
- this.apiDestinedRestApi.resource = this.apiDestinedRestApi.api.root.addResource(this.props.api.resource ?? this.apiResource);
343
- }
344
305
  createApiDestinedIntegrationRequestParameters() {
345
306
  if (!this.props.api.withResource)
346
307
  return;
@@ -453,6 +414,98 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
453
414
  ...this.props.api.methodErrorResponse,
454
415
  };
455
416
  }
417
+ /**
418
+ * @summary Method to create rest restApi for Api
419
+ * @protected
420
+ */
421
+ createApiDestinedRestApi() {
422
+ if (this.props.api.useExisting && this.props.api.importedRestApiRef) {
423
+ this.apiDestinedRestApi.api = apig.RestApi.fromRestApiId(this, `${this.id}-sns-rest-api`, cdk.Fn.importValue(this.props.api.importedRestApiRef));
424
+ return;
425
+ }
426
+ this.apiDestinedRestApi.api = new apig.RestApi(this, `${this.id}-sns-rest-api`, {
427
+ ...{
428
+ defaultIntegration: this.apiDestinedRestApi.integration,
429
+ defaultMethodOptions: {
430
+ methodResponses: [this.apiDestinedRestApi.methodResponse, this.apiDestinedRestApi.methodErrorResponse],
431
+ },
432
+ deployOptions: {
433
+ dataTraceEnabled: true,
434
+ description: `${this.id} - ${this.props.stage} stage`,
435
+ loggingLevel: apig.MethodLoggingLevel.INFO,
436
+ metricsEnabled: true,
437
+ stageName: this.props.stage,
438
+ },
439
+ endpointConfiguration: {
440
+ types: [apig.EndpointType.REGIONAL],
441
+ },
442
+ defaultCorsPreflightOptions: {
443
+ allowOrigins: apig.Cors.ALL_ORIGINS,
444
+ allowMethods: ['POST'],
445
+ allowHeaders: apig.Cors.DEFAULT_HEADERS,
446
+ },
447
+ restApiName: `${this.id}-destined-rest-api-${this.props.stage}`,
448
+ },
449
+ ...this.props.api,
450
+ });
451
+ this.addCfnOutput(`${this.id}-restApiId`, this.apiDestinedRestApi.api.restApiId);
452
+ this.addCfnOutput(`${this.id}-restApiRootResourceId`, this.apiDestinedRestApi.api.root.resourceId);
453
+ }
454
+ createApiDestinedResponseModel() {
455
+ if (!this.props.api.withResource)
456
+ return;
457
+ this.apiDestinedRestApi.responseModel = new apig.Model(this, `${this.id}-response-model`, {
458
+ restApi: this.apiDestinedRestApi.api,
459
+ ...{
460
+ contentType: 'application/json',
461
+ modelName: 'ResponseModel',
462
+ schema: {
463
+ schema: apig.JsonSchemaVersion.DRAFT4,
464
+ title: 'pollResponse',
465
+ type: apig.JsonSchemaType.OBJECT,
466
+ properties: { message: { type: apig.JsonSchemaType.STRING } },
467
+ },
468
+ },
469
+ ...this.props.api.responseModel,
470
+ });
471
+ }
472
+ createApiDestinedErrorResponseModel() {
473
+ if (!this.props.api.withResource)
474
+ return;
475
+ this.apiDestinedRestApi.errorResponseModel = new apig.Model(this, `${this.id}-error-response-model`, {
476
+ restApi: this.apiDestinedRestApi.api,
477
+ ...{
478
+ contentType: 'application/json',
479
+ modelName: 'ErrorResponseModel',
480
+ schema: {
481
+ schema: apig.JsonSchemaVersion.DRAFT4,
482
+ title: 'errorResponse',
483
+ type: apig.JsonSchemaType.OBJECT,
484
+ properties: {
485
+ state: { type: apig.JsonSchemaType.STRING },
486
+ message: { type: apig.JsonSchemaType.STRING },
487
+ },
488
+ },
489
+ },
490
+ ...this.props.api.errorResponseModel,
491
+ });
492
+ }
493
+ createApiDestinedResource() {
494
+ if (!this.props.api.withResource)
495
+ return;
496
+ let rootResource;
497
+ if (this.props.api.withResource && this.props.api.importedRestApiRootResourceRef) {
498
+ rootResource = apig.Resource.fromResourceAttributes(this, `${this.id}-root-resource`, {
499
+ resourceId: cdk.Fn.importValue(this.props.api.importedRestApiRootResourceRef),
500
+ restApi: this.apiDestinedRestApi.api,
501
+ path: '/',
502
+ });
503
+ }
504
+ else {
505
+ rootResource = this.apiDestinedRestApi.api.root;
506
+ }
507
+ this.apiDestinedRestApi.resource = rootResource.addResource(this.props.api.resource ?? this.apiResource);
508
+ }
456
509
  createApiDestinedResourceMethod() {
457
510
  if (!this.props.api.withResource)
458
511
  return;
@@ -465,6 +518,8 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
465
518
  * @protected
466
519
  */
467
520
  createApiDomain() {
521
+ if (this.props.api.useExisting)
522
+ return;
468
523
  this.apiDestinedRestApi.domain = this.apiManager.createApiDomain(`${this.id}-api-domain`, this, this.isProductionStage()
469
524
  ? `${this.props.apiSubDomain}.${this.fullyQualifiedDomainName}`
470
525
  : `${this.props.apiSubDomain}-${this.props.stage}.${this.fullyQualifiedDomainName}`, this.apiDestinedRestApi.certificate);
@@ -474,23 +529,22 @@ class ApiToEventBridgeTarget extends common_1.CommonConstruct {
474
529
  * @protected
475
530
  */
476
531
  createApiBasePathMapping() {
477
- const apiRootPaths = this.props.apiRootPaths;
478
- if (apiRootPaths && apiRootPaths.length > 0) {
479
- apiRootPaths.forEach((apiRootPath) => {
480
- this.apiDestinedBasePathMappings.push(new apig.BasePathMapping(this, `${this.id}-base-bath-mapping-${apiRootPath}`, {
481
- basePath: this.props.api.resource,
482
- domainName: this.apiDestinedRestApi.domain,
483
- restApi: this.apiDestinedRestApi.api,
484
- stage: this.apiDestinedRestApi.api.deploymentStage,
485
- }));
486
- });
487
- }
532
+ if (this.props.api.useExisting)
533
+ return;
534
+ new apig.BasePathMapping(this, `${this.id}-base-bath-mapping`, {
535
+ basePath: '',
536
+ domainName: this.apiDestinedRestApi.domain,
537
+ restApi: this.apiDestinedRestApi.api,
538
+ stage: this.apiDestinedRestApi.api.deploymentStage,
539
+ });
488
540
  }
489
541
  /**
490
542
  * @summary Method to create route53 records for Api API
491
543
  * @protected
492
544
  */
493
545
  createApiRouteAssets() {
546
+ if (this.props.api.useExisting)
547
+ return;
494
548
  this.route53Manager.createApiGatewayARecord(`${this.id}-custom-domain-a-record`, this, this.props.apiSubDomain, this.apiDestinedRestApi.domain, this.apiDestinedRestApi.hostedZone);
495
549
  }
496
550
  }
@@ -36,7 +36,7 @@ export declare class SnsManager {
36
36
  * @param {string} id scoped id of the resource
37
37
  * @param {common.CommonConstruct} scope scope in which this resource is defined
38
38
  * @param {types.SubscriptionProps} props
39
- * @param {lambda.Function} lambdaFunction
39
+ * @param {lambda.IFunction} lambdaFunction
40
40
  */
41
- createLambdaNotificationService(id: string, scope: common.CommonConstruct, props: types.SubscriptionProps, lambdaFunction: lambda.Function): sns.Topic;
41
+ createLambdaNotificationService(id: string, scope: common.CommonConstruct, props: types.SubscriptionProps, lambdaFunction: lambda.IFunction): sns.Topic;
42
42
  }
@@ -71,7 +71,7 @@ class SnsManager {
71
71
  * @param {string} id scoped id of the resource
72
72
  * @param {common.CommonConstruct} scope scope in which this resource is defined
73
73
  * @param {types.SubscriptionProps} props
74
- * @param {lambda.Function} lambdaFunction
74
+ * @param {lambda.IFunction} lambdaFunction
75
75
  */
76
76
  createLambdaNotificationService(id, scope, props, lambdaFunction) {
77
77
  if (!props)
@@ -170,7 +170,7 @@ export interface ApiDestinationEventType {
170
170
  * @subcategory Types
171
171
  */
172
172
  export interface ApiDestinedRestApiType {
173
- api: apig.RestApi;
173
+ api: apig.IRestApi;
174
174
  certificate: acm.ICertificate;
175
175
  domain: apig.DomainName;
176
176
  errorResponseModel: apig.Model;
@@ -189,7 +189,7 @@ export interface ApiDestinedRestApiType {
189
189
  methodResponse: apig.MethodResponse;
190
190
  resource: apig.Resource;
191
191
  responseModel: apig.Model;
192
- topic: sns.Topic;
192
+ topic: sns.ITopic;
193
193
  topicRole: iam.Role;
194
194
  }
195
195
  /**
@@ -200,7 +200,7 @@ export interface ApiDestinedLambdaType {
200
200
  destinationFailure: destinations.EventBridgeDestination;
201
201
  destinationSuccess: destinations.EventBridgeDestination;
202
202
  environment: types.ApiDestinedLambdaEnvironment;
203
- function: lambda.Function;
203
+ function: lambda.IFunction;
204
204
  layers: lambda.LayerVersion[];
205
205
  layerSource?: lambda.AssetCode;
206
206
  policy: iam.PolicyDocument;
@@ -212,7 +212,6 @@ export interface ApiDestinedLambdaType {
212
212
  * @subcategory Properties
213
213
  */
214
214
  export interface ApiToEventBridgeTargetRestApiProps {
215
- withResource?: boolean;
216
215
  certificate: AcmProps;
217
216
  integrationResponse?: apig.IntegrationResponse;
218
217
  integrationErrorResponse?: apig.IntegrationResponse;
@@ -223,6 +222,10 @@ export interface ApiToEventBridgeTargetRestApiProps {
223
222
  errorResponseModel?: apig.ModelOptions;
224
223
  responseModel?: apig.ModelOptions;
225
224
  restApi?: apig.RestApiProps;
225
+ importedRestApiRef?: string;
226
+ importedRestApiRootResourceRef?: string;
227
+ useExisting: boolean;
228
+ withResource?: boolean;
226
229
  }
227
230
  /**
228
231
  * @category cdk-utils.api-to-eventbridge-target
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradientedge/cdk-utils",
3
- "version": "4.6.4",
3
+ "version": "4.7.0",
4
4
  "description": "Utilities for AWS CDK provisioning",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -50,7 +50,7 @@
50
50
  "app-root-path": "^3.0.0",
51
51
  "aws-cdk-lib": "^2.12.0",
52
52
  "aws-sdk": "^2.1074.0",
53
- "constructs": "^10.0.62",
53
+ "constructs": "^10.0.63",
54
54
  "lodash": "^4.17.21",
55
55
  "moment": "^2.29.1",
56
56
  "nconf": "^0.11.3",
@@ -1,3 +1,4 @@
1
+ import * as cdk from 'aws-cdk-lib'
1
2
  import * as apig from 'aws-cdk-lib/aws-apigateway'
2
3
  import * as eventstargets from 'aws-cdk-lib/aws-events-targets'
3
4
  import * as iam from 'aws-cdk-lib/aws-iam'
@@ -48,7 +49,6 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
48
49
 
49
50
  /* rest restApi related resources */
50
51
  apiDestinedRestApi: types.ApiDestinedRestApiType
51
- apiDestinedBasePathMappings: apig.BasePathMapping[] = []
52
52
  apiResource: string
53
53
 
54
54
  constructor(parent: Construct, id: string, props: types.ApiToEventBridgeTargetProps) {
@@ -91,15 +91,15 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
91
91
  /* restApi related resources */
92
92
  this.createApiDestinedTopicRole()
93
93
  this.createApiDestinedTopic()
94
- this.createApiDestinedRestApi()
95
- this.createApiDestinedResponseModel()
96
- this.createApiDestinedErrorResponseModel()
97
- this.createApiDestinedResource()
98
94
  this.createApiDestinedIntegrationRequestParameters()
99
95
  this.createApiDestinedIntegrationRequestTemplates()
100
96
  this.createApiDestinedIntegrationResponse()
101
97
  this.createApiDestinedIntegrationErrorResponse()
102
98
  this.createApiDestinedIntegration()
99
+ this.createApiDestinedRestApi()
100
+ this.createApiDestinedResource()
101
+ this.createApiDestinedResponseModel()
102
+ this.createApiDestinedErrorResponseModel()
103
103
  this.createApiDestinedMethodResponse()
104
104
  this.createApiDestinedMethodErrorResponse()
105
105
  this.createApiDestinedResourceMethod()
@@ -134,6 +134,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
134
134
  * @protected
135
135
  */
136
136
  protected resolveCertificate() {
137
+ if (this.props.api.useExisting) return
137
138
  if (
138
139
  this.props.api.certificate.useExistingCertificate &&
139
140
  this.props.api.certificate.certificateSsmName &&
@@ -159,6 +160,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
159
160
  * @protected
160
161
  */
161
162
  protected createApiDestinedLambdaPolicy() {
163
+ if (this.props.api.useExisting) return
162
164
  this.apiDestinedLambda.policy = new iam.PolicyDocument({
163
165
  statements: [this.iamManager.statementForReadSecrets(this), this.iamManager.statementForPutEvents()],
164
166
  })
@@ -169,6 +171,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
169
171
  * @protected
170
172
  */
171
173
  protected createApiDestinedLambdaRole() {
174
+ if (this.props.api.useExisting) return
172
175
  this.apiDestinedLambda.role = this.iamManager.createRoleForLambda(
173
176
  `${this.id}-lambda-destined-role`,
174
177
  this,
@@ -181,6 +184,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
181
184
  * @protected
182
185
  */
183
186
  protected createApiDestinedLambdaEnvironment() {
187
+ if (this.props.api.useExisting) return
184
188
  this.apiDestinedLambda.environment = {
185
189
  NODE_ENV: this.props.nodeEnv,
186
190
  LOG_LEVEL: this.props.logLevel,
@@ -194,6 +198,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
194
198
  * @protected
195
199
  */
196
200
  protected createApiDestinedLambdaLayers() {
201
+ if (this.props.api.useExisting) return
197
202
  const layers: lambda.LayerVersion[] = []
198
203
  if (this.props.lambda.layerSource) {
199
204
  layers.push(
@@ -209,6 +214,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
209
214
  * @protected
210
215
  */
211
216
  protected createApiDestinedLambdaDestinations() {
217
+ if (this.props.api.useExisting) return
212
218
  this.apiDestinedLambda.destinationSuccess = new destinations.EventBridgeDestination(this.apiEvent.eventBus)
213
219
  this.apiDestinedLambda.destinationFailure = new destinations.EventBridgeDestination(this.apiEvent.eventBus)
214
220
  }
@@ -218,6 +224,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
218
224
  * @protected
219
225
  */
220
226
  protected createApiDestinedLambdaFunction() {
227
+ if (this.props.api.useExisting) return
221
228
  if (!this.props.lambda.source) throw 'Api Destined Lambda props undefined'
222
229
 
223
230
  this.apiDestinedLambda.function = this.lambdaManager.createLambdaFunction(
@@ -239,12 +246,14 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
239
246
  }
240
247
 
241
248
  protected createApiDestinedEventBus() {
249
+ if (this.props.api.useExisting) return
242
250
  this.apiEvent.eventBus = this.eventManager.createEventBus(`${this.id}-destined-event-bus`, this, {
243
251
  eventBusName: `${this.props.event.eventBusName}`,
244
252
  })
245
253
  }
246
254
 
247
255
  protected createApiDestinationLogGroupSuccess() {
256
+ if (this.props.api.useExisting) return
248
257
  this.apiEvent.logGroupSuccess = this.logManager.createLogGroup(`${this.id}-destination-success-log`, this, {
249
258
  ...{
250
259
  logGroupName: `/${this.id}/events/api-destination-success`,
@@ -258,6 +267,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
258
267
  * @protected
259
268
  */
260
269
  protected createApiDestinationRuleSuccess() {
270
+ if (this.props.api.useExisting) return
261
271
  this.props.event.ruleSuccess = {
262
272
  ...{
263
273
  ruleName: `${this.id}-api-destination-success`,
@@ -285,6 +295,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
285
295
  }
286
296
 
287
297
  protected createApiDestinationLogGroupFailure() {
298
+ if (this.props.api.useExisting) return
288
299
  this.apiEvent.logGroupFailure = this.logManager.createLogGroup(`${this.id}-destination-failure-log`, this, {
289
300
  ...{
290
301
  logGroupName: `/${this.id}/events/api-destination-failure`,
@@ -298,6 +309,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
298
309
  * @protected
299
310
  */
300
311
  protected createApiDestinationRuleFailure() {
312
+ if (this.props.api.useExisting) return
301
313
  this.props.event.ruleFailure = {
302
314
  ...{
303
315
  ruleName: `${this.id}-api-destination-failure`,
@@ -327,6 +339,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
327
339
  }
328
340
 
329
341
  protected createApiDestinedTopic() {
342
+ if (!this.props.api.withResource) return
330
343
  this.apiDestinedRestApi.topic = this.snsManager.createLambdaNotificationService(
331
344
  `${this.id}-destined-topic`,
332
345
  this,
@@ -339,80 +352,6 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
339
352
  this.apiDestinedRestApi.topic.grantPublish(this.apiDestinedRestApi.topicRole)
340
353
  }
341
354
 
342
- /**
343
- * @summary Method to create rest restApi for Api
344
- * @protected
345
- */
346
- protected createApiDestinedRestApi() {
347
- this.apiDestinedRestApi.api = new apig.RestApi(this, `${this.id}-sns-rest-api`, {
348
- ...{
349
- deployOptions: {
350
- dataTraceEnabled: true,
351
- description: `${this.id} - ${this.props.stage} stage`,
352
- loggingLevel: apig.MethodLoggingLevel.INFO,
353
- metricsEnabled: true,
354
- stageName: this.props.stage,
355
- },
356
- endpointConfiguration: {
357
- types: [apig.EndpointType.REGIONAL],
358
- },
359
- defaultCorsPreflightOptions: {
360
- allowOrigins: apig.Cors.ALL_ORIGINS,
361
- allowMethods: ['POST'],
362
- allowHeaders: apig.Cors.DEFAULT_HEADERS,
363
- },
364
- restApiName: `${this.id}-destined-rest-api-${this.props.stage}`,
365
- },
366
- ...this.props.api,
367
- })
368
- }
369
-
370
- protected createApiDestinedResponseModel() {
371
- this.apiDestinedRestApi.responseModel = this.apiDestinedRestApi.api.addModel(`${this.id}-response-model`, {
372
- ...{
373
- contentType: 'application/json',
374
- modelName: 'ResponseModel',
375
- schema: {
376
- schema: apig.JsonSchemaVersion.DRAFT4,
377
- title: 'pollResponse',
378
- type: apig.JsonSchemaType.OBJECT,
379
- properties: { message: { type: apig.JsonSchemaType.STRING } },
380
- },
381
- },
382
- ...this.props.api.responseModel,
383
- })
384
- }
385
-
386
- protected createApiDestinedErrorResponseModel() {
387
- this.apiDestinedRestApi.errorResponseModel = this.apiDestinedRestApi.api.addModel(
388
- `${this.id}-error-response-model`,
389
- {
390
- ...{
391
- contentType: 'application/json',
392
- modelName: 'ErrorResponseModel',
393
- schema: {
394
- schema: apig.JsonSchemaVersion.DRAFT4,
395
- title: 'errorResponse',
396
- type: apig.JsonSchemaType.OBJECT,
397
- properties: {
398
- state: { type: apig.JsonSchemaType.STRING },
399
- message: { type: apig.JsonSchemaType.STRING },
400
- },
401
- },
402
- },
403
- ...this.props.api.errorResponseModel,
404
- }
405
- )
406
- }
407
-
408
- protected createApiDestinedResource() {
409
- if (!this.props.api.withResource) return
410
-
411
- this.apiDestinedRestApi.resource = this.apiDestinedRestApi.api.root.addResource(
412
- this.props.api.resource ?? this.apiResource
413
- )
414
- }
415
-
416
355
  protected createApiDestinedIntegrationRequestParameters() {
417
356
  if (!this.props.api.withResource) return
418
357
  this.apiDestinedRestApi.integrationRequestParameters = {
@@ -525,6 +464,104 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
525
464
  }
526
465
  }
527
466
 
467
+ /**
468
+ * @summary Method to create rest restApi for Api
469
+ * @protected
470
+ */
471
+ protected createApiDestinedRestApi() {
472
+ if (this.props.api.useExisting && this.props.api.importedRestApiRef) {
473
+ this.apiDestinedRestApi.api = apig.RestApi.fromRestApiId(
474
+ this,
475
+ `${this.id}-sns-rest-api`,
476
+ cdk.Fn.importValue(this.props.api.importedRestApiRef)
477
+ )
478
+ return
479
+ }
480
+ this.apiDestinedRestApi.api = new apig.RestApi(this, `${this.id}-sns-rest-api`, {
481
+ ...{
482
+ defaultIntegration: this.apiDestinedRestApi.integration,
483
+ defaultMethodOptions: {
484
+ methodResponses: [this.apiDestinedRestApi.methodResponse, this.apiDestinedRestApi.methodErrorResponse],
485
+ },
486
+ deployOptions: {
487
+ dataTraceEnabled: true,
488
+ description: `${this.id} - ${this.props.stage} stage`,
489
+ loggingLevel: apig.MethodLoggingLevel.INFO,
490
+ metricsEnabled: true,
491
+ stageName: this.props.stage,
492
+ },
493
+ endpointConfiguration: {
494
+ types: [apig.EndpointType.REGIONAL],
495
+ },
496
+ defaultCorsPreflightOptions: {
497
+ allowOrigins: apig.Cors.ALL_ORIGINS,
498
+ allowMethods: ['POST'],
499
+ allowHeaders: apig.Cors.DEFAULT_HEADERS,
500
+ },
501
+ restApiName: `${this.id}-destined-rest-api-${this.props.stage}`,
502
+ },
503
+ ...this.props.api,
504
+ })
505
+ this.addCfnOutput(`${this.id}-restApiId`, this.apiDestinedRestApi.api.restApiId)
506
+ this.addCfnOutput(`${this.id}-restApiRootResourceId`, this.apiDestinedRestApi.api.root.resourceId)
507
+ }
508
+
509
+ protected createApiDestinedResponseModel() {
510
+ if (!this.props.api.withResource) return
511
+ this.apiDestinedRestApi.responseModel = new apig.Model(this, `${this.id}-response-model`, {
512
+ restApi: this.apiDestinedRestApi.api,
513
+ ...{
514
+ contentType: 'application/json',
515
+ modelName: 'ResponseModel',
516
+ schema: {
517
+ schema: apig.JsonSchemaVersion.DRAFT4,
518
+ title: 'pollResponse',
519
+ type: apig.JsonSchemaType.OBJECT,
520
+ properties: { message: { type: apig.JsonSchemaType.STRING } },
521
+ },
522
+ },
523
+ ...this.props.api.responseModel,
524
+ })
525
+ }
526
+
527
+ protected createApiDestinedErrorResponseModel() {
528
+ if (!this.props.api.withResource) return
529
+ this.apiDestinedRestApi.errorResponseModel = new apig.Model(this, `${this.id}-error-response-model`, {
530
+ restApi: this.apiDestinedRestApi.api,
531
+ ...{
532
+ contentType: 'application/json',
533
+ modelName: 'ErrorResponseModel',
534
+ schema: {
535
+ schema: apig.JsonSchemaVersion.DRAFT4,
536
+ title: 'errorResponse',
537
+ type: apig.JsonSchemaType.OBJECT,
538
+ properties: {
539
+ state: { type: apig.JsonSchemaType.STRING },
540
+ message: { type: apig.JsonSchemaType.STRING },
541
+ },
542
+ },
543
+ },
544
+ ...this.props.api.errorResponseModel,
545
+ })
546
+ }
547
+
548
+ protected createApiDestinedResource() {
549
+ if (!this.props.api.withResource) return
550
+
551
+ let rootResource
552
+ if (this.props.api.withResource && this.props.api.importedRestApiRootResourceRef) {
553
+ rootResource = apig.Resource.fromResourceAttributes(this, `${this.id}-root-resource`, {
554
+ resourceId: cdk.Fn.importValue(this.props.api.importedRestApiRootResourceRef),
555
+ restApi: this.apiDestinedRestApi.api,
556
+ path: '/',
557
+ })
558
+ } else {
559
+ rootResource = this.apiDestinedRestApi.api.root
560
+ }
561
+
562
+ this.apiDestinedRestApi.resource = rootResource.addResource(this.props.api.resource ?? this.apiResource)
563
+ }
564
+
528
565
  protected createApiDestinedResourceMethod() {
529
566
  if (!this.props.api.withResource) return
530
567
  this.apiDestinedRestApi.method = this.apiDestinedRestApi.resource.addMethod(
@@ -541,6 +578,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
541
578
  * @protected
542
579
  */
543
580
  protected createApiDomain() {
581
+ if (this.props.api.useExisting) return
544
582
  this.apiDestinedRestApi.domain = this.apiManager.createApiDomain(
545
583
  `${this.id}-api-domain`,
546
584
  this,
@@ -556,19 +594,13 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
556
594
  * @protected
557
595
  */
558
596
  protected createApiBasePathMapping() {
559
- const apiRootPaths = this.props.apiRootPaths
560
- if (apiRootPaths && apiRootPaths.length > 0) {
561
- apiRootPaths.forEach((apiRootPath: string) => {
562
- this.apiDestinedBasePathMappings.push(
563
- new apig.BasePathMapping(this, `${this.id}-base-bath-mapping-${apiRootPath}`, {
564
- basePath: this.props.api.resource,
565
- domainName: this.apiDestinedRestApi.domain,
566
- restApi: this.apiDestinedRestApi.api,
567
- stage: this.apiDestinedRestApi.api.deploymentStage,
568
- })
569
- )
570
- })
571
- }
597
+ if (this.props.api.useExisting) return
598
+ new apig.BasePathMapping(this, `${this.id}-base-bath-mapping`, {
599
+ basePath: '',
600
+ domainName: this.apiDestinedRestApi.domain,
601
+ restApi: this.apiDestinedRestApi.api,
602
+ stage: this.apiDestinedRestApi.api.deploymentStage,
603
+ })
572
604
  }
573
605
 
574
606
  /**
@@ -576,6 +608,7 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
576
608
  * @protected
577
609
  */
578
610
  protected createApiRouteAssets() {
611
+ if (this.props.api.useExisting) return
579
612
  this.route53Manager.createApiGatewayARecord(
580
613
  `${this.id}-custom-domain-a-record`,
581
614
  this,
@@ -62,13 +62,13 @@ export class SnsManager {
62
62
  * @param {string} id scoped id of the resource
63
63
  * @param {common.CommonConstruct} scope scope in which this resource is defined
64
64
  * @param {types.SubscriptionProps} props
65
- * @param {lambda.Function} lambdaFunction
65
+ * @param {lambda.IFunction} lambdaFunction
66
66
  */
67
67
  public createLambdaNotificationService(
68
68
  id: string,
69
69
  scope: common.CommonConstruct,
70
70
  props: types.SubscriptionProps,
71
- lambdaFunction: lambda.Function
71
+ lambdaFunction: lambda.IFunction
72
72
  ) {
73
73
  if (!props) throw `Subscription props undefined`
74
74
 
@@ -181,7 +181,7 @@ export interface ApiDestinationEventType {
181
181
  * @subcategory Types
182
182
  */
183
183
  export interface ApiDestinedRestApiType {
184
- api: apig.RestApi
184
+ api: apig.IRestApi
185
185
  certificate: acm.ICertificate
186
186
  domain: apig.DomainName
187
187
  errorResponseModel: apig.Model
@@ -196,7 +196,7 @@ export interface ApiDestinedRestApiType {
196
196
  methodResponse: apig.MethodResponse
197
197
  resource: apig.Resource
198
198
  responseModel: apig.Model
199
- topic: sns.Topic
199
+ topic: sns.ITopic
200
200
  topicRole: iam.Role
201
201
  }
202
202
 
@@ -208,7 +208,7 @@ export interface ApiDestinedLambdaType {
208
208
  destinationFailure: destinations.EventBridgeDestination
209
209
  destinationSuccess: destinations.EventBridgeDestination
210
210
  environment: types.ApiDestinedLambdaEnvironment
211
- function: lambda.Function
211
+ function: lambda.IFunction
212
212
  layers: lambda.LayerVersion[]
213
213
  layerSource?: lambda.AssetCode
214
214
  policy: iam.PolicyDocument
@@ -221,7 +221,6 @@ export interface ApiDestinedLambdaType {
221
221
  * @subcategory Properties
222
222
  */
223
223
  export interface ApiToEventBridgeTargetRestApiProps {
224
- withResource?: boolean
225
224
  certificate: AcmProps
226
225
  integrationResponse?: apig.IntegrationResponse
227
226
  integrationErrorResponse?: apig.IntegrationResponse
@@ -232,6 +231,10 @@ export interface ApiToEventBridgeTargetRestApiProps {
232
231
  errorResponseModel?: apig.ModelOptions
233
232
  responseModel?: apig.ModelOptions
234
233
  restApi?: apig.RestApiProps
234
+ importedRestApiRef?: string
235
+ importedRestApiRootResourceRef?: string
236
+ useExisting: boolean
237
+ withResource?: boolean
235
238
  }
236
239
 
237
240
  /**