@gradientedge/cdk-utils 7.9.0 → 7.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.
@@ -24,9 +24,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.SiteWithEcsBackend = void 0;
27
+ const cdk = __importStar(require("aws-cdk-lib"));
27
28
  const cloudfront = __importStar(require("aws-cdk-lib/aws-cloudfront"));
28
29
  const origins = __importStar(require("aws-cdk-lib/aws-cloudfront-origins"));
29
30
  const ecs = __importStar(require("aws-cdk-lib/aws-ecs"));
31
+ const ecsPatterns = __importStar(require("aws-cdk-lib/aws-ecs-patterns"));
30
32
  const iam = __importStar(require("aws-cdk-lib/aws-iam"));
31
33
  const common_1 = require("../../common");
32
34
  /**
@@ -219,22 +221,47 @@ class SiteWithEcsBackend extends common_1.CommonConstruct {
219
221
  * @protected
220
222
  */
221
223
  createEcsService() {
222
- const fargateService = this.ecsManager.createLoadBalancedFargateService(this.id, this, {
223
- ...this.props.siteTask,
224
- ...{
225
- domainName: this.siteInternalDomainName,
226
- domainZone: this.siteHostedZone,
227
- healthCheck: this.props.siteHealthCheck,
228
- taskImageOptions: {
229
- ...this.props.siteTask.taskImageOptions,
230
- environment: this.siteEcsEnvironment,
231
- executionRole: this.siteEcsRole,
232
- taskRole: this.siteEcsRole,
233
- image: this.siteEcsContainerImage,
234
- secrets: this.siteSecrets,
235
- },
224
+ const fargateService = new ecsPatterns.ApplicationLoadBalancedFargateService(this, `${this.id}-ecs-service`, {
225
+ cluster: this.siteEcsCluster,
226
+ desiredCount: this.props.siteTask.desiredCount,
227
+ enableECSManagedTags: true,
228
+ serviceName: `${this.id}-${this.props.stage}`,
229
+ cpu: this.props.siteTask.cpu,
230
+ loadBalancerName: `${this.id}-${this.props.stage}`,
231
+ domainName: this.siteInternalDomainName,
232
+ domainZone: this.siteHostedZone,
233
+ listenerPort: this.props.siteTask.listenerPort,
234
+ memoryLimitMiB: this.props.siteTask.memoryLimitMiB,
235
+ healthCheckGracePeriod: cdk.Duration.seconds(60),
236
+ assignPublicIp: true,
237
+ taskImageOptions: {
238
+ enableLogging: true,
239
+ logDriver: ecs.LogDriver.awsLogs({
240
+ logGroup: this.siteEcsLogGroup,
241
+ streamPrefix: `${this.id}-${this.props.stage}/ecs`,
242
+ }),
243
+ image: this.siteEcsContainerImage,
244
+ executionRole: this.siteEcsRole,
245
+ taskRole: this.siteEcsRole,
246
+ containerPort: this.props.siteTask.taskImageOptions?.containerPort,
247
+ environment: this.siteEcsEnvironment,
248
+ secrets: this.siteSecrets,
236
249
  },
237
- }, this.siteEcsCluster, this.siteEcsLogGroup);
250
+ });
251
+ if (this.props.siteHealthCheck) {
252
+ fargateService.targetGroup.configureHealthCheck({
253
+ enabled: this.props.siteHealthCheck.enabled ?? true,
254
+ path: this.props.siteHealthCheck.path ?? '/',
255
+ port: this.props.siteHealthCheck.port,
256
+ interval: cdk.Duration.seconds(this.props.siteHealthCheck.intervalInSecs),
257
+ timeout: cdk.Duration.seconds(this.props.siteHealthCheck.timeoutInSecs),
258
+ healthyThresholdCount: this.props.siteHealthCheck.healthyThresholdCount,
259
+ unhealthyThresholdCount: this.props.siteHealthCheck.unhealthyThresholdCount,
260
+ healthyGrpcCodes: this.props.siteHealthCheck.healthyGrpcCodes,
261
+ healthyHttpCodes: this.props.siteHealthCheck.healthyHttpCodes,
262
+ protocol: this.props.siteHealthCheck.protocol,
263
+ });
264
+ }
238
265
  this.siteEcsService = fargateService.service;
239
266
  this.siteEcsTaskDefinition = fargateService.taskDefinition;
240
267
  this.siteEcsListener = fargateService.listener;
@@ -66,4 +66,19 @@ export declare class LambdaManager {
66
66
  * @param {string?} mountPath
67
67
  */
68
68
  createEdgeFunction(id: string, scope: common.CommonConstruct, props: types.LambdaEdgeProps, layers: lambda.ILayerVersion[], code: lambda.AssetCode, environment?: any, vpc?: ec2.IVpc, securityGroups?: ec2.ISecurityGroup[], accessPoint?: efs.IAccessPoint, mountPath?: string): cdk.aws_cloudfront.experimental.EdgeFunction;
69
+ /**
70
+ * @summary Method to create a lambda function (nodejs) with docker image
71
+ * @param {string} id scoped id of the resource
72
+ * @param {common.CommonConstruct} scope scope in which this resource is defined
73
+ * @param {types.LambdaProps} props
74
+ * @param {iam.Role | iam.CfnRole} role
75
+ * @param {lambda.DockerImageCode} code
76
+ * @param {Map<string, string>?} environment
77
+ * @param {ec2.IVpc?} vpc
78
+ * @param {ec2.ISecurityGroup[]?} securityGroups
79
+ * @param {efs.IAccessPoint?} accessPoint
80
+ * @param {string?} mountPath
81
+ * @param {ec2.SubnetSelection?} vpcSubnets
82
+ */
83
+ createLambdaDockerFunction(id: string, scope: common.CommonConstruct, props: types.LambdaProps, role: iam.Role | iam.CfnRole, code: lambda.DockerImageCode, environment?: any, vpc?: ec2.IVpc, securityGroups?: ec2.ISecurityGroup[], accessPoint?: efs.IAccessPoint, mountPath?: string, vpcSubnets?: ec2.SubnetSelection): cdk.aws_lambda.DockerImageFunction;
69
84
  }
@@ -142,5 +142,59 @@ class LambdaManager {
142
142
  createEdgeFunction(id, scope, props, layers, code, environment, vpc, securityGroups, accessPoint, mountPath) {
143
143
  return new cloudfront_manager_1.CloudFrontManager().createEdgeFunction(id, scope, props, layers, code, environment, vpc, securityGroups, accessPoint, mountPath);
144
144
  }
145
+ /**
146
+ * @summary Method to create a lambda function (nodejs) with docker image
147
+ * @param {string} id scoped id of the resource
148
+ * @param {common.CommonConstruct} scope scope in which this resource is defined
149
+ * @param {types.LambdaProps} props
150
+ * @param {iam.Role | iam.CfnRole} role
151
+ * @param {lambda.DockerImageCode} code
152
+ * @param {Map<string, string>?} environment
153
+ * @param {ec2.IVpc?} vpc
154
+ * @param {ec2.ISecurityGroup[]?} securityGroups
155
+ * @param {efs.IAccessPoint?} accessPoint
156
+ * @param {string?} mountPath
157
+ * @param {ec2.SubnetSelection?} vpcSubnets
158
+ */
159
+ createLambdaDockerFunction(id, scope, props, role, code, environment, vpc, securityGroups, accessPoint, mountPath, vpcSubnets) {
160
+ if (!props)
161
+ throw `Lambda props undefined for ${id}`;
162
+ const functionName = `${props.functionName}-${scope.props.stage}`;
163
+ let deadLetterQueue;
164
+ if (props.deadLetterQueueEnabled && props.dlq) {
165
+ const redriveQueue = scope.sqsManager.createRedriveQueueForLambda(`${id}-rdq`, scope, props);
166
+ deadLetterQueue = scope.sqsManager.createDeadLetterQueueForLambda(`${id}-dlq`, scope, props, redriveQueue);
167
+ }
168
+ const lambdaFunction = new lambda.DockerImageFunction(scope, `${id}`, {
169
+ ...props,
170
+ ...{
171
+ allowPublicSubnet: !!vpc,
172
+ functionName: functionName,
173
+ runtime: LambdaManager.NODEJS_RUNTIME,
174
+ code: code,
175
+ deadLetterQueue: deadLetterQueue,
176
+ architecture: props.architecture ?? lambda.Architecture.ARM_64,
177
+ environment: {
178
+ REGION: scope.props.region,
179
+ LAST_MODIFIED_TS: new Date().toISOString(),
180
+ STAGE: scope.props.stage,
181
+ ...environment,
182
+ },
183
+ filesystem: accessPoint
184
+ ? lambda.FileSystem.fromEfsAccessPoint(accessPoint, mountPath || '/mnt/msg')
185
+ : undefined,
186
+ reservedConcurrentExecutions: props.reservedConcurrentExecutions,
187
+ role: role instanceof iam.Role ? role : undefined,
188
+ securityGroups: securityGroups,
189
+ timeout: props.timeoutInSecs ? cdk.Duration.seconds(props.timeoutInSecs) : cdk.Duration.minutes(1),
190
+ vpc: vpc,
191
+ vpcSubnets: vpcSubnets,
192
+ tracing: props.tracing,
193
+ },
194
+ });
195
+ utils.createCfnOutput(`${id}-lambdaArn`, scope, lambdaFunction.functionArn);
196
+ utils.createCfnOutput(`${id}-lambdaName`, scope, lambdaFunction.functionName);
197
+ return lambdaFunction;
198
+ }
145
199
  }
146
200
  exports.LambdaManager = LambdaManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradientedge/cdk-utils",
3
- "version": "7.9.0",
3
+ "version": "7.10.0",
4
4
  "description": "Utilities for AWS CDK provisioning",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -1,8 +1,10 @@
1
+ import * as cdk from 'aws-cdk-lib'
1
2
  import * as certificateManager from 'aws-cdk-lib/aws-certificatemanager'
2
3
  import * as cloudfront from 'aws-cdk-lib/aws-cloudfront'
3
4
  import * as origins from 'aws-cdk-lib/aws-cloudfront-origins'
4
5
  import * as ec2 from 'aws-cdk-lib/aws-ec2'
5
6
  import * as ecs from 'aws-cdk-lib/aws-ecs'
7
+ import * as ecsPatterns from 'aws-cdk-lib/aws-ecs-patterns'
6
8
  import * as elb from 'aws-cdk-lib/aws-elasticloadbalancingv2'
7
9
  import * as iam from 'aws-cdk-lib/aws-iam'
8
10
  import * as logs from 'aws-cdk-lib/aws-logs'
@@ -241,28 +243,48 @@ export class SiteWithEcsBackend extends CommonConstruct {
241
243
  * @protected
242
244
  */
243
245
  protected createEcsService() {
244
- const fargateService = this.ecsManager.createLoadBalancedFargateService(
245
- this.id,
246
- this,
247
- {
248
- ...this.props.siteTask,
249
- ...{
250
- domainName: this.siteInternalDomainName,
251
- domainZone: this.siteHostedZone,
252
- healthCheck: this.props.siteHealthCheck,
253
- taskImageOptions: {
254
- ...this.props.siteTask.taskImageOptions,
255
- environment: this.siteEcsEnvironment,
256
- executionRole: this.siteEcsRole,
257
- taskRole: this.siteEcsRole,
258
- image: this.siteEcsContainerImage,
259
- secrets: this.siteSecrets,
260
- },
261
- },
246
+ const fargateService = new ecsPatterns.ApplicationLoadBalancedFargateService(this, `${this.id}-ecs-service`, {
247
+ cluster: this.siteEcsCluster,
248
+ desiredCount: this.props.siteTask.desiredCount,
249
+ enableECSManagedTags: true,
250
+ serviceName: `${this.id}-${this.props.stage}`,
251
+ cpu: this.props.siteTask.cpu,
252
+ loadBalancerName: `${this.id}-${this.props.stage}`,
253
+ domainName: this.siteInternalDomainName,
254
+ domainZone: this.siteHostedZone,
255
+ listenerPort: this.props.siteTask.listenerPort,
256
+ memoryLimitMiB: this.props.siteTask.memoryLimitMiB,
257
+ healthCheckGracePeriod: cdk.Duration.seconds(60),
258
+ assignPublicIp: true,
259
+ taskImageOptions: {
260
+ enableLogging: true,
261
+ logDriver: ecs.LogDriver.awsLogs({
262
+ logGroup: this.siteEcsLogGroup,
263
+ streamPrefix: `${this.id}-${this.props.stage}/ecs`,
264
+ }),
265
+ image: this.siteEcsContainerImage,
266
+ executionRole: this.siteEcsRole,
267
+ taskRole: this.siteEcsRole,
268
+ containerPort: this.props.siteTask.taskImageOptions?.containerPort,
269
+ environment: this.siteEcsEnvironment,
270
+ secrets: this.siteSecrets,
262
271
  },
263
- this.siteEcsCluster,
264
- this.siteEcsLogGroup
265
- )
272
+ })
273
+
274
+ if (this.props.siteHealthCheck) {
275
+ fargateService.targetGroup.configureHealthCheck({
276
+ enabled: this.props.siteHealthCheck.enabled ?? true,
277
+ path: this.props.siteHealthCheck.path ?? '/',
278
+ port: this.props.siteHealthCheck.port,
279
+ interval: cdk.Duration.seconds(this.props.siteHealthCheck.intervalInSecs),
280
+ timeout: cdk.Duration.seconds(this.props.siteHealthCheck.timeoutInSecs),
281
+ healthyThresholdCount: this.props.siteHealthCheck.healthyThresholdCount,
282
+ unhealthyThresholdCount: this.props.siteHealthCheck.unhealthyThresholdCount,
283
+ healthyGrpcCodes: this.props.siteHealthCheck.healthyGrpcCodes,
284
+ healthyHttpCodes: this.props.siteHealthCheck.healthyHttpCodes,
285
+ protocol: this.props.siteHealthCheck.protocol,
286
+ })
287
+ }
266
288
 
267
289
  this.siteEcsService = fargateService.service
268
290
  this.siteEcsTaskDefinition = fargateService.taskDefinition
@@ -166,4 +166,75 @@ export class LambdaManager {
166
166
  mountPath
167
167
  )
168
168
  }
169
+
170
+ /**
171
+ * @summary Method to create a lambda function (nodejs) with docker image
172
+ * @param {string} id scoped id of the resource
173
+ * @param {common.CommonConstruct} scope scope in which this resource is defined
174
+ * @param {types.LambdaProps} props
175
+ * @param {iam.Role | iam.CfnRole} role
176
+ * @param {lambda.DockerImageCode} code
177
+ * @param {Map<string, string>?} environment
178
+ * @param {ec2.IVpc?} vpc
179
+ * @param {ec2.ISecurityGroup[]?} securityGroups
180
+ * @param {efs.IAccessPoint?} accessPoint
181
+ * @param {string?} mountPath
182
+ * @param {ec2.SubnetSelection?} vpcSubnets
183
+ */
184
+ public createLambdaDockerFunction(
185
+ id: string,
186
+ scope: common.CommonConstruct,
187
+ props: types.LambdaProps,
188
+ role: iam.Role | iam.CfnRole,
189
+ code: lambda.DockerImageCode,
190
+ environment?: any,
191
+ vpc?: ec2.IVpc,
192
+ securityGroups?: ec2.ISecurityGroup[],
193
+ accessPoint?: efs.IAccessPoint,
194
+ mountPath?: string,
195
+ vpcSubnets?: ec2.SubnetSelection
196
+ ) {
197
+ if (!props) throw `Lambda props undefined for ${id}`
198
+
199
+ const functionName = `${props.functionName}-${scope.props.stage}`
200
+
201
+ let deadLetterQueue
202
+ if (props.deadLetterQueueEnabled && props.dlq) {
203
+ const redriveQueue = scope.sqsManager.createRedriveQueueForLambda(`${id}-rdq`, scope, props)
204
+ deadLetterQueue = scope.sqsManager.createDeadLetterQueueForLambda(`${id}-dlq`, scope, props, redriveQueue)
205
+ }
206
+
207
+ const lambdaFunction = new lambda.DockerImageFunction(scope, `${id}`, {
208
+ ...props,
209
+ ...{
210
+ allowPublicSubnet: !!vpc,
211
+ functionName: functionName,
212
+ runtime: LambdaManager.NODEJS_RUNTIME,
213
+ code: code,
214
+ deadLetterQueue: deadLetterQueue,
215
+ architecture: props.architecture ?? lambda.Architecture.ARM_64,
216
+ environment: {
217
+ REGION: scope.props.region,
218
+ LAST_MODIFIED_TS: new Date().toISOString(),
219
+ STAGE: scope.props.stage,
220
+ ...environment,
221
+ },
222
+ filesystem: accessPoint
223
+ ? lambda.FileSystem.fromEfsAccessPoint(accessPoint, mountPath || '/mnt/msg')
224
+ : undefined,
225
+ reservedConcurrentExecutions: props.reservedConcurrentExecutions,
226
+ role: role instanceof iam.Role ? role : undefined,
227
+ securityGroups: securityGroups,
228
+ timeout: props.timeoutInSecs ? cdk.Duration.seconds(props.timeoutInSecs) : cdk.Duration.minutes(1),
229
+ vpc: vpc,
230
+ vpcSubnets: vpcSubnets,
231
+ tracing: props.tracing,
232
+ },
233
+ })
234
+
235
+ utils.createCfnOutput(`${id}-lambdaArn`, scope, lambdaFunction.functionArn)
236
+ utils.createCfnOutput(`${id}-lambdaName`, scope, lambdaFunction.functionName)
237
+
238
+ return lambdaFunction
239
+ }
169
240
  }