@gradientedge/cdk-utils 8.56.0 → 8.58.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.
@@ -4,6 +4,9 @@ import * as iam from 'aws-cdk-lib/aws-iam';
4
4
  import * as lambda from 'aws-cdk-lib/aws-lambda';
5
5
  import * as common from '../../common';
6
6
  import * as types from '../../types';
7
+ import * as cdk from 'aws-cdk-lib';
8
+ import * as sqs from 'aws-cdk-lib/aws-sqs';
9
+ import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
7
10
  /**
8
11
  * @stability stable
9
12
  * @category cdk-utils.event-manager
@@ -64,4 +67,13 @@ export declare class EventManager {
64
67
  * @param {any} eventPattern
65
68
  */
66
69
  createFargateTaskRule(id: string, scope: common.CommonConstruct, props: types.RuleProps, cluster: ecs.ICluster, task: ecs.ITaskDefinition, subnetIds: string[], role: iam.Role | iam.CfnRole, eventPattern?: any): events.CfnRule;
70
+ /**
71
+ * @summary Method to create an eventbridge pipe with sqs queue as source and step function as target
72
+ * @param {string} id scoped id of the resource
73
+ * @param {common.CommonConstruct} scope scope in which this resource is defined
74
+ * @param props the props for the pipe
75
+ * @param sourceQueue the source sqs queue
76
+ * @param targetStepFunction the target step function
77
+ */
78
+ createSqsToSfnCfnPipe(id: string, scope: common.CommonConstruct, props: types.SqsToSfnPipeProps, sourceQueue: sqs.IQueue, targetStepFunction: sfn.IStateMachine): cdk.aws_pipes.CfnPipe;
67
79
  }
@@ -29,6 +29,7 @@ const iam = __importStar(require("aws-cdk-lib/aws-iam"));
29
29
  const lambda = __importStar(require("aws-cdk-lib/aws-lambda"));
30
30
  const utils = __importStar(require("../../utils"));
31
31
  const cdk = __importStar(require("aws-cdk-lib"));
32
+ const pipes = __importStar(require("aws-cdk-lib/aws-pipes"));
32
33
  /**
33
34
  * @stability stable
34
35
  * @category cdk-utils.event-manager
@@ -170,5 +171,50 @@ class EventManager {
170
171
  utils.createCfnOutput(`${id}-ruleName`, scope, eventRule.name);
171
172
  return eventRule;
172
173
  }
174
+ /**
175
+ * @summary Method to create an eventbridge pipe with sqs queue as source and step function as target
176
+ * @param {string} id scoped id of the resource
177
+ * @param {common.CommonConstruct} scope scope in which this resource is defined
178
+ * @param props the props for the pipe
179
+ * @param sourceQueue the source sqs queue
180
+ * @param targetStepFunction the target step function
181
+ */
182
+ createSqsToSfnCfnPipe(id, scope, props, sourceQueue, targetStepFunction) {
183
+ const pipeRole = scope.iamManager.createRoleForSqsToSfnPipe(`${id}-role`, scope, sourceQueue.queueArn, targetStepFunction.stateMachineArn);
184
+ const pipe = new pipes.CfnPipe(scope, `${id}`, {
185
+ ...props,
186
+ name: `${props.name}-${scope.props.stage}`,
187
+ description: '',
188
+ source: sourceQueue.queueArn,
189
+ sourceParameters: {
190
+ filterCriteria: props.pipeFilterPattern
191
+ ? {
192
+ filters: [
193
+ {
194
+ pattern: JSON.stringify(props.pipeFilterPattern),
195
+ },
196
+ ],
197
+ }
198
+ : undefined,
199
+ sqsQueueParameters: {
200
+ batchSize: props.sqsBatchSize,
201
+ maximumBatchingWindowInSeconds: props.sqsMaximumBatchingWindowInSeconds,
202
+ },
203
+ },
204
+ target: targetStepFunction.stateMachineArn,
205
+ targetParameters: {
206
+ inputTemplate: props.sfnInputTemplate,
207
+ stepFunctionStateMachineParameters: {
208
+ invocationType: props.sfnInvocationType ?? 'FIRE_AND_FORGET',
209
+ },
210
+ },
211
+ enrichment: props.enrichment,
212
+ enrichmentParameters: props.enrichmentParameters,
213
+ roleArn: pipeRole.roleArn,
214
+ });
215
+ utils.createCfnOutput(`${id}-pipeArn`, scope, pipe.attrArn);
216
+ utils.createCfnOutput(`${id}-pipeName`, scope, pipe.name);
217
+ return pipe;
218
+ }
173
219
  }
