@certenza/aws-cdk-infrastructure-commons 2.0.5 → 2.2.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.
@@ -23,11 +23,19 @@ type DynamoDBIndex = {
23
23
  * Creates a DynamoDB table
24
24
  * @param scope - The scope of the DynamoDB table
25
25
  * @param tableName - The name of the DynamoDB table
26
+ * @param environment - The environment of the DynamoDB table
26
27
  * @param keys - The keys of the DynamoDB table
27
28
  * @param indexes - The indexes of the DynamoDB table
28
29
  * @param options - The options of the DynamoDB table
29
30
  * @returns The DynamoDB table
30
31
  */
31
- declare const createDynamoDBTable: (scope: Construct, tableName: string, keys: DynamoDBKeys, indexes: DynamoDBIndex[], options?: Partial<dynamodb.TableProps>) => cdk.aws_dynamodb.Table;
32
- export { createDynamoDBTable };
32
+ declare const createDynamoDBTable: (scope: Construct, tableName: string, environment: string, keys: DynamoDBKeys, indexes: DynamoDBIndex[], options?: Partial<dynamodb.TableProps>) => cdk.aws_dynamodb.Table;
33
+ /**
34
+ * Import a DynamoDB table from the infrastructure dynamodb stack
35
+ * @param scope - The scope of the stack
36
+ * @param tableName - The name of the table to import
37
+ * @returns The imported DynamoDB table
38
+ */
39
+ declare const importDynamoDBTable: (scope: Construct, tableName: string) => cdk.aws_dynamodb.ITable;
40
+ export { createDynamoDBTable, importDynamoDBTable };
33
41
  export type { DynamoDBIndex, DynamoDBKeys };
@@ -33,19 +33,20 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.createDynamoDBTable = void 0;
36
+ exports.importDynamoDBTable = exports.createDynamoDBTable = void 0;
37
37
  const cdk = __importStar(require("aws-cdk-lib"));
38
38
  const dynamodb = __importStar(require("aws-cdk-lib/aws-dynamodb"));
39
39
  /**
40
40
  * Creates a DynamoDB table
41
41
  * @param scope - The scope of the DynamoDB table
42
42
  * @param tableName - The name of the DynamoDB table
43
+ * @param environment - The environment of the DynamoDB table
43
44
  * @param keys - The keys of the DynamoDB table
44
45
  * @param indexes - The indexes of the DynamoDB table
45
46
  * @param options - The options of the DynamoDB table
46
47
  * @returns The DynamoDB table
47
48
  */
48
- const createDynamoDBTable = (scope, tableName, keys, indexes, options = {}) => {
49
+ const createDynamoDBTable = (scope, tableName, environment, keys, indexes, options = {}) => {
49
50
  // Create the table props
50
51
  let tableProps = {
51
52
  tableName,
@@ -54,7 +55,10 @@ const createDynamoDBTable = (scope, tableName, keys, indexes, options = {}) => {
54
55
  type: dynamodb.AttributeType.STRING,
55
56
  },
56
57
  billingMode: dynamodb.BillingMode.PAY_PER_REQUEST, // On-demand
57
- removalPolicy: cdk.RemovalPolicy.DESTROY, // For testing; use RETAIN in production
58
+ removalPolicy: environment === "production"
59
+ ? cdk.RemovalPolicy.RETAIN
60
+ : cdk.RemovalPolicy.DESTROY,
61
+ deletionProtection: environment === "production",
58
62
  ...options,
59
63
  };
60
64
  // If a sort key is provided, add it to the table props
@@ -97,3 +101,17 @@ const createDynamoDBTable = (scope, tableName, keys, indexes, options = {}) => {
97
101
  return table;
98
102
  };
99
103
  exports.createDynamoDBTable = createDynamoDBTable;
104
+ /**
105
+ * Import a DynamoDB table from the infrastructure dynamodb stack
106
+ * @param scope - The scope of the stack
107
+ * @param tableName - The name of the table to import
108
+ * @returns The imported DynamoDB table
109
+ */
110
+ const importDynamoDBTable = (scope, tableName) => {
111
+ return cdk.aws_dynamodb.Table.fromTableAttributes(scope, `Imported${tableName}Table`, {
112
+ tableName: cdk.Fn.importValue(`CertenzaInfrastructureDynamoDBStack-${tableName}TableName`),
113
+ tableArn: cdk.Fn.importValue(`CertenzaInfrastructureDynamoDBStack-${tableName}TableArn`),
114
+ tableStreamArn: cdk.Fn.importValue(`CertenzaInfrastructureDynamoDBStack-${tableName}TableStreamArn`),
115
+ });
116
+ };
117
+ exports.importDynamoDBTable = importDynamoDBTable;
@@ -31,5 +31,17 @@ declare const createLambdaFunction: (scope: Construct, functionName: string, env
31
31
  * @returns The lambda integration
32
32
  */
33
33
  declare const createLambdaApiGatewayIntegration: (lambdaFunction: lambda.Function) => cdk.aws_apigateway.LambdaIntegration;
34
- export { createLambdaFunction, createLambdaApiGatewayIntegration };
34
+ /**
35
+ * Creates a lambda DynamoDB stream event source
36
+ * @param table - The table to create the event source for
37
+ * @returns The lambda DynamoDB stream event source
38
+ */
39
+ declare const createLambdaDynamoDBStreamEventSource: (table: cdk.aws_dynamodb.Table) => cdk.aws_lambda_event_sources.DynamoEventSource;
40
+ /**
41
+ * Creates a lambda SQS event source
42
+ * @param queue - The queue to create the event source for
43
+ * @returns The lambda SQS event source
44
+ */
45
+ declare const createLambdaSQSEventSource: (queue: cdk.aws_sqs.Queue) => cdk.aws_lambda_event_sources.SqsEventSource;
46
+ export { createLambdaFunction, createLambdaApiGatewayIntegration, createLambdaDynamoDBStreamEventSource, createLambdaSQSEventSource, };
35
47
  export type { CreateLambdaFunctionProps };
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.createLambdaApiGatewayIntegration = exports.createLambdaFunction = void 0;
36
+ exports.createLambdaSQSEventSource = exports.createLambdaDynamoDBStreamEventSource = exports.createLambdaApiGatewayIntegration = exports.createLambdaFunction = void 0;
37
37
  /**
38
38
  * Lambda functions and integrations
39
39
  */
@@ -119,3 +119,29 @@ const createLambdaApiGatewayIntegration = (lambdaFunction) => {
119
119
  });
120
120
  };
121
121
  exports.createLambdaApiGatewayIntegration = createLambdaApiGatewayIntegration;
122
+ /**
123
+ * Creates a lambda DynamoDB stream event source
124
+ * @param table - The table to create the event source for
125
+ * @returns The lambda DynamoDB stream event source
126
+ */
127
+ const createLambdaDynamoDBStreamEventSource = (table) => {
128
+ return new cdk.aws_lambda_event_sources.DynamoEventSource(table, {
129
+ startingPosition: cdk.aws_lambda.StartingPosition.LATEST,
130
+ batchSize: 1,
131
+ retryAttempts: -1, // Retry indefinitely
132
+ maxRecordAge: cdk.Duration.days(7), // Keep records for 7 days
133
+ });
134
+ };
135
+ exports.createLambdaDynamoDBStreamEventSource = createLambdaDynamoDBStreamEventSource;
136
+ /**
137
+ * Creates a lambda SQS event source
138
+ * @param queue - The queue to create the event source for
139
+ * @returns The lambda SQS event source
140
+ */
141
+ const createLambdaSQSEventSource = (queue) => {
142
+ return new cdk.aws_lambda_event_sources.SqsEventSource(queue, {
143
+ batchSize: 1,
144
+ maxConcurrency: 10,
145
+ });
146
+ };
147
+ exports.createLambdaSQSEventSource = createLambdaSQSEventSource;
package/dist/src/sqs.d.ts CHANGED
@@ -20,4 +20,15 @@ declare const createSqsQueue: (scope: Construct, id: string, props: Partial<sqs.
20
20
  * @returns The SQS API Gateway integration
21
21
  */
22
22
  declare const createSQSApiGatewayIntegration: (scope: Construct, queue: sqs.Queue) => apigateway.AwsIntegration;
23
- export { createSqsQueue, createSQSApiGatewayIntegration };
23
+ /**
24
+ * Creates an EventBridge rule to suscribe an SQS queue to an EventBridge event bus
25
+ * @param scope - The scope of the construct
26
+ * @param id - The id of the rule
27
+ * @param eventBus - The event bus to suscribe to
28
+ * @param suscriberQueue - The queue to suscribe to the event bus
29
+ * @param eventBridgeDeadLetterQueue - The dead letter queue for the event bus
30
+ * @param eventPattern - The event pattern to suscribe to
31
+ * @returns The event bridge rule
32
+ */
33
+ declare const createSQSToEventBridgeRule: (scope: Construct, id: string, eventBus: cdk.aws_events.EventBus, suscriberQueue: sqs.Queue, eventBridgeDeadLetterQueue: sqs.Queue, eventPattern: cdk.aws_events.EventPattern) => cdk.aws_events.Rule;
34
+ export { createSqsQueue, createSQSApiGatewayIntegration, createSQSToEventBridgeRule, };
package/dist/src/sqs.js CHANGED
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.createSQSApiGatewayIntegration = exports.createSqsQueue = void 0;
36
+ exports.createSQSToEventBridgeRule = exports.createSQSApiGatewayIntegration = exports.createSqsQueue = void 0;
37
37
  /**
38
38
  * Library to create SQS queues
39
39
  */
@@ -111,3 +111,26 @@ const createSQSApiGatewayIntegration = (scope, queue) => {
111
111
  });
112
112
  };
113
113
  exports.createSQSApiGatewayIntegration = createSQSApiGatewayIntegration;
114
+ /**
115
+ * Creates an EventBridge rule to suscribe an SQS queue to an EventBridge event bus
116
+ * @param scope - The scope of the construct
117
+ * @param id - The id of the rule
118
+ * @param eventBus - The event bus to suscribe to
119
+ * @param suscriberQueue - The queue to suscribe to the event bus
120
+ * @param eventBridgeDeadLetterQueue - The dead letter queue for the event bus
121
+ * @param eventPattern - The event pattern to suscribe to
122
+ * @returns The event bridge rule
123
+ */
124
+ const createSQSToEventBridgeRule = (scope, id, eventBus, suscriberQueue, eventBridgeDeadLetterQueue, eventPattern) => {
125
+ return new cdk.aws_events.Rule(scope, id, {
126
+ eventBus: eventBus,
127
+ eventPattern: eventPattern,
128
+ targets: [
129
+ new cdk.aws_events_targets.SqsQueue(suscriberQueue, {
130
+ retryAttempts: 5,
131
+ deadLetterQueue: eventBridgeDeadLetterQueue,
132
+ }),
133
+ ],
134
+ });
135
+ };
136
+ exports.createSQSToEventBridgeRule = createSQSToEventBridgeRule;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@certenza/aws-cdk-infrastructure-commons",
3
- "version": "2.0.5",
3
+ "version": "2.2.0",
4
4
  "description": "Common infrastructure reusable utilities and resources for Certenza projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",