@jaypie/constructs 1.1.21 → 1.1.22

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.
@@ -0,0 +1,73 @@
1
+ import { Construct } from "constructs";
2
+ import { Duration, Stack, RemovalPolicy } from "aws-cdk-lib";
3
+ import * as lambda from "aws-cdk-lib/aws-lambda";
4
+ import * as iam from "aws-cdk-lib/aws-iam";
5
+ import * as cloudwatch from "aws-cdk-lib/aws-cloudwatch";
6
+ import * as secretsmanager from "aws-cdk-lib/aws-secretsmanager";
7
+ import { JaypieEnvSecret } from "./JaypieEnvSecret.js";
8
+ export interface JaypieLambdaProps {
9
+ code: lambda.Code | string;
10
+ datadogApiKeyArn?: string;
11
+ environment?: {
12
+ [key: string]: string;
13
+ };
14
+ envSecrets?: {
15
+ [key: string]: secretsmanager.ISecret;
16
+ };
17
+ handler: string;
18
+ layers?: lambda.ILayerVersion[];
19
+ logRetention?: number;
20
+ memorySize?: number;
21
+ paramsAndSecrets?: lambda.ParamsAndSecretsLayerVersion | boolean;
22
+ paramsAndSecretsOptions?: {
23
+ cacheSize?: number;
24
+ logLevel?: lambda.ParamsAndSecretsLogLevel;
25
+ parameterStoreTtl?: number;
26
+ secretsManagerTtl?: number;
27
+ };
28
+ reservedConcurrentExecutions?: number;
29
+ roleTag?: string;
30
+ runtime?: lambda.Runtime;
31
+ secrets?: JaypieEnvSecret[];
32
+ timeout?: Duration | number;
33
+ vendorTag?: string;
34
+ }
35
+ export declare class JaypieLambda extends Construct implements lambda.IFunction {
36
+ private readonly _lambda;
37
+ private readonly _code;
38
+ constructor(scope: Construct, id: string, props: JaypieLambdaProps);
39
+ get lambda(): lambda.Function;
40
+ get code(): lambda.Code;
41
+ get functionArn(): string;
42
+ get functionName(): string;
43
+ get grantPrincipal(): iam.IPrincipal;
44
+ get role(): iam.IRole | undefined;
45
+ get architecture(): lambda.Architecture;
46
+ get connections(): import("aws-cdk-lib/aws-ec2").Connections;
47
+ get isBoundToVpc(): boolean;
48
+ get latestVersion(): lambda.IVersion;
49
+ get permissionsNode(): import("constructs").Node;
50
+ get resourceArnsForGrantInvoke(): string[];
51
+ addEventSource(source: lambda.IEventSource): void;
52
+ addEventSourceMapping(id: string, options: lambda.EventSourceMappingOptions): lambda.EventSourceMapping;
53
+ addFunctionUrl(options?: lambda.FunctionUrlOptions): lambda.FunctionUrl;
54
+ addPermission(id: string, permission: lambda.Permission): void;
55
+ addToRolePolicy(statement: iam.PolicyStatement): void;
56
+ configureAsyncInvoke(options: lambda.EventInvokeConfigOptions): void;
57
+ grantInvoke(grantee: iam.IGrantable): iam.Grant;
58
+ grantInvokeCompositePrincipal(compositePrincipal: iam.CompositePrincipal): iam.Grant[];
59
+ grantInvokeUrl(grantee: iam.IGrantable): iam.Grant;
60
+ metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric;
61
+ metricDuration(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
62
+ metricErrors(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
63
+ metricInvocations(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
64
+ metricThrottles(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
65
+ grantInvokeLatestVersion(grantee: iam.IGrantable): iam.Grant;
66
+ grantInvokeVersion(grantee: iam.IGrantable, version: lambda.Version): iam.Grant;
67
+ get env(): {
68
+ account: string;
69
+ region: string;
70
+ };
71
+ get stack(): Stack;
72
+ applyRemovalPolicy(policy: RemovalPolicy): void;
73
+ }
@@ -5,35 +5,15 @@ import * as sqs from "aws-cdk-lib/aws-sqs";
5
5
  import * as iam from "aws-cdk-lib/aws-iam";
6
6
  import * as cloudwatch from "aws-cdk-lib/aws-cloudwatch";
7
7
  import * as kms from "aws-cdk-lib/aws-kms";
8
- import * as secretsmanager from "aws-cdk-lib/aws-secretsmanager";
9
- import { JaypieEnvSecret } from "./JaypieEnvSecret.js";
10
- export interface JaypieQueuedLambdaProps {
8
+ import { JaypieLambdaProps } from "./JaypieLambda.js";
9
+ export interface JaypieQueuedLambdaProps extends JaypieLambdaProps {
11
10
  batchSize?: number;
12
- code: lambda.Code | string;
13
- environment?: {
14
- [key: string]: string;
15
- };
16
- envSecrets?: {
17
- [key: string]: secretsmanager.ISecret;
18
- };
19
11
  fifo?: boolean;
20
- handler: string;
21
- layers?: lambda.ILayerVersion[];
22
- logRetention?: number;
23
- memorySize?: number;
24
- paramsAndSecrets?: lambda.ParamsAndSecretsLayerVersion;
25
- reservedConcurrentExecutions?: number;
26
- roleTag?: string;
27
- runtime?: lambda.Runtime;
28
- secrets?: JaypieEnvSecret[];
29
- timeout?: Duration | number;
30
- vendorTag?: string;
31
12
  visibilityTimeout?: Duration | number;
32
13
  }
33
14
  export declare class JaypieQueuedLambda extends Construct implements lambda.IFunction, sqs.IQueue {
34
15
  private readonly _queue;
35
- private readonly _lambda;
36
- private readonly _code;
16
+ private readonly _lambdaConstruct;
37
17
  constructor(scope: Construct, id: string, props: JaypieQueuedLambdaProps);
38
18
  get queue(): sqs.Queue;
39
19
  get lambda(): lambda.Function;
@@ -0,0 +1 @@
1
+ export {};
@@ -215,47 +215,54 @@ class JaypieHostedZone extends constructs.Construct {
215
215
  }
216
216
  }
217
217
 
218
- class JaypieMongoDbSecret extends JaypieEnvSecret {
219
- constructor(scope, id = "MongoConnectionString", props) {
220
- const defaultProps = {
221
- envKey: "MONGODB_URI",
222
- roleTag: cdk$1.CDK.ROLE.STORAGE,
223
- vendorTag: cdk$1.CDK.VENDOR.MONGODB,
224
- ...props,
225
- };
226
- super(scope, id, defaultProps);
227
- }
228
- }
229
-
230
- class JaypieOpenAiSecret extends JaypieEnvSecret {
231
- constructor(scope, id = "OpenAiApiKey", props) {
232
- const defaultProps = {
233
- envKey: "OPENAI_API_KEY",
234
- roleTag: cdk$1.CDK.ROLE.PROCESSING,
235
- vendorTag: cdk$1.CDK.VENDOR.OPENAI,
236
- ...props,
237
- };
238
- super(scope, id, defaultProps);
239
- }
240
- }
241
-
242
- class JaypieQueuedLambda extends constructs.Construct {
218
+ class JaypieLambda extends constructs.Construct {
243
219
  constructor(scope, id, props) {
244
220
  super(scope, id);
245
- const { batchSize = 1, code, environment = {}, envSecrets = {}, fifo = true, handler = "index.handler", layers = [], logRetention = cdk$1.CDK.LAMBDA.LOG_RETENTION, memorySize = cdk$1.CDK.LAMBDA.MEMORY_SIZE, paramsAndSecrets, reservedConcurrentExecutions, roleTag, runtime = lambda__namespace.Runtime.NODEJS_20_X, secrets = [], timeout = cdk.Duration.seconds(cdk$1.CDK.DURATION.LAMBDA_WORKER), vendorTag, visibilityTimeout = cdk.Duration.seconds(cdk$1.CDK.DURATION.LAMBDA_WORKER), } = props;
221
+ const { code, datadogApiKeyArn, environment: initialEnvironment = {}, envSecrets = {}, handler = "index.handler", layers = [], logRetention = cdk$1.CDK.LAMBDA.LOG_RETENTION, memorySize = cdk$1.CDK.LAMBDA.MEMORY_SIZE, paramsAndSecrets, paramsAndSecretsOptions, reservedConcurrentExecutions, roleTag, runtime = lambda__namespace.Runtime.NODEJS_20_X, secrets = [], timeout = cdk.Duration.seconds(cdk$1.CDK.DURATION.LAMBDA_WORKER), vendorTag, } = props;
222
+ // Create a mutable copy of the environment variables
223
+ let environment = { ...initialEnvironment };
246
224
  this._code = typeof code === "string" ? lambda__namespace.Code.fromAsset(code) : code;
247
- // Create SQS Queue
248
- this._queue = new sqs__namespace.Queue(this, "Queue", {
249
- fifo,
250
- visibilityTimeout: typeof visibilityTimeout === "number"
251
- ? cdk.Duration.seconds(visibilityTimeout)
252
- : visibilityTimeout,
253
- });
254
- if (roleTag) {
255
- cdk.Tags.of(this._queue).add(cdk$1.CDK.TAG.ROLE, roleTag);
225
+ // Create a working copy of layers
226
+ const resolvedLayers = [...layers];
227
+ // Determine if we should add Datadog integration
228
+ // Check for datadog API key ARN in different sources
229
+ const resolvedDatadogApiKeyArn = datadogApiKeyArn ||
230
+ process.env.DATADOG_API_KEY_ARN ||
231
+ process.env.CDK_ENV_DATADOG_API_KEY_ARN;
232
+ // Add Datadog integration if API key is available
233
+ if (resolvedDatadogApiKeyArn) {
234
+ // Add Datadog Node.js layer
235
+ const datadogNodeLayer = lambda__namespace.LayerVersion.fromLayerVersionArn(this, "DatadogNodeLayer", `arn:aws:lambda:${cdk.Stack.of(this).region}:464622532012:layer:Datadog-Node20-x:${cdk$1.CDK.DATADOG.LAYER.NODE}`);
236
+ resolvedLayers.push(datadogNodeLayer);
237
+ // Add Datadog Extension layer
238
+ const datadogExtensionLayer = lambda__namespace.LayerVersion.fromLayerVersionArn(this, "DatadogExtensionLayer", `arn:aws:lambda:${cdk.Stack.of(this).region}:464622532012:layer:Datadog-Extension:${cdk$1.CDK.DATADOG.LAYER.EXTENSION}`);
239
+ resolvedLayers.push(datadogExtensionLayer);
240
+ // Set Datadog environment variables
241
+ Object.assign(environment, {
242
+ DD_API_KEY_SECRET_ARN: resolvedDatadogApiKeyArn,
243
+ DD_ENV: process.env.PROJECT_ENV || "",
244
+ DD_SERVICE: process.env.PROJECT_SERVICE || "",
245
+ DD_SITE: cdk$1.CDK.DATADOG.SITE,
246
+ DD_TAGS: `${cdk$1.CDK.TAG.SPONSOR}:${process.env.PROJECT_SPONSOR || ""}`,
247
+ });
256
248
  }
257
- if (vendorTag) {
258
- cdk.Tags.of(this._queue).add(cdk$1.CDK.TAG.VENDOR, vendorTag);
249
+ // Configure ParamsAndSecrets layer
250
+ let resolvedParamsAndSecrets = undefined;
251
+ if (paramsAndSecrets !== false) {
252
+ if (paramsAndSecrets instanceof lambda__namespace.ParamsAndSecretsLayerVersion) {
253
+ resolvedParamsAndSecrets = paramsAndSecrets;
254
+ }
255
+ else {
256
+ // Create default ParamsAndSecrets layer
257
+ resolvedParamsAndSecrets =
258
+ lambda__namespace.ParamsAndSecretsLayerVersion.fromVersion(lambda__namespace.ParamsAndSecretsVersions.V1_0_103, {
259
+ cacheSize: paramsAndSecretsOptions?.cacheSize,
260
+ logLevel: paramsAndSecretsOptions?.logLevel ||
261
+ lambda__namespace.ParamsAndSecretsLogLevel.WARN,
262
+ parameterStoreTtl: paramsAndSecretsOptions?.parameterStoreTtl,
263
+ secretsManagerTtl: paramsAndSecretsOptions?.secretsManagerTtl,
264
+ });
265
+ }
259
266
  }
260
267
  // Process secrets environment variables
261
268
  const secretsEnvironment = Object.entries(envSecrets).reduce((acc, [key, secret]) => ({
@@ -276,16 +283,15 @@ class JaypieQueuedLambda extends constructs.Construct {
276
283
  this._lambda = new lambda__namespace.Function(this, "Function", {
277
284
  code: this._code,
278
285
  environment: {
279
- CDK_ENV_QUEUE_URL: this._queue.queueUrl,
280
286
  ...environment,
281
287
  ...secretsEnvironment,
282
288
  ...jaypieSecretsEnvironment,
283
289
  },
284
290
  handler,
285
- layers,
291
+ layers: resolvedLayers,
286
292
  logRetention,
287
293
  memorySize,
288
- paramsAndSecrets,
294
+ paramsAndSecrets: resolvedParamsAndSecrets,
289
295
  reservedConcurrentExecutions,
290
296
  runtime,
291
297
  timeout: typeof timeout === "number" ? cdk.Duration.seconds(timeout) : timeout,
@@ -299,11 +305,11 @@ class JaypieQueuedLambda extends constructs.Construct {
299
305
  secret.grantRead(this);
300
306
  secret.grantRead(this._lambda);
301
307
  });
302
- this._queue.grantConsumeMessages(this._lambda);
303
- this._queue.grantSendMessages(this._lambda);
304
- this._lambda.addEventSource(new lambdaEventSources__namespace.SqsEventSource(this._queue, {
305
- batchSize,
306
- }));
308
+ // Grant Datadog API key read permission if applicable
309
+ if (resolvedDatadogApiKeyArn) {
310
+ const datadogApiKey = secretsmanager__namespace.Secret.fromSecretCompleteArn(this, "DatadogApiKeyGrant", resolvedDatadogApiKeyArn);
311
+ datadogApiKey.grantRead(this._lambda);
312
+ }
307
313
  if (roleTag) {
308
314
  cdk.Tags.of(this._lambda).add(cdk$1.CDK.TAG.ROLE, roleTag);
309
315
  }
@@ -312,9 +318,6 @@ class JaypieQueuedLambda extends constructs.Construct {
312
318
  }
313
319
  }
314
320
  // Public accessors
315
- get queue() {
316
- return this._queue;
317
- }
318
321
  get lambda() {
319
322
  return this._lambda;
320
323
  }
@@ -412,6 +415,178 @@ class JaypieQueuedLambda extends constructs.Construct {
412
415
  }
413
416
  applyRemovalPolicy(policy) {
414
417
  this._lambda.applyRemovalPolicy(policy);
418
+ }
419
+ }
420
+
421
+ class JaypieMongoDbSecret extends JaypieEnvSecret {
422
+ constructor(scope, id = "MongoConnectionString", props) {
423
+ const defaultProps = {
424
+ envKey: "MONGODB_URI",
425
+ roleTag: cdk$1.CDK.ROLE.STORAGE,
426
+ vendorTag: cdk$1.CDK.VENDOR.MONGODB,
427
+ ...props,
428
+ };
429
+ super(scope, id, defaultProps);
430
+ }
431
+ }
432
+
433
+ class JaypieOpenAiSecret extends JaypieEnvSecret {
434
+ constructor(scope, id = "OpenAiApiKey", props) {
435
+ const defaultProps = {
436
+ envKey: "OPENAI_API_KEY",
437
+ roleTag: cdk$1.CDK.ROLE.PROCESSING,
438
+ vendorTag: cdk$1.CDK.VENDOR.OPENAI,
439
+ ...props,
440
+ };
441
+ super(scope, id, defaultProps);
442
+ }
443
+ }
444
+
445
+ class JaypieQueuedLambda extends constructs.Construct {
446
+ constructor(scope, id, props) {
447
+ super(scope, id);
448
+ const { batchSize = 1, code, environment = {}, envSecrets = {}, fifo = true, handler = "index.handler", layers = [], logRetention = cdk$1.CDK.LAMBDA.LOG_RETENTION, memorySize = cdk$1.CDK.LAMBDA.MEMORY_SIZE, paramsAndSecrets, reservedConcurrentExecutions, roleTag, runtime = lambda__namespace.Runtime.NODEJS_20_X, secrets = [], timeout = cdk.Duration.seconds(cdk$1.CDK.DURATION.LAMBDA_WORKER), vendorTag, visibilityTimeout = cdk.Duration.seconds(cdk$1.CDK.DURATION.LAMBDA_WORKER), } = props;
449
+ // Create SQS Queue
450
+ this._queue = new sqs__namespace.Queue(this, "Queue", {
451
+ fifo,
452
+ visibilityTimeout: typeof visibilityTimeout === "number"
453
+ ? cdk.Duration.seconds(visibilityTimeout)
454
+ : visibilityTimeout,
455
+ });
456
+ if (roleTag) {
457
+ cdk.Tags.of(this._queue).add(cdk$1.CDK.TAG.ROLE, roleTag);
458
+ }
459
+ if (vendorTag) {
460
+ cdk.Tags.of(this._queue).add(cdk$1.CDK.TAG.VENDOR, vendorTag);
461
+ }
462
+ // Create Lambda with JaypieLambda
463
+ this._lambdaConstruct = new JaypieLambda(this, "Function", {
464
+ code,
465
+ environment: {
466
+ ...environment,
467
+ CDK_ENV_QUEUE_URL: this._queue.queueUrl,
468
+ },
469
+ envSecrets,
470
+ handler,
471
+ layers,
472
+ logRetention,
473
+ memorySize,
474
+ paramsAndSecrets,
475
+ reservedConcurrentExecutions,
476
+ roleTag,
477
+ runtime,
478
+ secrets,
479
+ timeout,
480
+ vendorTag,
481
+ });
482
+ // Set up queue and lambda integration
483
+ this._queue.grantConsumeMessages(this._lambdaConstruct);
484
+ this._queue.grantSendMessages(this._lambdaConstruct);
485
+ this._lambdaConstruct.addEventSource(new lambdaEventSources__namespace.SqsEventSource(this._queue, {
486
+ batchSize,
487
+ }));
488
+ }
489
+ // Public accessors
490
+ get queue() {
491
+ return this._queue;
492
+ }
493
+ get lambda() {
494
+ return this._lambdaConstruct.lambda;
495
+ }
496
+ get code() {
497
+ return this._lambdaConstruct.code;
498
+ }
499
+ // IFunction implementation
500
+ get functionArn() {
501
+ return this._lambdaConstruct.functionArn;
502
+ }
503
+ get functionName() {
504
+ return this._lambdaConstruct.functionName;
505
+ }
506
+ get grantPrincipal() {
507
+ return this._lambdaConstruct.grantPrincipal;
508
+ }
509
+ get role() {
510
+ return this._lambdaConstruct.role;
511
+ }
512
+ get architecture() {
513
+ return this._lambdaConstruct.architecture;
514
+ }
515
+ get connections() {
516
+ return this._lambdaConstruct.connections;
517
+ }
518
+ get isBoundToVpc() {
519
+ return this._lambdaConstruct.isBoundToVpc;
520
+ }
521
+ get latestVersion() {
522
+ return this._lambdaConstruct.latestVersion;
523
+ }
524
+ get permissionsNode() {
525
+ return this._lambdaConstruct.permissionsNode;
526
+ }
527
+ get resourceArnsForGrantInvoke() {
528
+ return this._lambdaConstruct.resourceArnsForGrantInvoke;
529
+ }
530
+ addEventSource(source) {
531
+ this._lambdaConstruct.addEventSource(source);
532
+ }
533
+ addEventSourceMapping(id, options) {
534
+ return this._lambdaConstruct.addEventSourceMapping(id, options);
535
+ }
536
+ addFunctionUrl(options) {
537
+ return this._lambdaConstruct.addFunctionUrl(options);
538
+ }
539
+ addPermission(id, permission) {
540
+ this._lambdaConstruct.addPermission(id, permission);
541
+ }
542
+ addToRolePolicy(statement) {
543
+ this._lambdaConstruct.addToRolePolicy(statement);
544
+ }
545
+ configureAsyncInvoke(options) {
546
+ this._lambdaConstruct.configureAsyncInvoke(options);
547
+ }
548
+ grantInvoke(grantee) {
549
+ return this._lambdaConstruct.grantInvoke(grantee);
550
+ }
551
+ grantInvokeCompositePrincipal(compositePrincipal) {
552
+ return this._lambdaConstruct.grantInvokeCompositePrincipal(compositePrincipal);
553
+ }
554
+ grantInvokeUrl(grantee) {
555
+ return this._lambdaConstruct.grantInvokeUrl(grantee);
556
+ }
557
+ metric(metricName, props) {
558
+ return this._lambdaConstruct.metric(metricName, props);
559
+ }
560
+ metricDuration(props) {
561
+ return this._lambdaConstruct.metricDuration(props);
562
+ }
563
+ metricErrors(props) {
564
+ return this._lambdaConstruct.metricErrors(props);
565
+ }
566
+ metricInvocations(props) {
567
+ return this._lambdaConstruct.metricInvocations(props);
568
+ }
569
+ metricThrottles(props) {
570
+ return this._lambdaConstruct.metricThrottles(props);
571
+ }
572
+ // Additional IFunction implementation
573
+ grantInvokeLatestVersion(grantee) {
574
+ return this._lambdaConstruct.grantInvokeLatestVersion(grantee);
575
+ }
576
+ grantInvokeVersion(grantee, version) {
577
+ return this._lambdaConstruct.grantInvokeVersion(grantee, version);
578
+ }
579
+ get env() {
580
+ return {
581
+ account: cdk.Stack.of(this).account,
582
+ region: cdk.Stack.of(this).region,
583
+ };
584
+ }
585
+ get stack() {
586
+ return cdk.Stack.of(this);
587
+ }
588
+ applyRemovalPolicy(policy) {
589
+ this._lambdaConstruct.applyRemovalPolicy(policy);
415
590
  this._queue.applyRemovalPolicy(policy);
416
591
  }
417
592
  // IQueue implementation
@@ -763,6 +938,7 @@ class JaypieTraceSigningKeySecret extends JaypieEnvSecret {
763
938
 
764
939
  exports.JaypieEnvSecret = JaypieEnvSecret;
765
940
  exports.JaypieHostedZone = JaypieHostedZone;
941
+ exports.JaypieLambda = JaypieLambda;
766
942
  exports.JaypieMongoDbSecret = JaypieMongoDbSecret;
767
943
  exports.JaypieOpenAiSecret = JaypieOpenAiSecret;
768
944
  exports.JaypieQueuedLambda = JaypieQueuedLambda;