@gradientedge/cdk-utils 8.153.0 → 8.155.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 (35) hide show
  1. package/dist/src/lib/aws/construct/event-handler/handler.d.ts +29 -0
  2. package/dist/src/lib/aws/construct/event-handler/handler.js +25 -0
  3. package/dist/src/lib/aws/construct/event-handler/index.d.ts +3 -0
  4. package/dist/src/lib/aws/construct/event-handler/index.js +19 -0
  5. package/dist/src/lib/aws/construct/event-handler/main.d.ts +91 -0
  6. package/dist/src/lib/aws/construct/event-handler/main.js +198 -0
  7. package/dist/src/lib/aws/construct/event-handler/types.d.ts +42 -0
  8. package/dist/src/lib/aws/construct/event-handler/types.js +2 -0
  9. package/dist/src/lib/aws/construct/index.d.ts +2 -0
  10. package/dist/src/lib/aws/construct/index.js +2 -0
  11. package/dist/src/lib/aws/construct/piped-event-handler/index.d.ts +2 -0
  12. package/dist/src/lib/aws/construct/piped-event-handler/index.js +18 -0
  13. package/dist/src/lib/aws/construct/piped-event-handler/main.d.ts +38 -0
  14. package/dist/src/lib/aws/construct/piped-event-handler/main.js +69 -0
  15. package/dist/src/lib/aws/construct/piped-event-handler/types.d.ts +9 -0
  16. package/dist/src/lib/aws/construct/piped-event-handler/types.js +2 -0
  17. package/dist/src/lib/aws/services/eventbridge/main.d.ts +10 -1
  18. package/dist/src/lib/aws/services/eventbridge/main.js +36 -0
  19. package/dist/src/lib/aws/services/eventbridge/types.d.ts +7 -1
  20. package/dist/src/lib/aws/services/identity-access-management/main.d.ts +8 -0
  21. package/dist/src/lib/aws/services/identity-access-management/main.js +19 -0
  22. package/dist/src/lib/aws/services/lambda/types.d.ts +4 -0
  23. package/package.json +18 -18
  24. package/src/lib/aws/construct/event-handler/handler.ts +30 -0
  25. package/src/lib/aws/construct/event-handler/index.ts +3 -0
  26. package/src/lib/aws/construct/event-handler/main.ts +243 -0
  27. package/src/lib/aws/construct/event-handler/types.ts +44 -0
  28. package/src/lib/aws/construct/index.ts +2 -0
  29. package/src/lib/aws/construct/piped-event-handler/index.ts +2 -0
  30. package/src/lib/aws/construct/piped-event-handler/main.ts +81 -0
  31. package/src/lib/aws/construct/piped-event-handler/types.ts +10 -0
  32. package/src/lib/aws/services/eventbridge/main.ts +59 -1
  33. package/src/lib/aws/services/eventbridge/types.ts +8 -1
  34. package/src/lib/aws/services/identity-access-management/main.ts +23 -0
  35. package/src/lib/aws/services/lambda/types.ts +5 -0
@@ -2,7 +2,6 @@ import { CfnPipeProps } from 'aws-cdk-lib/aws-pipes';
2
2
  import { CfnRuleProps, EventBusProps as EBProps, RuleProps as EBRuleProps } from 'aws-cdk-lib/aws-events';
3
3
  import { TagProps } from '../../types';
4
4
  /**
5
- }
6
5
  */