174
220
  exports.EventManager = EventManager;
@@ -38,6 +38,16 @@ export declare class IamManager {
38
38
  * @param {string[]} resourceArns list of ARNs to allow access to
39
39
  */
40
40
  statementForPutEvents(resourceArns?: string[]): cdk.aws_iam.PolicyStatement;
41
+ /**
42
+ * @summary Method to create iam statement to start stepfunction execution
43
+ * @param {string[]} resourceArns list of ARNs to allow access to
44
+ */
45
+ statementForStartExecution(resourceArns?: string[]): cdk.aws_iam.PolicyStatement;
46
+ /**
47
+ * @summary Method to create iam statement to poll queue
48
+ * @param {string[]} resourceArns list of ARNs to allow access to
49
+ */
50
+ statementForPollQueue(resourceArns?: string[]): cdk.aws_iam.PolicyStatement;
41
51
  /**
42
52
  * @summary Method to create iam statement to invoke lambda function
43
53
  * @param {string[]} resourceArns list of ARNs to allow access to
@@ -189,6 +199,14 @@ export declare class IamManager {
189
199
  * @param {iam.ServicePrincipal} servicePrinicpal
190
200
  */
191
201
  createRoleForStepFunction(id: string, scope: common.CommonConstruct, policy: iam.PolicyDocument, servicePrinicpal?: iam.ServicePrincipal): cdk.aws_iam.Role;
202
+ /**
203
+ * @summary Method to create iam statement for sqs to step function pipe
204
+ * @param {string} id scoped id of the resource
205
+ * @param {common.CommonConstruct} scope scope in which this resource is defined
206
+ * @param {string} queueArn the arn of the sqs queue
207
+ * @param {string} stepFunctionArn the arn of the step function
208
+ */
209
+ createRoleForSqsToSfnPipe(id: string, scope: common.CommonConstruct, queueArn: string, stepFunctionArn: string): cdk.aws_iam.Role;
192
210
  /**
193
211
  * @summary Method to create iam policy for sqs
194
212
  * @param {string} id scoped id of the resource
@@ -73,6 +73,28 @@ class IamManager {
73
73
  resources: resourceArns ?? ['*'],
74
74
  });
75
75
  }
76
+ /**
77
+ * @summary Method to create iam statement to start stepfunction execution
78
+ * @param {string[]} resourceArns list of ARNs to allow access to
79
+ */
80
+ statementForStartExecution(resourceArns) {
81
+ return new iam.PolicyStatement({
82
+ effect: iam.Effect.ALLOW,
83
+ actions: ['states:StartExecution'],
84
+ resources: resourceArns ?? ['*'],
85
+ });
86
+ }
87
+ /**
88
+ * @summary Method to create iam statement to poll queue
89
+ * @param {string[]} resourceArns list of ARNs to allow access to
90
+ */
91
+ statementForPollQueue(resourceArns) {
92
+ return new iam.PolicyStatement({
93
+ effect: iam.Effect.ALLOW,
94
+ actions: ['sqs:ReceiveMessage', 'sqs:DeleteMessage', 'sqs:GetQueueAttributes'],
95
+ resources: resourceArns ?? ['*'],
96
+ });
97
+ }
76
98
  /**
77
99
  * @summary Method to create iam statement to invoke lambda function
78
100
  * @param {string[]} resourceArns list of ARNs to allow access to
@@ -458,6 +480,25 @@ class IamManager {
458
480
  utils.createCfnOutput(`${id}Name`, scope, role.roleName);
459
481
  return role;
460
482
  }
483
+ /**
484
+ * @summary Method to create iam statement for sqs to step function pipe
485
+ * @param {string} id scoped id of the resource
486
+ * @param {common.CommonConstruct} scope scope in which this resource is defined
487
+ * @param {string} queueArn the arn of the sqs queue
488
+ * @param {string} stepFunctionArn the arn of the step function
489
+ */
490
+ createRoleForSqsToSfnPipe(id, scope, queueArn, stepFunctionArn) {
491
+ const role = new iam.Role(scope, `${id}`, {
492
+ assumedBy: new iam.ServicePrincipal('pipes.amazonaws.com'),
493
+ description: `Role for ${id} Pipe`,
494
+ roleName: `${id}-${scope.props.stage}`,
495
+ });
496
+ role.addToPolicy(this.statementForPollQueue([queueArn]));
497
+ role.addToPolicy(this.statementForStartExecution([stepFunctionArn]));
498
+ utils.createCfnOutput(`${id}Arn`, scope, role.roleArn);
499
+ utils.createCfnOutput(`${id}Name`, scope, role.roleName);
500
+ return role;
501
+ }
461
502
  /**
462
503
  * @summary Method to create iam policy for sqs
463
504
  * @param {string} id scoped id of the resource
@@ -128,7 +128,8 @@ class LambdaManager {
128
128
  });
129
129
  if (props.lambdaAliases && props.lambdaAliases.length > 0) {
130
130
  props.lambdaAliases.forEach(alias => {
131
- const functionAlias = this.createLambdaFunctionAlias(`${id}-${alias.aliasName}`, scope, alias, lambdaFunction.currentVersion);
131
+ const aliasId = alias.id ?? `${id}-${alias.aliasName}`;
132
+ const functionAlias = this.createLambdaFunctionAlias(`${aliasId}`, scope, alias, lambdaFunction.currentVersion);
132
133
  if (alias.provisionedConcurrency) {
133
134
  const functionAutoScaling = functionAlias.addAutoScaling(alias.provisionedConcurrency);
134
135
  functionAutoScaling.scaleOnUtilization({
@@ -29,6 +29,7 @@ import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';
29
29
  import * as wafv2 from 'aws-cdk-lib/aws-wafv2';
30
30
  import * as types from '../index';
31
31
  import * as appAutoscaling from 'aws-cdk-lib/aws-applicationautoscaling';
32
+ import * as pipes from 'aws-cdk-lib/aws-pipes';
32
33
  /**
33
34
  * @category cdk-utils.app-config-manager
34
35
  * @subcategory Properties
@@ -660,6 +661,17 @@ export interface EksClusterProps extends eks.ClusterProps {
660
661
  export interface RuleProps extends events.CfnRuleProps {
661
662
  input?: string;
662
663
  }
664
+ /**
665
+ * @category cdk-utils.event-manager
666
+ * @subcategory Properties
667
+ */
668
+ export interface SqsToSfnPipeProps extends pipes.CfnPipeProps {
669
+ pipeFilterPattern?: any;
670
+ sqsBatchSize?: number;
671
+ sqsMaximumBatchingWindowInSeconds?: number;
672
+ sfnInvocationType?: string;
673
+ sfnInputTemplate?: string;
674
+ }
663
675
  /**
664
676
  * @category cdk-utils.event-manager
665
677
  * @subcategory Properties
@@ -707,6 +719,7 @@ export interface LambdaProps extends lambda.FunctionProps {
707
719
  * @subcategory Properties
708
720
  */
709
721
  export interface LambdaAliasProps extends lambda.AliasProps {
722
+ id?: string;
710
723
  provisionedConcurrency?: ProvisionedConcurrencyProps;
711
724
  }
712
725
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradientedge/cdk-utils",
3
- "version": "8.56.0",
3
+ "version": "8.58.0",
4
4
  "description": "Utilities for AWS CDK provisioning",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -6,6 +6,9 @@ import * as common from '../../common'
6
6
  import * as types from '../../types'
7
7
  import * as utils from '../../utils'
8
8
  import * as cdk from 'aws-cdk-lib'
9
+ import * as pipes from 'aws-cdk-lib/aws-pipes'
10
+ import * as sqs from 'aws-cdk-lib/aws-sqs'
11
+ import * as sfn from 'aws-cdk-lib/aws-stepfunctions'
9
12
 
10
13
  /**
11
14
  * @stability stable
@@ -185,4 +188,64 @@ export class EventManager {
185
188
 
186
189
  return eventRule
187
190
  }
191
+
192
+ /**
193
+ * @summary Method to create an eventbridge pipe with sqs queue as source and step function as target
194
+ * @param {string} id scoped id of the resource
195
+ * @param {common.CommonConstruct} scope scope in which this resource is defined
196
+ * @param props the props for the pipe
197
+ * @param sourceQueue the source sqs queue
198
+ * @param targetStepFunction the target step function
199
+ */
200
+ public createSqsToSfnCfnPipe(
201
+ id: string,
202
+ scope: common.CommonConstruct,
203
+ props: types.SqsToSfnPipeProps,
204
+ sourceQueue: sqs.IQueue,
205
+ targetStepFunction: sfn.IStateMachine
206
+ ) {
207
+ const pipeRole = scope.iamManager.createRoleForSqsToSfnPipe(
208
+ `${id}-role`,
209
+ scope,
210
+ sourceQueue.queueArn,
211
+ targetStepFunction.stateMachineArn
212
+ )
213
+
214
+ const pipe = new pipes.CfnPipe(scope, `${id}`, {
215
+ ...props,
216
+ name: `${props.name}-${scope.props.stage}`,
217
+ description: '',
218
+ source: sourceQueue.queueArn,
219
+ sourceParameters: {
220
+ filterCriteria: props.pipeFilterPattern
221
+ ? {
222
+ filters: [
223
+ {
224
+ pattern: JSON.stringify(props.pipeFilterPattern),
225
+ },
226
+ ],
227
+ }
228
+ : undefined,
229
+ sqsQueueParameters: {
230
+ batchSize: props.sqsBatchSize,
231
+ maximumBatchingWindowInSeconds: props.sqsMaximumBatchingWindowInSeconds,
232
+ },
233
+ },
234
+ target: targetStepFunction.stateMachineArn,
235
+ targetParameters: {
236
+ inputTemplate: props.sfnInputTemplate,
237
+ stepFunctionStateMachineParameters: {
238
+ invocationType: props.sfnInvocationType ?? 'FIRE_AND_FORGET',
239
+ },
240
+ },
241
+ enrichment: props.enrichment,
242
+ enrichmentParameters: props.enrichmentParameters,
243
+ roleArn: pipeRole.roleArn,
244
+ })
245
+
246
+ utils.createCfnOutput(`${id}-pipeArn`, scope, pipe.attrArn)
247
+ utils.createCfnOutput(`${id}-pipeName`, scope, pipe.name)
248
+
249
+ return pipe
250
+ }
188
251
  }
