@gradientedge/cdk-utils 9.9.0 → 9.10.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.
Files changed (111) hide show
  1. package/dist/src/lib/aws/common/construct.d.ts +8 -1
  2. package/dist/src/lib/aws/common/construct.js +14 -0
  3. package/dist/src/lib/aws/common/stack.d.ts +3 -0
  4. package/dist/src/lib/aws/common/stack.js +3 -0
  5. package/dist/src/lib/aws/common/types.d.ts +8 -0
  6. package/dist/src/lib/aws/construct/api-to-any-target/main.js +3 -1
  7. package/dist/src/lib/aws/construct/api-to-any-target/types.d.ts +3 -3
  8. package/dist/src/lib/aws/construct/api-to-eventbridge-target/main.js +3 -1
  9. package/dist/src/lib/aws/construct/api-to-eventbridge-target/types.d.ts +6 -6
  10. package/dist/src/lib/aws/construct/api-to-eventbridge-target-with-sns/main.js +4 -2
  11. package/dist/src/lib/aws/construct/api-to-lambda-target/main.js +1 -1
  12. package/dist/src/lib/aws/construct/lambda-with-iam-access/main.js +4 -5
  13. package/dist/src/lib/aws/construct/site-with-ecs-backend/main.js +13 -9
  14. package/dist/src/lib/aws/construct/site-with-lambda-backend/main.js +9 -3
  15. package/dist/src/lib/aws/services/api-gateway/main.js +3 -1
  16. package/dist/src/lib/aws/services/api-gateway/types.d.ts +6 -1
  17. package/dist/src/lib/aws/services/appconfig/main.js +1 -1
  18. package/dist/src/lib/aws/services/appconfig/types.d.ts +2 -0
  19. package/dist/src/lib/aws/services/cloudfront/main.js +8 -2
  20. package/dist/src/lib/aws/services/cloudfront/types.d.ts +2 -0
  21. package/dist/src/lib/aws/services/cloudtrail/main.js +3 -1
  22. package/dist/src/lib/aws/services/cloudtrail/types.d.ts +2 -0
  23. package/dist/src/lib/aws/services/dynamodb/main.js +3 -1
  24. package/dist/src/lib/aws/services/dynamodb/types.d.ts +2 -0
  25. package/dist/src/lib/aws/services/elastic-container-service/main.js +6 -2
  26. package/dist/src/lib/aws/services/elastic-container-service/types.d.ts +2 -0
  27. package/dist/src/lib/aws/services/elastic-file-system/main.js +3 -1
  28. package/dist/src/lib/aws/services/elastic-file-system/types.d.ts +3 -1
  29. package/dist/src/lib/aws/services/elastic-kubernetes-service/main.js +3 -1
  30. package/dist/src/lib/aws/services/elastic-kubernetes-service/types.d.ts +2 -0
  31. package/dist/src/lib/aws/services/elasticache/main.js +3 -1
  32. package/dist/src/lib/aws/services/elasticache/types.d.ts +2 -0
  33. package/dist/src/lib/aws/services/eventbridge/main.js +34 -9
  34. package/dist/src/lib/aws/services/eventbridge/types.d.ts +9 -1
  35. package/dist/src/lib/aws/services/evidently/main.js +22 -4
  36. package/dist/src/lib/aws/services/evidently/types.d.ts +5 -0
  37. package/dist/src/lib/aws/services/identity-access-management/main.js +10 -10
  38. package/dist/src/lib/aws/services/lambda/main.js +6 -2
  39. package/dist/src/lib/aws/services/lambda/types.d.ts +5 -2
  40. package/dist/src/lib/aws/services/secrets-manager/index.d.ts +1 -0
  41. package/dist/src/lib/aws/services/secrets-manager/index.js +1 -0
  42. package/dist/src/lib/aws/services/secrets-manager/main.d.ts +3 -2
  43. package/dist/src/lib/aws/services/secrets-manager/main.js +5 -1
  44. package/dist/src/lib/aws/services/secrets-manager/types.d.ts +5 -0
  45. package/dist/src/lib/aws/services/secrets-manager/types.js +2 -0
  46. package/dist/src/lib/aws/services/simple-notification-service/main.js +8 -4
  47. package/dist/src/lib/aws/services/simple-notification-service/types.d.ts +2 -0
  48. package/dist/src/lib/aws/services/simple-queue-service/main.js +9 -7
  49. package/dist/src/lib/aws/services/simple-queue-service/types.d.ts +2 -0
  50. package/dist/src/lib/aws/services/simple-storage-service/main.d.ts +1 -1
  51. package/dist/src/lib/aws/services/simple-storage-service/main.js +12 -8
  52. package/dist/src/lib/aws/services/simple-storage-service/types.d.ts +3 -1
  53. package/dist/src/lib/aws/services/step-function/main.js +3 -1
  54. package/dist/src/lib/aws/services/step-function/types.d.ts +2 -0
  55. package/dist/src/lib/aws/services/virtual-private-cloud/main.js +10 -3
  56. package/dist/src/lib/aws/services/virtual-private-cloud/types.d.ts +2 -0
  57. package/dist/src/lib/aws/services/web-application-firewall/main.js +6 -2
  58. package/dist/src/lib/aws/services/web-application-firewall/types.d.ts +3 -0
  59. package/package.json +1 -1
  60. package/src/lib/aws/common/construct.ts +16 -1
  61. package/src/lib/aws/common/stack.ts +3 -0
  62. package/src/lib/aws/common/types.ts +9 -0
  63. package/src/lib/aws/construct/api-to-any-target/main.ts +6 -1
  64. package/src/lib/aws/construct/api-to-any-target/types.ts +2 -3
  65. package/src/lib/aws/construct/api-to-eventbridge-target/main.ts +7 -1
  66. package/src/lib/aws/construct/api-to-eventbridge-target/types.ts +6 -7
  67. package/src/lib/aws/construct/api-to-eventbridge-target-with-sns/main.ts +7 -2
  68. package/src/lib/aws/construct/api-to-lambda-target/main.ts +1 -1
  69. package/src/lib/aws/construct/lambda-with-iam-access/main.ts +5 -5
  70. package/src/lib/aws/construct/site-with-ecs-backend/main.ts +15 -9
  71. package/src/lib/aws/construct/site-with-lambda-backend/main.ts +11 -3
  72. package/src/lib/aws/services/api-gateway/main.ts +3 -1
  73. package/src/lib/aws/services/api-gateway/types.ts +7 -1
  74. package/src/lib/aws/services/appconfig/main.ts +1 -1
  75. package/src/lib/aws/services/appconfig/types.ts +2 -0
  76. package/src/lib/aws/services/cloudfront/main.ts +6 -2
  77. package/src/lib/aws/services/cloudfront/types.ts +2 -0
  78. package/src/lib/aws/services/cloudtrail/main.ts +2 -1
  79. package/src/lib/aws/services/cloudtrail/types.ts +4 -1
  80. package/src/lib/aws/services/dynamodb/main.ts +2 -1
  81. package/src/lib/aws/services/dynamodb/types.ts +4 -1
  82. package/src/lib/aws/services/elastic-container-service/main.ts +4 -2
  83. package/src/lib/aws/services/elastic-container-service/types.ts +2 -0
  84. package/src/lib/aws/services/elastic-file-system/main.ts +2 -1
  85. package/src/lib/aws/services/elastic-file-system/types.ts +3 -1
  86. package/src/lib/aws/services/elastic-kubernetes-service/main.ts +2 -1
  87. package/src/lib/aws/services/elastic-kubernetes-service/types.ts +2 -0
  88. package/src/lib/aws/services/elasticache/main.ts +2 -1
  89. package/src/lib/aws/services/elasticache/types.ts +4 -1
  90. package/src/lib/aws/services/eventbridge/main.ts +28 -9
  91. package/src/lib/aws/services/eventbridge/types.ts +11 -2
  92. package/src/lib/aws/services/evidently/main.ts +18 -4
  93. package/src/lib/aws/services/evidently/types.ts +13 -4
  94. package/src/lib/aws/services/identity-access-management/main.ts +10 -10
  95. package/src/lib/aws/services/lambda/main.ts +4 -2
  96. package/src/lib/aws/services/lambda/types.ts +5 -2
  97. package/src/lib/aws/services/secrets-manager/index.ts +1 -0
  98. package/src/lib/aws/services/secrets-manager/main.ts +7 -3
  99. package/src/lib/aws/services/secrets-manager/types.ts +6 -0
  100. package/src/lib/aws/services/simple-notification-service/main.ts +6 -4
  101. package/src/lib/aws/services/simple-notification-service/types.ts +4 -1
  102. package/src/lib/aws/services/simple-queue-service/main.ts +8 -7
  103. package/src/lib/aws/services/simple-queue-service/types.ts +2 -0
  104. package/src/lib/aws/services/simple-storage-service/main.ts +12 -8
  105. package/src/lib/aws/services/simple-storage-service/types.ts +3 -1
  106. package/src/lib/aws/services/step-function/main.ts +3 -1
  107. package/src/lib/aws/services/step-function/types.ts +4 -1
  108. package/src/lib/aws/services/virtual-private-cloud/main.ts +9 -3
  109. package/src/lib/aws/services/virtual-private-cloud/types.ts +2 -0
  110. package/src/lib/aws/services/web-application-firewall/main.ts +4 -2
  111. package/src/lib/aws/services/web-application-firewall/types.ts +7 -2