7
6
  export interface SqsToSfnPipeProps extends CfnPipeProps {
8
7
  pipeFilterPattern?: any;
@@ -11,6 +10,13 @@ export interface SqsToSfnPipeProps extends CfnPipeProps {
11
10
  sqsBatchSize?: number;
12
11
  sqsMaximumBatchingWindowInSeconds?: number;
13
12
  }
13
+ /**
14
+ */
15
+ export interface SqsToLambdaPipeProps extends CfnPipeProps {
16
+ pipeFilterPattern?: any;
17
+ sqsBatchSize?: number;
18
+ sqsMaximumBatchingWindowInSeconds?: number;
19
+ }
14
20
  /**
15
21
  */
16
22
  export interface EventRuleProps extends EBRuleProps {
@@ -235,6 +235,14 @@ export declare class IamManager {
235
235
  * @param stepFunctionArn the arn of the step function
236
236
  */
237
237
  createRoleForSqsToSfnPipe(id: string, scope: CommonConstruct, queueArn: string, stepFunctionArn: string): Role;
238
+ /**
239
+ * @summary Method to create iam statement for sqs to lambda pipe
240
+ * @param id scoped id of the resource
241
+ * @param scope scope in which this resource is defined
242
+ * @param queueArn the arn of the sqs queue
243
+ * @param lambdaArn the arn of the lambda function
244
+ */
245
+ createRoleForSqsToLambdaPipe(id: string, scope: CommonConstruct, queueArn: string, lambdaArn: string): Role;
238
246
  /**
239
247
  * @summary Method to create iam policy for sqs
240
248
  * @param id scoped id of the resource
@@ -545,6 +545,25 @@ class IamManager {
545
545
  (0, utils_1.createCfnOutput)(`${id}Name`, scope, role.roleName);
546
546
  return role;
547
547
  }
548
+ /**
549
+ * @summary Method to create iam statement for sqs to lambda pipe
550
+ * @param id scoped id of the resource
551
+ * @param scope scope in which this resource is defined
552
+ * @param queueArn the arn of the sqs queue
553
+ * @param lambdaArn the arn of the lambda function
554
+ */
555
+ createRoleForSqsToLambdaPipe(id, scope, queueArn, lambdaArn) {
556
+ const role = new aws_iam_1.Role(scope, `${id}`, {
557
+ assumedBy: new aws_iam_1.ServicePrincipal('pipes.amazonaws.com'),
558
+ description: `Role for ${id} Pipe`,
559
+ roleName: `${id}-${scope.props.stage}`,
560
+ });
561
+ role.addToPolicy(this.statementForPollQueue([queueArn]));
562
+ role.addToPolicy(this.statementForInvokeLambda([lambdaArn]));
563
+ (0, utils_1.createCfnOutput)(`${id}Arn`, scope, role.roleArn);
564
+ (0, utils_1.createCfnOutput)(`${id}Name`, scope, role.roleName);
565
+ return role;
566
+ }
548
567
  /**
549
568
  * @summary Method to create iam policy for sqs
550
569
  * @param id scoped id of the resource
@@ -2,6 +2,7 @@ import { AliasProps, FunctionProps } from 'aws-cdk-lib/aws-lambda';
2
2
  import { TagProps } from '../../types';
3
3
  import { EdgeFunctionProps } from 'aws-cdk-lib/aws-cloudfront/lib/experimental';
4
4
  import { QueueProps } from '../simple-queue-service';
5
+ import { SqsEventSourceProps } from 'aws-cdk-lib/aws-lambda-event-sources';
5
6
  /**
6
7
  */
7
8
  export interface ProvisionedConcurrencyProps {
@@ -41,3 +42,6 @@ export interface LambdaEnvironment {
41
42
  STAGE?: string;
42
43
  TZ: string;
43
44
  }
45
+ export interface SQSEventSourceProps extends SqsEventSourceProps {
46
+ maxBatchingWindowInSecs: number;
47
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradientedge/cdk-utils",
3
- "version": "8.153.0",
3
+ "version": "8.155.0",
4
4
  "description": "Utilities for AWS CDK provisioning",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -47,19 +47,19 @@
47
47
  }
48
48
  },
49
49
  "dependencies": {
50
- "@aws-sdk/client-secrets-manager": "^3.484.0",
51
- "@aws-sdk/credential-providers": "^3.484.0",
52
- "@aws-sdk/types": "^3.468.0",
53
- "@cdktf/provider-aws": "^18.2.0",
54
- "@cdktf/provider-azurerm": "^11.2.0",
55
- "@cdktf/provider-cloudflare": "^10.0.4",
50
+ "@aws-sdk/client-secrets-manager": "^3.496.0",
51
+ "@aws-sdk/credential-providers": "^3.496.0",
52
+ "@aws-sdk/types": "^3.496.0",
53
+ "@cdktf/provider-aws": "^19.2.0",
54
+ "@cdktf/provider-azurerm": "^12.2.0",
55
+ "@cdktf/provider-cloudflare": "^11.0.0",
56
56
  "@types/lodash": "^4.14.202",
57
- "@types/node": "^20.10.6",
57
+ "@types/node": "^20.11.5",
58
58
  "@types/uuid": "^9.0.7",
59
59
  "app-root-path": "^3.1.0",
60
- "aws-cdk-lib": "^2.117.0",
61
- "cdktf": "^0.19.2",
62
- "cdktf-local-exec": "^0.4.14",
60
+ "aws-cdk-lib": "^2.122.0",
61
+ "cdktf": "^0.20.1",
62
+ "cdktf-local-exec": "^0.5.2",
63
63
  "constructs": "^10.3.0",
64
64
  "lodash": "^4.17.21",
65
65
  "moment": "^2.30.1",
@@ -73,18 +73,18 @@
73
73
  "@babel/eslint-parser": "^7.23.3",
74
74
  "@babel/plugin-proposal-class-properties": "^7.18.6",
75
75
  "@types/jest": "^29.5.11",
76
- "@typescript-eslint/eslint-plugin": "^6.17.0",
77
- "@typescript-eslint/parser": "^6.17.0",
78
- "aws-cdk": "^2.117.0",
76
+ "@typescript-eslint/eslint-plugin": "^6.19.0",
77
+ "@typescript-eslint/parser": "^6.19.0",
78
+ "aws-cdk": "^2.122.0",
79
79
  "better-docs": "^2.7.3",
80
80
  "codecov": "^3.8.3",
81
81
  "commitizen": "^4.3.0",
82
82
  "docdash": "^2.0.2",
83
- "dotenv": "^16.3.1",
83
+ "dotenv": "^16.3.2",
84
84
  "eslint": "^8.56.0",
85
85
  "eslint-config-prettier": "^9.1.0",
86
86
  "eslint-plugin-import": "^2.29.1",
87
- "eslint-plugin-jsdoc": "^47.0.2",
87
+ "eslint-plugin-jsdoc": "^48.0.2",
88
88
  "husky": "^8.0.3",
89
89
  "jest": "^29.7.0",
90
90
  "jest-extended": "^4.0.2",
@@ -94,10 +94,10 @@
94
94
  "jsdoc-mermaid": "^1.0.0",
95
95
  "jsdoc-plugin-typescript": "^2.2.1",
96
96
  "jsdoc-to-markdown": "^8.0.0",
97
- "prettier": "^3.1.1",
97
+ "prettier": "^3.2.4",
98
98
  "prettier-plugin-organize-imports": "^3.2.4",
99
99
  "rimraf": "^5.0.5",
100
- "semantic-release": "^22.0.12",
100
+ "semantic-release": "^23.0.0",
101
101
  "taffydb": "^2.7.3",
102
102
  "ts-jest": "^29.1.1",
103
103
  "ts-node": "^10.9.2",
@@ -0,0 +1,30 @@
1
+ import { Archive, IEventBus, IRuleTarget, Rule } from 'aws-cdk-lib/aws-events'
2
+ import { CloudWatchLogGroup, EcsTask, LambdaFunction, SfnStateMachine, SqsQueue } from 'aws-cdk-lib/aws-events-targets'
3
+ import { PolicyDocument, Role } from 'aws-cdk-lib/aws-iam'
4
+ import { IFunction } from 'aws-cdk-lib/aws-lambda'
5
+ import { LogGroup } from 'aws-cdk-lib/aws-logs'
6
+ import { Queue } from 'aws-cdk-lib/aws-sqs'
7
+ import { Chain, Map, StateMachine } from 'aws-cdk-lib/aws-stepfunctions'
8
+ import { EventHandlerType } from './types'
9
+
10
+ export class Handler implements EventHandlerType {
11
+ archive: Archive
12
+ ecsTargets: EcsTask[] = []
13
+ eventBus: IEventBus
14
+ eventWorkflowDefinition: Chain
15
+ lambdaFunctions: IFunction[] = []
16
+ lambdaTargets: LambdaFunction[] = []
17
+ logTargets: CloudWatchLogGroup[] = []
18
+ queue: Queue
19
+ rule: Rule
20
+ rulePattern: any
21
+ sqsTargets: SqsQueue[] = []
22
+ stepFunctionTargets: SfnStateMachine[] = []
23
+ targets: IRuleTarget[] = []
24
+ workflow: StateMachine
25
+ workflowDefinition: Chain
26
+ workflowLogGroup: LogGroup
27
+ workflowMapState: Map
28
+ workflowPolicy: PolicyDocument
29
+ workflowRole: Role
30
+ }
@@ -0,0 +1,3 @@
1
+ export * from './handler'
2
+ export * from './main'
3
+ export * from './types'
@@ -0,0 +1,243 @@
1
+ import { Duration, Fn } from 'aws-cdk-lib'
2
+ import { ISecurityGroup, IVpc, SecurityGroup } from 'aws-cdk-lib/aws-ec2'
3
+ import { Archive, EventBus, Schedule } from 'aws-cdk-lib/aws-events'
4
+ import { SfnStateMachine, SqsQueue } from 'aws-cdk-lib/aws-events-targets'
5
+ import { CfnQueuePolicy } from 'aws-cdk-lib/aws-sqs'
6
+ import { JsonPath, Map } from 'aws-cdk-lib/aws-stepfunctions'
7
+ import { Construct } from 'constructs'
8
+ import _ from 'lodash'
9
+ import { CommonConstruct } from '../../common'
10
+ import { Handler } from './handler'
11
+ import { EventHandlerProps } from './types'
12
+
13
+ /**
14
+ * @classdesc Provides a construct to create and deploy an EventBridge Event Handler
15
+ * @example
16
+ * import { EventHandler, EventHandlerProps } '@gradientedge/cdk-utils'
17
+ * import { Construct } from 'constructs'
18
+ *
19
+ * class CustomConstruct extends EventHandler {
20
+ * constructor(parent: Construct, id: string, props: EventHandlerProps) {
21
+ * super(parent, id, props)
22
+ * this.props = props
23
+ * this.id = id
24
+ * this.initResources()
25
+ * }
26
+ * }
27
+ */
28
+ export class EventHandler extends CommonConstruct {
29
+ props: EventHandlerProps
30
+ id: string
31
+ handler: Handler
32
+ provisionTarget: boolean = true
33
+ securityGroup: ISecurityGroup
34
+ useMapState: boolean
35
+ vpc: IVpc
36
+
37
+ constructor(parent: Construct, id: string, props: EventHandlerProps) {
38
+ super(parent, id, props)
39
+ this.props = props
40
+ this.id = id
41
+ this.handler = new Handler()
42
+ }
43
+
44
+ protected initResources() {
45
+ this.createSQSEventSource()
46
+ this.createWorkflow()
47
+ this.createEventRulePattern()
48
+ this.createEventRuleTargets()
49
+ this.resolveEventBus()
50
+ this.createEventArchive()
51
+ this.createEventRule()
52
+ }
53
+
54
+ /**
55
+ * @summary Method to resolve common vpc or create a new one.
56
+ */
57
+ protected resolveVpc() {
58
+ if (this.props.useExistingVpc) {
59
+ this.vpc = this.vpcManager.retrieveCommonVpc(`${this.id}-vpc`, this, this.props.vpcName)
60
+ } else {
61
+ this.vpc = this.vpcManager.createCommonVpc(this, this.props.vpc, this.props.vpc.vpcName)
62
+ }
63
+ }
64
+
65
+ /**
66
+ * @summary Method to resolve the exported security group.
67
+ */
68
+ protected resolveSecurityGroup() {
69
+ if (this.props.securityGroupExportName) {
70
+ this.securityGroup = SecurityGroup.fromSecurityGroupId(
71
+ this,
72
+ `${this.id}-security-group`,
73
+ Fn.importValue(this.props.securityGroupExportName)
74
+ )
75
+ }
76
+ }
77
+
78
+ /**
79
+ * @summary Method to create sqs event source if queue targets are defined.
80
+ */
81
+ protected createSQSEventSource() {
82
+ if (!this.props.eventSqs) return
83
+
84
+ this.handler.queue = this.sqsManager.createQueue(`${this.id}-sqs-queue`, this, this.props.eventSqs)
85
+ const sqsPolicyDocument = this.iamManager.createPolicyForSqsEvent(
86
+ `${this.id}-sqs-policy-document`,
87
+ this,
88
+ this.handler.queue,
89
+ this.handler.rule
90
+ )
91
+ new CfnQueuePolicy(this, `${this.id}-sqs-queue-policy`, {
92
+ policyDocument: sqsPolicyDocument.toJSON(),
93
+ queues: [this.handler.queue.queueUrl],
94
+ })
95
+ this.handler.sqsTargets = [new SqsQueue(this.handler.queue)]
96
+ }
97
+
98
+ /**
99
+ * @summary Method to create the event rule pattern.
100
+ */
101
+ protected createEventRulePattern() {
102
+ this.handler.rulePattern = this.props.eventRule.eventPattern
103
+ }
104
+
105
+ /**
106
+ * @summary Method to create the event rule targets.
107
+ */
108
+ protected createEventRuleTargets() {
109
+ this.handler.targets = [
110
+ ...this.handler.stepFunctionTargets,
111
+ ...this.handler.lambdaTargets,
112
+ ...this.handler.sqsTargets,
113
+ ...this.handler.ecsTargets,
114
+ ...this.handler.logTargets,
115
+ ]
116
+ }
117
+
118
+ /**
119
+ * @summary Method to resolve the event bus name or use the default bus.
120
+ */
121
+ protected resolveEventBus() {
122
+ this.handler.eventBus = EventBus.fromEventBusName(this, `${this.id}-bus`, this.props.eventBusName ?? 'default')
123
+ }
124
+
125
+ /**
126
+ * @summary Method to create an event archive if the event rule is not a scheduled one.
127
+ */
128
+ protected createEventArchive() {
129
+ /* do not enable for scheduled events */
130
+ if (this.props.eventRule.schedule || this.props.eventRuleSchedule || !this.props.eventRuleArchiveEnabled) return
131
+ this.handler.archive = new Archive(this, `${this.id}-archive`, {
132
+ archiveName: `${this.props.eventRule.ruleName}-${this.props.stage}`.replace(
133
+ `${this.node.tryGetContext('stackName')}-`,
134
+ ''
135
+ ),
136
+ description: `Archive of events for ${this.props.eventRule.ruleName}`,
137
+ eventPattern: this.handler.rulePattern,
138
+ retention: Duration.days(this.props.eventRetentionInDays ?? 7),
139
+ sourceEventBus: this.handler.eventBus,
140
+ })
141
+ }
142
+
143
+ /**
144
+ * @summary Method to create the event rule.
145
+ */
146
+ protected createEventRule() {
147
+ let schedule
148
+ if (this.props.eventRuleSchedule) {
149
+ schedule = Schedule.expression(this.props.eventRuleSchedule)
150
+ }
151
+ this.handler.rule = this.eventManager.createRule(
152
+ `${this.id}-rule`,
153
+ this,
154
+ {
155
+ ...this.props.eventRule,
156
+ eventPattern: this.handler.rulePattern,
157
+ schedule: schedule,
158
+ },
159
+ this.props.eventBusName ? this.handler.eventBus : undefined,
160
+ this.handler.targets
161
+ )
162
+ }
163
+
164
+ /**
165
+ * @summary Method to create the workflow steps.
166
+ */
167
+ protected createWorkflowSteps() {}
168
+
169
+ /**
170
+ * @summary Method to create the workflow definition.
171
+ */
172
+ protected createWorkflowDefinition() {
173
+ if (this.useMapState) {
174
+ this.handler.workflowMapState = new Map(this, `Map Iterator`, {
175
+ ...this.props.workflowMapState,
176
+ itemsPath: JsonPath.entirePayload,
177
+ })
178
+ }
179
+
180
+ this.handler.workflowDefinition = this.handler.eventWorkflowDefinition
181
+ if (this.useMapState) {
182
+ this.handler.workflowMapState.itemProcessor(this.handler.workflowDefinition)
183
+ }
184
+ }
185
+
186
+ /**
187
+ * @summary Method to create the workflow policy.
188
+ */
189
+ protected createWorkflowPolicy() {}
190
+
191
+ /**
192
+ * @summary Method to create the workflow role.
193
+ */
194
+ protected createWorkflowRole() {
195
+ this.handler.workflowRole = this.iamManager.createRoleForStepFunction(
196
+ `${this.id}-workflow-role`,
197
+ this,
198
+ this.handler.workflowPolicy
199
+ )
200
+ }
201
+
202
+ /**
203
+ * @summary Method to create the workflow log group.
204
+ */
205
+ protected createWorkflowLogGroup() {
206
+ this.handler.workflowLogGroup = this.logManager.createLogGroup(
207
+ `${this.id}-workflow-log`,
208
+ this,
209
+ this.props.workflowLog
210
+ )
211
+ }
212
+
213
+ /**
214
+ * @summary Method to create the workflow state machine.
215
+ */
216
+ protected createWorkflowStateMachine() {
217
+ this.handler.workflow = this.sfnManager.createStateMachine(
218
+ `${this.id}-workflow`,
219
+ this,
220
+ this.props.workflow,
221
+ this.useMapState ? this.handler.workflowMapState : this.handler.workflowDefinition,
222
+ this.handler.workflowLogGroup,
223
+ this.handler.workflowRole
224
+ )
225
+ }
226
+
227
+ /**
228
+ * @summary Method to create the workflow.
229
+ */
230
+ protected createWorkflow() {
231
+ if (_.isEmpty(this.props.workflow)) return
232
+
233
+ this.createWorkflowSteps()
234
+ this.createWorkflowDefinition()
235
+ this.createWorkflowPolicy()
236
+ this.createWorkflowRole()
237
+ this.createWorkflowLogGroup()
238
+ this.createWorkflowStateMachine()
239
+ if (this.provisionTarget) {
240
+ this.handler.stepFunctionTargets = [new SfnStateMachine(this.handler.workflow)]
241
+ }
242
+ }
243
+ }
@@ -0,0 +1,44 @@
1
+ import { Archive, IEventBus, IRuleTarget, Rule } from 'aws-cdk-lib/aws-events'
2
+ import { CloudWatchLogGroup, EcsTask, LambdaFunction, SfnStateMachine, SqsQueue } from 'aws-cdk-lib/aws-events-targets'
3
+ import { PolicyDocument, Role } from 'aws-cdk-lib/aws-iam'
4
+ import { LogGroup } from 'aws-cdk-lib/aws-logs'
5
+ import { Queue } from 'aws-cdk-lib/aws-sqs'
6
+ import { Chain, Map, StateMachine } from 'aws-cdk-lib/aws-stepfunctions'
7
+ import { CommonStackProps } from '../../common'
8
+ import { EventRuleProps, LogProps, QueueProps, SfnMapProps, SfnStateMachineProps, VpcProps } from '../../services'
9
+
10
+ export interface EventHandlerProps extends CommonStackProps {
11
+ eventBusName: string
12
+ eventRetentionInDays: number
13
+ eventRule: EventRuleProps
14
+ eventRuleArchiveEnabled: boolean
15
+ eventRuleSchedule: string
16
+ eventSqs: QueueProps
17
+ securityGroupExportName: string
18
+ useExistingVpc: boolean
19
+ vpc: VpcProps
20
+ vpcName: string
21
+ workflow: SfnStateMachineProps
22
+ workflowLog: LogProps
23
+ workflowMapState: SfnMapProps
24
+ }
25
+
26
+ export interface EventHandlerType {
27
+ archive: Archive
28
+ ecsTargets: EcsTask[]
29
+ eventBus: IEventBus
30
+ lambdaTargets: LambdaFunction[]
31
+ logTargets: CloudWatchLogGroup[]
32
+ queue: Queue
33
+ rule: Rule
34
+ rulePattern: any
35
+ sqsTargets: SqsQueue[]
36
+ stepFunctionTargets: SfnStateMachine[]
37
+ targets: IRuleTarget[]
38
+ workflow: StateMachine
39
+ workflowDefinition: Chain
40
+ workflowLogGroup: LogGroup
41
+ workflowMapState: Map
42
+ workflowPolicy: PolicyDocument
43
+ workflowRole: Role
44
+ }
@@ -3,9 +3,11 @@ export * from './api-to-eventbridge-target'
3
3
  export * from './api-to-eventbridge-target-with-sns'
4
4
  export * from './api-to-lambda-target'
5
5
  export * from './application-configuration'
6
+ export * from './event-handler'
6
7
  export * from './graphql-api-lambda'
7
8
  export * from './graphql-api-lambda-with-cache'
8
9
  export * from './lambda-with-iam-access'
10
+ export * from './piped-event-handler'
9
11
  export * from './rest-api-lambda'
10
12
  export * from './rest-api-lambda-with-cache'
11
13
  export * from './site-with-ecs-backend'
@@ -0,0 +1,2 @@
1
+ export * from './main'
2
+ export * from './types'
@@ -0,0 +1,81 @@
1
+ import { SqsQueue } from 'aws-cdk-lib/aws-events-targets'
2
+ import { IQueue } from 'aws-cdk-lib/aws-sqs'
3
+ import { Construct } from 'constructs'
4
+ import _ from 'lodash'
5
+ import { EventHandler } from '../event-handler'
6
+ import { PipedEventHandlerProps } from './types'
7
+
8
+ /**
9
+ * @classdesc Provides a construct to create and deploy an EventBridge Piped Event Handler
10
+ * @example
11
+ * import { PipedEventHandler, PipedEventHandlerProps } '@gradientedge/cdk-utils'
12
+ * import { Construct } from 'constructs'
13
+ *
14
+ * class CustomConstruct extends PipedEventHandler {
15
+ * constructor(parent: Construct, id: string, props: PipedEventHandlerProps) {
16
+ * super(parent, id, props)
17
+ * this.props = props
18
+ * this.id = id
19
+ * this.initResources()
20
+ * }
21
+ * }
22
+ */
23
+ export class PipedEventHandler extends EventHandler {
24
+ props: PipedEventHandlerProps
25
+ pipedDlq: IQueue
26
+ pipedQueue: IQueue
27
+
28
+ protected constructor(parent: Construct, id: string, props: PipedEventHandlerProps) {
29
+ super(parent, id, props)
30
+ this.props = props
31
+ this.id = id
32
+ this.useMapState = true
33
+ this.provisionTarget = false
34
+ }
35
+
36
+ protected initResources() {
37
+ this.createPipedQueue()
38
+ this.handler.sqsTargets = [new SqsQueue(this.pipedQueue)]
39
+ super.initResources()
40
+ this.createSqsToSfnPipe()
41
+ this.createSqsToLambdaPipe()
42
+ }
43
+
44
+ /**
45
+ * @summary Method to create the piped queue and dlq.
46
+ */
47
+ protected createPipedQueue() {
48
+ this.pipedDlq = this.sqsManager.createQueue(`${this.id}-pipe-queue-dlq`, this, this.props.pipedDlq)
49
+ this.pipedQueue = this.sqsManager.createQueue(`${this.id}-pipe-queue`, this, this.props.pipedQueue, this.pipedDlq)
50
+ }
51
+
52
+ /**
53
+ * @summary Method to create the SQS to SFN pipe.
54
+ */
55
+ protected createSqsToSfnPipe() {
56
+ if (_.isEmpty(this.props.sqsToSfnPipe) || !this.handler.workflow) return
57
+ this.eventManager.createSqsToSfnCfnPipe(
58
+ `${this.id}-pipe-sfn`,
59
+ this,
60
+ this.props.sqsToSfnPipe,
61
+ this.pipedQueue,
62
+ this.handler.workflow
63
+ )
64
+ }
65
+
66
+ /**
67
+ * @summary Method to create the SQS to Lambda pipe.
68
+ */
69
+ protected createSqsToLambdaPipe() {
70
+ if (_.isEmpty(this.props.sqsToLambdaPipe) || _.isEmpty(this.handler.lambdaFunctions)) return
71
+ _.forEach(this.handler.lambdaFunctions, (lambdaFunction, index) => {
72
+ this.eventManager.createSqsToLambdaCfnPipe(
73
+ `${this.id}-pipe-lambda-${index}`,
74
+ this,
75
+ this.props.sqsToLambdaPipe,
76
+ this.pipedQueue,
77
+ lambdaFunction
78
+ )
79
+ })
80
+ }
81
+ }
@@ -0,0 +1,10 @@
1
+ import { QueueProps } from 'aws-cdk-lib/aws-sqs'
2
+ import { SqsToLambdaPipeProps, SqsToSfnPipeProps } from '../../services'
3
+ import { EventHandlerProps } from '../event-handler'
4
+
5
+ export interface PipedEventHandlerProps extends EventHandlerProps {
6
+ pipedDlq: QueueProps
7
+ pipedQueue: QueueProps
8
+ sqsToLambdaPipe: SqsToLambdaPipeProps
9
+ sqsToSfnPipe: SqsToSfnPipeProps
10
+ }
@@ -9,7 +9,14 @@ import { IStateMachine } from 'aws-cdk-lib/aws-stepfunctions'
9
9
  import _ from 'lodash'
10
10
  import { CommonConstruct } from '../../common'
11
11
  import { createCfnOutput } from '../../utils'
12
- import { EventBusProps, EventRuleProps, RuleProps, SqsToSfnPipeProps, DynamoDbToLambdaPipeProps } from './types'
12
+ import {
13
+ DynamoDbToLambdaPipeProps,
14
+ EventBusProps,
15
+ EventRuleProps,
16
+ RuleProps,
17
+ SqsToLambdaPipeProps,
18
+ SqsToSfnPipeProps,
19
+ } from './types'
13
20
 
14
21
  /**
15
22
  * @classdesc Provides operations on AWS EventBridge.
@@ -247,6 +254,57 @@ export class EventManager {
247
254
  return pipe
248
255
  }
249
256
 
257
+ /**
258
+ * @summary Method to create an eventbridge pipe with sqs queue as source and lambda function as target
259
+ * @param id scoped id of the resource
260
+ * @param scope scope in which this resource is defined
261
+ * @param props the props for the pipe
262
+ * @param sourceQueue the source sqs queue
263
+ * @param targetLambdaFunction the target lambda function
264
+ */
265
+ public createSqsToLambdaCfnPipe(
266
+ id: string,
267
+ scope: CommonConstruct,
268
+ props: SqsToLambdaPipeProps,
269
+ sourceQueue: IQueue,
270
+ targetLambdaFunction: IFunction
271
+ ) {
272
+ const pipeRole = scope.iamManager.createRoleForSqsToLambdaPipe(
273
+ `${id}-role`,
274
+ scope,
275
+ sourceQueue.queueArn,
276
+ targetLambdaFunction.functionArn
277
+ )
278
+
279
+ const pipe = new CfnPipe(scope, `${id}`, {
280
+ ...props,
281
+ name: `${props.name}-${scope.props.stage}`,
282
+ roleArn: pipeRole.roleArn,
283
+ source: sourceQueue.queueArn,
284
+ sourceParameters: {
285
+ filterCriteria: props.pipeFilterPattern
286
+ ? {
287
+ filters: [
288
+ {
289
+ pattern: JSON.stringify(props.pipeFilterPattern),
290
+ },
291
+ ],
292
+ }
293
+ : undefined,
294
+ sqsQueueParameters: {
295
+ batchSize: props.sqsBatchSize,
296
+ maximumBatchingWindowInSeconds: props.sqsMaximumBatchingWindowInSeconds,
297
+ },
298
+ },
299
+ target: targetLambdaFunction.functionArn,
300
+ })
301
+
302
+ createCfnOutput(`${id}-pipeArn`, scope, pipe.attrArn)
303
+ createCfnOutput(`${id}-pipeName`, scope, pipe.name)
304
+
305
+ return pipe
306
+ }
307
+
250
308
  /**
251
309
  * @summary Method to create an eventbridge pipe with DynamoDb stream as source and lambda function as target
252
310
  * @param id scoped id of the resource
@@ -3,7 +3,6 @@ import { CfnRuleProps, EventBusProps as EBProps, RuleProps as EBRuleProps } from
3
3
  import { TagProps } from '../../types'
4
4
 
5
5
  /**
6
- }
7
6
  */
8
7
  export interface SqsToSfnPipeProps extends CfnPipeProps {
9
8
  pipeFilterPattern?: any
@@ -13,6 +12,14 @@ export interface SqsToSfnPipeProps extends CfnPipeProps {
13
12
  sqsMaximumBatchingWindowInSeconds?: number
14
13
  }
15
14
 
15
+ /**
16
+ */
17
+ export interface SqsToLambdaPipeProps extends CfnPipeProps {
18
+ pipeFilterPattern?: any
19
+ sqsBatchSize?: number
20
+ sqsMaximumBatchingWindowInSeconds?: number
21
+ }
22
+
16
23
  /**
17
24
  */
18
25
  export interface EventRuleProps extends EBRuleProps {