@@ -56,6 +56,30 @@ export class IamManager {
56
56
  })
57
57
  }
58
58
 
59
+ /**
60
+ * @summary Method to create iam statement to start stepfunction execution
61
+ * @param {string[]} resourceArns list of ARNs to allow access to
62
+ */
63
+ public statementForStartExecution(resourceArns?: string[]) {
64
+ return new iam.PolicyStatement({
65
+ effect: iam.Effect.ALLOW,
66
+ actions: ['states:StartExecution'],
67
+ resources: resourceArns ?? ['*'],
68
+ })
69
+ }
70
+
71
+ /**
72
+ * @summary Method to create iam statement to poll queue
73
+ * @param {string[]} resourceArns list of ARNs to allow access to
74
+ */
75
+ public statementForPollQueue(resourceArns?: string[]) {
76
+ return new iam.PolicyStatement({
77
+ effect: iam.Effect.ALLOW,
78
+ actions: ['sqs:ReceiveMessage', 'sqs:DeleteMessage', 'sqs:GetQueueAttributes'],
79
+ resources: resourceArns ?? ['*'],
80
+ })
81
+ }
82
+
59
83
  /**
60
84
  * @summary Method to create iam statement to invoke lambda function
61
85
  * @param {string[]} resourceArns list of ARNs to allow access to
@@ -508,6 +532,34 @@ export class IamManager {
508
532
  return role
509
533
  }
510
534
 
535
+ /**
536
+ * @summary Method to create iam statement for sqs to step function pipe
537
+ * @param {string} id scoped id of the resource
538
+ * @param {common.CommonConstruct} scope scope in which this resource is defined
539
+ * @param {string} queueArn the arn of the sqs queue
540
+ * @param {string} stepFunctionArn the arn of the step function
541
+ */
542
+ public createRoleForSqsToSfnPipe(
543
+ id: string,
544
+ scope: common.CommonConstruct,
545
+ queueArn: string,
546
+ stepFunctionArn: string
547
+ ) {
548
+ const role = new iam.Role(scope, `${id}`, {
549
+ assumedBy: new iam.ServicePrincipal('pipes.amazonaws.com'),
550
+ description: `Role for ${id} Pipe`,
551
+ roleName: `${id}-${scope.props.stage}`,
552
+ })
553
+
554
+ role.addToPolicy(this.statementForPollQueue([queueArn]))
555
+ role.addToPolicy(this.statementForStartExecution([stepFunctionArn]))
556
+
557
+ utils.createCfnOutput(`${id}Arn`, scope, role.roleArn)
558
+ utils.createCfnOutput(`${id}Name`, scope, role.roleName)
559
+
560
+ return role
561
+ }
562
+
511
563
  /**
512
564
  * @summary Method to create iam policy for sqs
513
565
  * @param {string} id scoped id of the resource
@@ -132,12 +132,8 @@ export class LambdaManager {
132
132
 
133
133
  if (props.lambdaAliases && props.lambdaAliases.length > 0) {
134
134
  props.lambdaAliases.forEach(alias => {
135
- const functionAlias = this.createLambdaFunctionAlias(
136
- `${id}-${alias.aliasName}`,
137
- scope,
138
- alias,
139
- lambdaFunction.currentVersion
140
- )
135
+ const aliasId = alias.id ?? `${id}-${alias.aliasName}`
136
+ const functionAlias = this.createLambdaFunctionAlias(`${aliasId}`, scope, alias, lambdaFunction.currentVersion)
141
137
 
142
138
  if (alias.provisionedConcurrency) {
143
139
  const functionAutoScaling = functionAlias.addAutoScaling(alias.provisionedConcurrency)
@@ -29,6 +29,7 @@ import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks'
29
29
  import * as wafv2 from 'aws-cdk-lib/aws-wafv2'
30
30
  import * as types from '../index'
31
31
  import * as appAutoscaling from 'aws-cdk-lib/aws-applicationautoscaling'
32
+ import * as pipes from 'aws-cdk-lib/aws-pipes'
32
33
 
33
34
  /**
34
35
  * @category cdk-utils.app-config-manager
@@ -706,6 +707,18 @@ export interface RuleProps extends events.CfnRuleProps {
706
707
  input?: string
707
708
  }
708
709
 
710
+ /**
711
+ * @category cdk-utils.event-manager
712
+ * @subcategory Properties
713
+ */
714
+ export interface SqsToSfnPipeProps extends pipes.CfnPipeProps {
715
+ pipeFilterPattern?: any
716
+ sqsBatchSize?: number
717
+ sqsMaximumBatchingWindowInSeconds?: number
718
+ sfnInvocationType?: string
719
+ sfnInputTemplate?: string
720
+ }
721
+
709
722
  /**
710
723
  * @category cdk-utils.event-manager
711
724
  * @subcategory Properties
@@ -757,6 +770,7 @@ export interface LambdaProps extends lambda.FunctionProps {
757
770
  * @subcategory Properties
758
771
  */
759
772
  export interface LambdaAliasProps extends lambda.AliasProps {
773
+ id?: string
760
774
  provisionedConcurrency?: ProvisionedConcurrencyProps
761
775
  }
762
776