@@ -1,6 +1,7 @@
1
- import { TagProps } from '../../types';
2
1
  import * as s3 from 'aws-cdk-lib/aws-s3';
3
2
  import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
3
+ import { ResourceNameFormatterProps } from '../../common';
4
+ import { TagProps } from '../../types';
4
5
  /**
5
6
  */
6
7
  export interface LifecycleRule extends s3.LifecycleRule {
@@ -21,5 +22,6 @@ export interface S3BucketProps extends s3.BucketProps {
21
22
  existingBucket?: boolean;
22
23
  lifecycleRules?: LifecycleRule[];
23
24
  logBucketName?: string;
25
+ resourceNameOptions?: ResourceNameFormatterProps;
24
26
  tags?: TagProps[];
25
27
  }
@@ -366,6 +366,8 @@ class SfnManager {
366
366
  createStateMachine(id, scope, props, definition, logGroup, role) {
367
367
  if (!props)
368
368
  throw `State Machine props undefined for ${id}`;
369
+ if (!props.stateMachineName)
370
+ throw `State Machine stateMachineName undefined for ${id}`;
369
371
  const stateMachine = new aws_stepfunctions_1.StateMachine(scope, `${id}`, {
370
372
  ...props,
371
373
  definitionBody: aws_stepfunctions_1.DefinitionBody.fromChainable(definition),
@@ -375,7 +377,7 @@ class SfnManager {
375
377
  level: props.logs?.level ?? aws_stepfunctions_1.LogLevel.ALL,
376
378
  },
377
379
  role,
378
- stateMachineName: `${props.stateMachineName}-${scope.props.stage}`,
380
+ stateMachineName: scope.resourceNameFormatter(props.stateMachineName, props.resourceNameOptions),
379
381
  });
380
382
  (0, utils_1.createCfnOutput)(`${id}-stateMachineName`, scope, stateMachine.stateMachineName);
381
383
  (0, utils_1.createCfnOutput)(`${id}-stateMachineArn`, scope, stateMachine.stateMachineArn);
@@ -1,5 +1,6 @@
1
1
  import { ChoiceProps, FailProps, MapProps, ParallelProps, PassProps, RetryProps, StateMachineProps, SucceedProps, WaitProps } from 'aws-cdk-lib/aws-stepfunctions';
2
2
  import { CallApiGatewayRestApiEndpointProps, DynamoDeleteItemProps, DynamoGetItemProps, DynamoPutItemProps, LambdaInvokeProps, SqsSendMessageProps, StepFunctionsStartExecutionProps } from 'aws-cdk-lib/aws-stepfunctions-tasks';
3
+ import { ResourceNameFormatterProps } from '../../common';
3
4
  /**
4
5
  */
5
6
  export interface SfnSucceedProps extends SucceedProps {
@@ -76,6 +77,7 @@ export interface SfnCallApiGatewayRestApiEndpointProps extends CallApiGatewayRes
76
77
  /**
77
78
  */
78
79
  export interface SfnStateMachineProps extends StateMachineProps {
80
+ resourceNameOptions?: ResourceNameFormatterProps;
79
81
  }
80
82
  /**
81
83
  */
@@ -38,14 +38,18 @@ class VpcManager {
38
38
  createVpc(id, scope, props) {
39
39
  if (!props)
40
40
  throw `Vpc props undefined for ${id}`;
41
+ if (!props.vpcName)
42
+ throw `Vpc vpcName undefined for ${id}`;
43
+ const vpcName = scope.resourceNameFormatter(props.vpcName, props.resourceNameOptions);
41
44
  let vpc;
42
45
  if (props.isIPV6) {
43
46
  vpc = new ipv6_1.Ipv6Vpc(scope, `${id}`, {
44
47
  ...props,
45
48
  subnetConfiguration: [
46
- { name: `${id}-public`, subnetType: aws_ec2_1.SubnetType.PUBLIC },
47
- { name: `${id}-private`, subnetType: aws_ec2_1.SubnetType.PRIVATE_WITH_EGRESS },
49
+ { name: `${vpcName}-public`, subnetType: aws_ec2_1.SubnetType.PUBLIC },
50
+ { name: `${vpcName}-private`, subnetType: aws_ec2_1.SubnetType.PRIVATE_WITH_EGRESS },
48
51
  ],
52
+ vpcName,
49
53
  });
50
54
  }
51
55
  else {
@@ -78,7 +82,10 @@ class VpcManager {
78
82
  * @param vpcIdentifier optional identifier for VPC
79
83
  */
80
84
  retrieveCommonVpc(id, scope, vpcIdentifier) {
81
- return aws_ec2_1.Vpc.fromLookup(scope, `${id}`, { vpcName: vpcIdentifier ?? CommonVpcIdentifier });
85
+ const vpcName = scope.resourceNameFormatter(vpcIdentifier ?? CommonVpcIdentifier);
86
+ return aws_ec2_1.Vpc.fromLookup(scope, `${id}`, {
87
+ vpcName: scope.resourceNameFormatter(vpcIdentifier ?? CommonVpcIdentifier),
88
+ });
82
89
  }
83
90
  }
84
91
  exports.VpcManager = VpcManager;
@@ -1,4 +1,6 @@
1
1
  import * as ec2 from 'aws-cdk-lib/aws-ec2';
2
+ import { ResourceNameFormatterProps } from '../../common';
2
3
  export interface VpcProps extends ec2.VpcProps {
3
4
  isIPV6?: boolean;
5
+ resourceNameOptions?: ResourceNameFormatterProps;
4
6
  }
@@ -29,10 +29,12 @@ class WafManager {
29
29
  createIpSet(id, scope, props) {
30
30
  if (!props)
31
31
  throw `WAF Ip Set props undefined for ${id}`;
32
+ if (!props.name)
33
+ throw `WAF Ip Set name undefined for ${id}`;
32
34
  const ipSet = new aws_wafv2_1.CfnIPSet(scope, `${id}`, {
33
35
  ...props,
34
36
  description: `IP Set for ${id} - ${scope.props.stage} stage`,
35
- name: scope.isProductionStage() ? props.name : `${props.name}-${scope.props.stage}`,
37
+ name: scope.resourceNameFormatter(props.name, props.resourceNameOptions),
36
38
  });
37
39
  (0, utils_1.createCfnOutput)(`${id}-ipSetId`, scope, ipSet.attrId);
38
40
  (0, utils_1.createCfnOutput)(`${id}-ipSetArn`, scope, ipSet.attrArn);
@@ -47,10 +49,12 @@ class WafManager {
47
49
  createWebAcl(id, scope, props) {
48
50
  if (!props)
49
51
  throw `WAF WebACL props undefined for ${id}`;
52
+ if (!props.name)
53
+ throw `WAF WebACL name undefined for ${id}`;
50
54
  const webAcl = new aws_wafv2_1.CfnWebACL(scope, `${id}`, {
51
55
  ...props,
52
56
  description: `Web Acl for ${id} - ${scope.props.stage} stage`,
53
- name: scope.isProductionStage() ? props.name : `${props.name}-${scope.props.stage}`,
57
+ name: scope.resourceNameFormatter(props.name, props.resourceNameOptions),
54
58
  tags: [{ key: 'service', value: scope.props.name }],
55
59
  });
56
60
  (0, utils_1.createCfnOutput)(`${id}-webAclId`, scope, webAcl.attrId);
@@ -1,9 +1,12 @@
1
1
  import { CfnIPSetProps, CfnWebACLProps } from 'aws-cdk-lib/aws-wafv2';
2
+ import { ResourceNameFormatterProps } from '../../common';
2
3
  /**
3
4
  */
4
5
  export interface WafIPSetProps extends CfnIPSetProps {
6
+ resourceNameOptions?: ResourceNameFormatterProps;
5
7
  }
6
8
  /**
7
9
  */
8
10
  export interface WafWebACLProps extends CfnWebACLProps {
11
+ resourceNameOptions?: ResourceNameFormatterProps;
9
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradientedge/cdk-utils",
3
- "version": "9.9.0",
3
+ "version": "9.10.0",
4
4
  "description": "Utilities for AWS CDK provisioning",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -33,7 +33,7 @@ import {
33
33
  WafManager,
34
34
  } from '../services'
35
35
  import { createCfnOutput } from '../utils'
36
- import { CommonStackProps } from './types'
36
+ import { CommonStackProps, ResourceNameFormatterProps } from './types'
37
37
 
38
38
  /**
39
39
  * @subcategory Construct
@@ -118,6 +118,21 @@ export class CommonConstruct extends Construct {
118
118
  this.determineFullyQualifiedDomain()
119
119
  }
120
120
 
121
+ /**
122
+ * @summary Helper method to format a resource name based on the provided options
123
+ * @param resourceName the resource name to format
124
+ * @param options options to control the formatting of the resource name
125
+ * @returns The formatted resource name
126
+ */
127
+ public resourceNameFormatter(resourceName: string, options?: ResourceNameFormatterProps) {
128
+ const resourceNameElements = []
129
+ resourceNameElements.push(options?.prefix ?? this.props.resourcePrefix)
130
+ resourceNameElements.push(resourceName)
131
+ resourceNameElements.push(options?.suffix ?? this.props.resourceSuffix)
132
+ resourceNameElements.push(this.props.stage)
133
+ return resourceNameElements.filter(resourceNameElement => resourceNameElement != undefined).join('-')
134
+ }
135
+
121
136
  /**
122
137
  * @summary Helper method to add CloudFormation outputs from the construct
123
138
  * @param id scoped id of the resource
@@ -52,11 +52,14 @@ export class CommonStack extends Stack {
52
52
  return {
53
53
  domainName: this.node.tryGetContext('domainName'),
54
54
  excludeDomainNameForBuckets: this.node.tryGetContext('excludeDomainNameForBuckets'),
55
+ excludeAccountNumberForBuckets: this.node.tryGetContext('excludeAccountNumberForBuckets'),
55
56
  extraContexts: this.node.tryGetContext('extraContexts'),
56
57
  logRetention: this.node.tryGetContext('logRetention'),
57
58
  name: props.stackName || 'cdk-utils',
58
59
  nodejsRuntime: this.node.tryGetContext('nodejsRuntime') ?? CommonStack.NODEJS_RUNTIME,
59
60
  region: this.node.tryGetContext('region'),
61
+ resourcePrefix: this.node.tryGetContext('resourcePrefix'),
62
+ resourceSuffix: this.node.tryGetContext('resourceSuffix'),
60
63
  skipStageForARecords: this.node.tryGetContext('skipStageForARecords'),
61
64
  stackName: props.stackName,
62
65
  stage: this.node.tryGetContext('stage'),
@@ -7,9 +7,18 @@ import { BaseProps } from '../../common'
7
7
  */
8
8
  export interface CommonStackProps extends BaseProps, StackProps {
9
9
  region: string
10
+ resourceProjectIdentifier?: string
11
+ resourcePrefix?: string
12
+ resourceSuffix?: string
10
13
  logRetention?: RetentionDays
11
14
  defaultReservedLambdaConcurrentExecutions?: number
12
15
  defaultTracing?: Tracing
13
16
  excludeDomainNameForBuckets?: boolean
17
+ excludeAccountNumberForBuckets?: boolean
14
18
  nodejsRuntime?: Runtime
15
19
  }
20
+
21
+ export interface ResourceNameFormatterProps {
22
+ prefix?: string
23
+ suffix?: string
24
+ }
@@ -126,6 +126,8 @@ export class ApiToAnyTarget extends CommonConstruct {
126
126
  return
127
127
  }
128
128
 
129
+ if (!this.props.api.restApi?.restApiName) throw `RestApi name undefined for ${this.id}`
130
+
129
131
  this.apiToAnyTargetRestApi.api = new RestApi(this, `${this.id}-rest-api`, {
130
132
  cloudWatchRole: this.props.api.restApi?.cloudWatchRole ?? true,
131
133
  defaultCorsPreflightOptions: {
@@ -151,8 +153,11 @@ export class ApiToAnyTarget extends CommonConstruct {
151
153
  endpointConfiguration: {
152
154
  types: [this.isProductionStage() ? EndpointType.EDGE : EndpointType.REGIONAL],
153
155
  },
154
- restApiName: `${this.id}-rest-api-${this.props.stage}`,
155
156
  ...this.props.api.restApi,
157
+ restApiName: this.resourceNameFormatter(
158
+ this.props.api.restApi?.restApiName,
159
+ this.props.api.restApi?.resourceNameOptions
160
+ ),
156
161
  })
157
162
  this.addCfnOutput(`${this.id}-restApiId`, this.apiToAnyTargetRestApi.api.restApiId)
158
163
  this.addCfnOutput(`${this.id}-restApiRootResourceId`, this.apiToAnyTargetRestApi.api.root.resourceId)
@@ -9,13 +9,12 @@ import {
9
9
  MethodResponse,
10
10
  MockIntegration,
11
11
  Resource,
12
- RestApiProps,
13
12
  } from 'aws-cdk-lib/aws-apigateway'
14
13
  import { ICertificate } from 'aws-cdk-lib/aws-certificatemanager'
15
14
  import { LogGroup } from 'aws-cdk-lib/aws-logs'
16
15
  import { IHostedZone } from 'aws-cdk-lib/aws-route53'
17
16
  import { CommonStackProps } from '../../common'
18
- import { AcmProps } from '../../services'
17
+ import { AcmProps, RestApigProps } from '../../services'
19
18
 
20
19
  export interface ApiToAnyTargetRestApiType {
21
20
  accessLogGroup: LogGroup
@@ -54,7 +53,7 @@ export interface ApiToAnyTargetRestApiProps {
54
53
  importedRestApiRootResourceRef?: string
55
54
  methodErrorResponse: MethodResponse
56
55
  methodResponse: MethodResponse
57
- restApi: RestApiProps
56
+ restApi: RestApigProps
58
57
  useExisting: boolean
59
58
  withResource?: boolean
60
59
  }
@@ -374,6 +374,8 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
374
374
  return
375
375
  }
376
376
 
377
+ if (!this.props.api.restApi?.restApiName) throw `RestApi name undefined for ${this.id}`
378
+
377
379
  this.apiToEventBridgeTargetRestApi.api = new RestApi(this, `${this.id}-rest-api`, {
378
380
  cloudWatchRole: this.props.api.restApi?.cloudWatchRole ?? true,
379
381
  defaultCorsPreflightOptions: {
@@ -402,9 +404,13 @@ export class ApiToEventBridgeTarget extends CommonConstruct {
402
404
  endpointConfiguration: {
403
405
  types: [EndpointType.REGIONAL],
404
406
  },
405
- restApiName: `${this.id}-rest-api-${this.props.stage}`,
406
407
  ...this.props.api.restApi,
408
+ restApiName: this.resourceNameFormatter(
409
+ this.props.api.restApi?.restApiName,
410
+ this.props.api.restApi?.resourceNameOptions
411
+ ),
407
412
  })
413
+
408
414
  this.addCfnOutput(`${this.id}-restApiId`, this.apiToEventBridgeTargetRestApi.api.restApiId)
409
415
  this.addCfnOutput(`${this.id}-restApiRootResourceId`, this.apiToEventBridgeTargetRestApi.api.root.resourceId)
410
416
  }
@@ -1,5 +1,3 @@
1
- import { IEventBus, Rule } from 'aws-cdk-lib/aws-events'
2
- import { LogGroup } from 'aws-cdk-lib/aws-logs'
3
1
  import {
4
2
  DomainName,
5
3
  IAuthorizer,
@@ -12,15 +10,16 @@ import {
12
10
  Model,
13
11
  ModelOptions,
14
12
  Resource,
15
- RestApiProps,
16
13
  } from 'aws-cdk-lib/aws-apigateway'
17
14
  import { ICertificate } from 'aws-cdk-lib/aws-certificatemanager'
15
+ import { IEventBus, Rule } from 'aws-cdk-lib/aws-events'
16
+ import { PolicyDocument, Role } from 'aws-cdk-lib/aws-iam'
17
+ import { AssetCode } from 'aws-cdk-lib/aws-lambda'
18
+ import { LogGroup } from 'aws-cdk-lib/aws-logs'
18
19
  import { IHostedZone } from 'aws-cdk-lib/aws-route53'
19
20
  import { ITopic } from 'aws-cdk-lib/aws-sns'
20
- import { PolicyDocument, Role } from 'aws-cdk-lib/aws-iam'
21
21
  import { CommonStackProps } from '../../common'
22
- import { AcmProps, EventRuleProps, LambdaProps, LogProps } from '../../services'
23
- import { AssetCode } from 'aws-cdk-lib/aws-lambda'
22
+ import { AcmProps, EventRuleProps, LambdaProps, LogProps, RestApigProps } from '../../services'
24
23
 
25
24
  /**
26
25
  */
@@ -73,7 +72,7 @@ export interface ApiToEventBridgeTargetRestApiProps {
73
72
  methodResponse?: MethodResponse
74
73
  resource: string
75
74
  responseModel?: ModelOptions
76
- restApi?: RestApiProps
75
+ restApi?: RestApigProps
77
76
  useExisting: boolean
78
77
  withResource?: boolean
79
78
  }
@@ -362,7 +362,7 @@ export class ApiToEventBridgeTargetWithSns extends CommonConstruct {
362
362
  `${this.id}-destined-topic`,
363
363
  this,
364
364
  {
365
- topicName: `${this.id}-destined-topic`,
365
+ topicName: this.resourceNameFormatter(`${this.id}-destined-topic`),
366
366
  },
367
367
  this.apiDestinedLambda.function
368
368
  )
@@ -513,6 +513,8 @@ export class ApiToEventBridgeTargetWithSns extends CommonConstruct {
513
513
  removalPolicy: RemovalPolicy.DESTROY,
514
514
  })
515
515
 
516
+ if (!this.props.api.restApi?.restApiName) throw `RestApi name undefined for ${this.id}`
517
+
516
518
  this.apiDestinedRestApi.api = new RestApi(this, `${this.id}-sns-rest-api`, {
517
519
  defaultCorsPreflightOptions: {
518
520
  allowHeaders: Cors.DEFAULT_HEADERS,
@@ -535,8 +537,11 @@ export class ApiToEventBridgeTargetWithSns extends CommonConstruct {
535
537
  endpointConfiguration: {
536
538
  types: [EndpointType.REGIONAL],
537
539
  },
538
- restApiName: `${this.id}-destined-rest-api-${this.props.stage}`,
539
540
  ...this.props.api,
541
+ restApiName: this.resourceNameFormatter(
542
+ this.props.api.restApi?.restApiName,
543
+ this.props.api.restApi?.resourceNameOptions
544
+ ),
540
545
  })
541
546
  this.addCfnOutput(`${this.id}-restApiId`, this.apiDestinedRestApi.api.restApiId)
542
547
  this.addCfnOutput(`${this.id}-restApiRootResourceId`, this.apiDestinedRestApi.api.root.resourceId)
@@ -138,7 +138,7 @@ export class ApiToLambdaTarget extends CommonConstruct {
138
138
  this.apiToLambdaTargetRestApi.lambda = Function.fromFunctionName(
139
139
  this,
140
140
  `${this.id}-lambda`,
141
- this.props.lambdaFunctionName
141
+ this.resourceNameFormatter(this.props.lambdaFunctionName)
142
142
  )
143
143
  }
144
144
 
@@ -187,11 +187,11 @@ export class LambdaWithIamAccess extends CommonConstruct {
187
187
  */
188
188
  protected createIamUserForLambdaFunction() {
189
189
  this.lambdaIamUser = new User(this, `${this.id}-lambda-user`, {
190
- userName: `${this.id}-user-${this.props.stage}`,
190
+ userName: this.resourceNameFormatter(`${this.id}-user`),
191
191
  })
192
192
 
193
193
  new Policy(this, `${this.id}-lambda-user-policy`, {
194
- policyName: `${this.id}-policy-${this.props.stage}`,
194
+ policyName: this.resourceNameFormatter(`${this.id}-policy`),
195
195
  statements: [
196
196
  new PolicyStatement({
197
197
  actions: ['lambda:InvokeFunction'],
@@ -204,7 +204,7 @@ export class LambdaWithIamAccess extends CommonConstruct {
204
204
  if (this.props.lambda.lambdaAliases && !_.isEmpty(this.props.lambda.lambdaAliases)) {
205
205
  _.forEach(this.props.lambda.lambdaAliases, (alias, index) => {
206
206
  new Policy(this, `${this.id}-alias-user-policy`, {
207
- policyName: `${this.id}-alias-policy-${index}-${this.props.stage}`,
207
+ policyName: this.resourceNameFormatter(`${this.id}--alias-policy-${index}`),
208
208
  statements: [
209
209
  new PolicyStatement({
210
210
  actions: ['lambda:InvokeFunction'],
@@ -230,9 +230,9 @@ export class LambdaWithIamAccess extends CommonConstruct {
230
230
  * @summary Method to create iam secret for the lambda function
231
231
  */
232
232
  protected createIamSecretForLambdaFunction() {
233
- this.lambdaUserAccessSecret = new Secret(
234
- this,
233
+ this.lambdaUserAccessSecret = this.secretsManager.createSecret(
235
234
  `${this.id}-lambda-user-secret-${this.props.stage}`,
235
+ this,
236
236
  this.props.lambdaSecret
237
237
  )
238
238
 
@@ -296,13 +296,11 @@ export class SiteWithEcsBackend extends CommonConstruct {
296
296
  enableECSManagedTags: true,
297
297
  healthCheckGracePeriod: Duration.seconds(60),
298
298
  listenerPort: this.props.siteTask.listenerPort,
299
- loadBalancerName: this.props.siteTask.loadBalancerName
300
- ? `${this.props.siteTask.loadBalancerName}-${this.props.stage}`
301
- : `${this.id}-${this.props.stage}`,
299
+ loadBalancerName: this.resourceNameFormatter(this.props.siteTask.loadBalancerName ?? this.id),
302
300
  maxHealthyPercent: this.props.siteTask.maxHealthyPercent,
303
301
  memoryLimitMiB: this.props.siteTask.memoryLimitMiB,
304
302
  minHealthyPercent: this.props.siteTask.minHealthyPercent,
305
- serviceName: `${this.id}-${this.props.stage}`,
303
+ serviceName: this.resourceNameFormatter(this.props.siteTask.serviceName ?? this.id),
306
304
  taskDefinition: this.props.siteTask.taskDefinition,
307
305
  taskImageOptions: {
308
306
  containerPort: this.props.siteTask.taskImageOptions?.containerPort,
@@ -402,7 +400,7 @@ export class SiteWithEcsBackend extends CommonConstruct {
402
400
  transitEncryption: this.props.siteFileSystem.transitEncryption,
403
401
  transitEncryptionPort: this.props.siteFileSystem.transitEncryptionPort,
404
402
  },
405
- name: `${this.id}-fs`,
403
+ name: this.resourceNameFormatter(this.props.siteFileSystem?.fileSystemName ?? this.id),
406
404
  })
407
405
 
408
406
  if (this.props.siteTask.mountPoints && !_.isEmpty(this.props.siteTask.mountPoints)) {
@@ -410,7 +408,7 @@ export class SiteWithEcsBackend extends CommonConstruct {
410
408
  this.siteEcsTaskDefinition.defaultContainer?.addMountPoints({
411
409
  containerPath: mountPoint.containerPath,
412
410
  readOnly: mountPoint.readOnly,
413
- sourceVolume: `${this.id}-fs`,
411
+ sourceVolume: this.resourceNameFormatter(this.props.siteFileSystem?.fileSystemName ?? this.id),
414
412
  })
415
413
  })
416
414
  }
@@ -430,8 +428,10 @@ export class SiteWithEcsBackend extends CommonConstruct {
430
428
  }
431
429
 
432
430
  protected createSiteCachePolicy(id: string, siteCachePolicy: SiteCachePolicyProps) {
431
+ if (!siteCachePolicy.cachePolicyName) throw `SiteCachePolicy cachePolicyName undefined for ${id}`
432
+
433
433
  return new CachePolicy(this, `${id}`, {
434
- cachePolicyName: `${this.id}-${siteCachePolicy.cachePolicyName}`,
434
+ cachePolicyName: this.resourceNameFormatter(siteCachePolicy.cachePolicyName),
435
435
  comment: `Policy for ${this.id}-distribution - ${this.props.stage} stage`,
436
436
  cookieBehavior: siteCachePolicy.cookieBehavior,
437
437
  enableAcceptEncodingBrotli: siteCachePolicy.enableAcceptEncodingBrotli,
@@ -453,11 +453,14 @@ export class SiteWithEcsBackend extends CommonConstruct {
453
453
 
454
454
  protected createSiteOriginRequestPolicy() {
455
455
  if (!this.props.siteOriginRequestPolicy) return
456
+ if (!this.props.siteOriginRequestPolicy.originRequestPolicyName)
457
+ throw `SiteOriginRequestPolicy originRequestPolicyName undefined for ${this.id}`
458
+
456
459
  this.siteOriginRequestPolicy = new OriginRequestPolicy(this, `${this.id}-sorp`, {
457
460
  comment: `Request Policy for ${this.id}-distribution - ${this.props.stage} stage`,
458
461
  cookieBehavior: this.props.siteOriginRequestPolicy.cookieBehavior,
459
462
  headerBehavior: this.props.siteOriginRequestPolicy.headerBehavior,
460
- originRequestPolicyName: `${this.id}-origin-request`,
463
+ originRequestPolicyName: this.resourceNameFormatter(this.props.siteOriginRequestPolicy.originRequestPolicyName),
461
464
  queryStringBehavior: this.props.siteOriginRequestPolicy.queryStringBehavior,
462
465
  })
463
466
 
@@ -468,10 +471,13 @@ export class SiteWithEcsBackend extends CommonConstruct {
468
471
 
469
472
  protected createResponseHeaderPolicy(props: SiteResponseHeadersPolicyProps) {
470
473
  if (!props) return undefined
474
+ if (!props.responseHeadersPolicyName)
475
+ throw `SiteResponseHeadersPolicy responseHeadersPolicyName undefined for ${this.id}`
476
+
471
477
  return new ResponseHeadersPolicy(this, `${this.id}-${props.type}-srhp`, {
472
478
  ...props,
473
479
  comment: `Response Header Policy for ${props.type} for ${this.id}-distribution - ${this.props.stage} stage`,
474
- responseHeadersPolicyName: `${this.id}-${props.type}-response`,
480
+ responseHeadersPolicyName: this.resourceNameFormatter(props.responseHeadersPolicyName),
475
481
  securityHeadersBehavior: {
476
482
  ...props.securityHeadersBehavior,
477
483
  strictTransportSecurity: {
@@ -190,8 +190,10 @@ export class SiteWithLambdaBackend extends CommonConstruct {
190
190
  }
191
191
 
192
192
  protected createSiteCachePolicy(id: string, siteCachePolicy: SiteWithLambdaBackendCachePolicyProps) {
193
+ if (!siteCachePolicy.cachePolicyName) throw `SiteCachePolicy cachePolicyName undefined for ${id}`
194
+
193
195
  return new CachePolicy(this, `${id}`, {
194
- cachePolicyName: `${this.id}-${siteCachePolicy.cachePolicyName}`,
196
+ cachePolicyName: this.resourceNameFormatter(siteCachePolicy.cachePolicyName),
195
197
  comment: `Policy for ${this.id}-distribution - ${this.props.stage} stage`,
196
198
  cookieBehavior: siteCachePolicy.cookieBehavior,
197
199
  enableAcceptEncodingBrotli: siteCachePolicy.enableAcceptEncodingBrotli,
@@ -213,11 +215,14 @@ export class SiteWithLambdaBackend extends CommonConstruct {
213
215
 
214
216
  protected createSiteOriginRequestPolicy() {
215
217
  if (!this.props.siteOriginRequestPolicy) return
218
+ if (!this.props.siteOriginRequestPolicy.originRequestPolicyName)
219
+ throw `SiteOriginRequestPolicy originRequestPolicyName undefined for ${this.id}`
220
+
216
221
  this.siteOriginRequestPolicy = new OriginRequestPolicy(this, `${this.id}-sorp`, {
217
222
  comment: `Request Policy for ${this.id}-distribution - ${this.props.stage} stage`,
218
223
  cookieBehavior: this.props.siteOriginRequestPolicy.cookieBehavior,
219
224
  headerBehavior: this.props.siteOriginRequestPolicy.headerBehavior,
220
- originRequestPolicyName: `${this.id}-origin-request`,
225
+ originRequestPolicyName: this.resourceNameFormatter(this.props.siteOriginRequestPolicy.originRequestPolicyName),
221
226
  queryStringBehavior: this.props.siteOriginRequestPolicy.queryStringBehavior,
222
227
  })
223
228
 
@@ -228,10 +233,13 @@ export class SiteWithLambdaBackend extends CommonConstruct {
228
233
 
229
234
  protected createResponseHeaderPolicy(props: SiteWithLambdaBackendResponseHeadersPolicyProps) {
230
235
  if (!props) return undefined
236
+ if (!props.responseHeadersPolicyName)
237
+ throw `SiteResponseHeadersPolicy responseHeadersPolicyName undefined for ${this.id}`
238
+
231
239
  return new ResponseHeadersPolicy(this, `${this.id}-${props.type}-srhp`, {
232
240
  ...props,
233
241
  comment: `Response Header Policy for ${props.type} for ${this.id}-distribution - ${this.props.stage} stage`,
234
- responseHeadersPolicyName: `${this.id}-${props.type}-response`,
242
+ responseHeadersPolicyName: this.resourceNameFormatter(props.responseHeadersPolicyName),
235
243
  securityHeadersBehavior: {
236
244
  ...props.securityHeadersBehavior,
237
245
  strictTransportSecurity: {
@@ -46,6 +46,8 @@ export class ApiManager {
46
46
  */
47
47
  public createLambdaRestApi(id: string, scope: CommonConstruct, props: LambdaRestApiProps, lambdaFunction: IFunction) {
48
48
  if (!props) throw `Api props undefined for ${id}`
49
+ if (!props.restApiName) throw `Api restApiName undefined for ${id}`
50
+
49
51
  const api = new LambdaRestApi(scope, `${id}`, {
50
52
  ...props,
51
53
  cloudWatchRole: props.cloudWatchRole || false,
@@ -63,7 +65,7 @@ export class ApiManager {
63
65
  handler: lambdaFunction,
64
66
  minCompressionSize: props.minCompressionSizeInBytes ? Size.bytes(props.minCompressionSizeInBytes) : undefined,
65
67
  proxy: props.proxy ?? true,
66
- restApiName: `${props.restApiName}-${scope.props.stage}`,
68
+ restApiName: scope.resourceNameFormatter(props.restApiName, props.resourceNameOptions),
67
69
  })
68
70
 
69
71
  if (props.tags && !_.isEmpty(props.tags)) {
@@ -1,4 +1,5 @@
1
- import { LambdaRestApiProps as LambdaRestApigProps } from 'aws-cdk-lib/aws-apigateway'
1
+ import { LambdaRestApiProps as LambdaRestApigProps, RestApiProps } from 'aws-cdk-lib/aws-apigateway'
2
+ import { ResourceNameFormatterProps } from '../../common'
2
3
  import { TagProps } from '../../types'
3
4
 
4
5
  /**
@@ -6,4 +7,9 @@ import { TagProps } from '../../types'
6
7
  export interface LambdaRestApiProps extends LambdaRestApigProps {
7
8
  tags?: TagProps[]
8
9
  minCompressionSizeInBytes?: number
10
+ resourceNameOptions?: ResourceNameFormatterProps
11
+ }
12
+
13
+ export interface RestApigProps extends RestApiProps {
14
+ resourceNameOptions?: ResourceNameFormatterProps
9
15
  }
@@ -51,7 +51,7 @@ export class AppConfigManager {
51
51
 
52
52
  const application = new CfnApplication(scope, `${id}`, {
53
53
  ...props.application,
54
- name: `${props.application.name}-${scope.props.stage}`,
54
+ name: scope.resourceNameFormatter(props.application.name, props.resourceNameOptions),
55
55
  })
56
56
 
57
57
  createCfnOutput(`${id}-ApplicationId`, scope, Fn.ref(application.logicalId))
@@ -5,6 +5,7 @@ import {
5
5
  CfnDeploymentStrategyProps,
6
6
  CfnEnvironmentProps,
7
7
  } from 'aws-cdk-lib/aws-appconfig'
8
+ import { ResourceNameFormatterProps } from '../../common'
8
9
 
9
10
  /**
10
11
  */
@@ -15,4 +16,5 @@ export interface AppConfigProps {
15
16
  deploymentStrategy: CfnDeploymentStrategyProps
16
17
  environment: CfnEnvironmentProps
17
18
  id: string
19
+ resourceNameOptions?: ResourceNameFormatterProps
18
20
  }
@@ -275,6 +275,7 @@ export class CloudFrontManager {
275
275
  mountPath?: string
276
276
  ) {
277
277
  if (!props) throw `EdgeFunction props undefined for ${id}`
278
+ if (!props.functionName) throw `EdgeFunction functionName undefined for ${id}`
278
279
 
279
280
  const edgeFunction = new cf.experimental.EdgeFunction(scope, `${id}`, {
280
281
  code: code,
@@ -283,7 +284,7 @@ export class CloudFrontManager {
283
284
  ...environment,
284
285
  },
285
286
  filesystem: accessPoint ? FileSystem.fromEfsAccessPoint(accessPoint, mountPath ?? '/mnt/msg') : undefined,
286
- functionName: `${props.functionName}-${scope.props.stage}`,
287
+ functionName: scope.resourceNameFormatter(props.functionName, props.resourceNameOptions),
287
288
  handler: props.handler ?? 'index.handler',
288
289
  layers: layers,
289
290
  logRetention: props.logRetention,
@@ -351,12 +352,15 @@ export class CloudFrontManager {
351
352
  * @param props
352
353
  */
353
354
  public createCloudfrontFunction(id: string, scope: CommonConstruct, props: CloudfrontFunctionProps) {
355
+ if (!props) throw `CloudFront Function props undefined for ${id}`
356
+ if (!props.functionName) throw `CloudFront Function functionName undefined for ${id}`
357
+
354
358
  const cloudfrontFunction = new Function(scope, `${id}`, {
355
359
  code: FunctionCode.fromFile({
356
360
  filePath: props.functionFilePath,
357
361
  }),
358
362
  comment: props.comment,
359
- functionName: `${props.functionName}-${scope.props.stage}`,
363
+ functionName: scope.resourceNameFormatter(props.functionName, props.resourceNameOptions),
360
364
  })
361
365
 
362
366
  createCfnOutput(`${id}-functionArn`, scope, cloudfrontFunction.functionArn)
@@ -3,6 +3,7 @@ import {
3
3
  CloudFrontWebDistributionProps,
4
4
  FunctionProps,
5
5
  } from 'aws-cdk-lib/aws-cloudfront'
6
+ import { ResourceNameFormatterProps } from '../../common'
6
7
  import { TagProps } from '../../types'
7
8
 
8
9
  /**
@@ -22,4 +23,5 @@ export interface DistributionProps extends CfDistributionProps {
22
23
  export interface CloudfrontFunctionProps extends FunctionProps {
23
24
  eventType: string
24
25
  functionFilePath: string
26
+ resourceNameOptions?: ResourceNameFormatterProps
25
27
  }
@@ -49,6 +49,7 @@ export class CloudTrailManager {
49
49
  logBucketPolicy: CfnBucketPolicy
50
50
  ) {
51
51
  if (!props) throw `CloudTrail props undefined for ${id}`
52
+ if (!props.trailName) throw `CloudTrail trailName undefined for ${id}`
52
53
 
53
54
  const role = scope.iamManager.createRoleForCloudTrail(`${id}Role`, scope, logGroup)
54
55
 
@@ -71,7 +72,7 @@ export class CloudTrailManager {
71
72
  s3BucketName: logBucket.bucketName,
72
73
  s3KeyPrefix: `logs-${props.trailName}`,
73
74
  tags: [{ key: 'service', value: scope.props.name }],
74
- trailName: `${props.trailName}-${scope.props.stage}`,
75
+ trailName: scope.resourceNameFormatter(props.trailName, props.resourceNameOptions),
75
76
  })
76
77
 
77
78
  cloudTrail.addDependency(logBucketPolicy)