Skip to content

Commit 318eae6

Browse files
feat(events): dead letter queue for an Event Bus (#30628)
### Issue # (if applicable) Closes #30531. ### Reason for this change EventBus L2 construct doesn't support for configuring dead letter queue. ### Description of changes Add `deadLetterQueue` prop to `EventBusProps` ### Description of how you validated changes Add both unit and integ test ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a32436a commit 318eae6

File tree

8 files changed

+107
-8
lines changed

8 files changed

+107
-8
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-events/test/integ.eventbus.js.snapshot/Stack.assets.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-events/test/integ.eventbus.js.snapshot/Stack.template.json

+13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
{
22
"Resources": {
3+
"DLQ581697C4": {
4+
"Type": "AWS::SQS::Queue",
5+
"UpdateReplacePolicy": "Delete",
6+
"DeletionPolicy": "Delete"
7+
},
38
"BusEA82B648": {
49
"Type": "AWS::Events::EventBus",
510
"Properties": {
11+
"DeadLetterConfig": {
12+
"Arn": {
13+
"Fn::GetAtt": [
14+
"DLQ581697C4",
15+
"Arn"
16+
]
17+
}
18+
},
619
"Description": "myEventBus",
720
"Name": "StackBusAA0A1E4B"
821
}

packages/@aws-cdk-testing/framework-integ/test/aws-events/test/integ.eventbus.js.snapshot/manifest.json

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-events/test/integ.eventbus.js.snapshot/tree.json

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-events/test/integ.eventbus.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import * as iam from 'aws-cdk-lib/aws-iam';
2+
import * as sqs from 'aws-cdk-lib/aws-sqs';
23
import { App, Stack } from 'aws-cdk-lib';
34
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
45
import { EventBus } from 'aws-cdk-lib/aws-events';
56

67
const app = new App();
78
const stack = new Stack(app, 'Stack');
9+
10+
const dlq = new sqs.Queue(stack, 'DLQ');
11+
812
const bus = new EventBus(stack, 'Bus', {
13+
deadLetterQueue: dlq,
914
description: 'myEventBus',
1015
});
1116

packages/aws-cdk-lib/aws-events/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,22 @@ bus.archive('MyArchive', {
222222
});
223223
```
224224

225+
## Dead-Letter Queue for EventBus
226+
227+
It is possible to configure a [Dead Letter Queue for an EventBus](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-rule-event-delivery.html#eb-rule-dlq). This is useful when you want to capture events that could not be delivered to any of the targets.
228+
229+
To configure a Dead Letter Queue for an EventBus, you can use the `deadLetterQueue` property of the `EventBus` construct.
230+
231+
```ts
232+
import * as sqs from 'aws-cdk-lib/aws-sqs';
233+
234+
const dlq = new sqs.Queue(this, 'DLQ');
235+
236+
const bus = new events.EventBus(this, 'Bus', {
237+
deadLetterQueue: dlq,
238+
});
239+
```
240+
225241
## Granting PutEvents to an existing EventBus
226242

227243
To import an existing EventBus into your CDK application, use `EventBus.fromEventBusArn`, `EventBus.fromEventBusAttributes`

packages/aws-cdk-lib/aws-events/lib/event-bus.ts

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Archive, BaseArchiveProps } from './archive';
33
import { CfnEventBus, CfnEventBusPolicy } from './events.generated';
44
import * as iam from '../../aws-iam';
55
import * as kms from '../../aws-kms';
6+
import * as sqs from '../../aws-sqs';
67
import { ArnFormat, IResource, Lazy, Names, Resource, Stack, Token } from '../../core';
78

89
/**
@@ -80,6 +81,15 @@ export interface EventBusProps {
8081
*/
8182
readonly eventSourceName?: string;
8283

84+
/**
85+
* Dead-letter queue for the event bus
86+
*
87+
* @see https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-rule-event-delivery.html#eb-rule-dlq
88+
*
89+
* @default - no dead-letter queue
90+
*/
91+
readonly deadLetterQueue?: sqs.IQueue;
92+
8393
/**
8494
* The event bus description.
8595
*
@@ -343,6 +353,9 @@ export class EventBus extends EventBusBase {
343353
const eventBus = new CfnEventBus(this, 'Resource', {
344354
name: this.physicalName,
345355
eventSourceName,
356+
deadLetterConfig: props?.deadLetterQueue ? {
357+
arn: props.deadLetterQueue.queueArn,
358+
} : undefined,
346359
description: props?.description,
347360
kmsKeyIdentifier: props?.kmsKey?.keyArn,
348361
});

packages/aws-cdk-lib/aws-events/test/event-bus.test.ts

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
22
import { Template } from '../../assertions';
33
import * as iam from '../../aws-iam';
4-
import { Effect } from '../../aws-iam';
54
import * as kms from '../../aws-kms';
5+
import * as sqs from '../../aws-sqs';
66
import { Aws, CfnResource, Stack, Arn, App, PhysicalName, CfnOutput } from '../../core';
77
import { EventBus } from '../lib';
88

@@ -568,7 +568,7 @@ describe('event bus', () => {
568568

569569
// WHEN
570570
bus.addToResourcePolicy(new iam.PolicyStatement({
571-
effect: Effect.ALLOW,
571+
effect: iam.Effect.ALLOW,
572572
principals: [new iam.AccountPrincipal('111111111111111')],
573573
actions: ['events:PutEvents'],
574574
sid: '123',
@@ -616,15 +616,15 @@ describe('event bus', () => {
616616
const bus = new EventBus(stack, 'Bus');
617617

618618
const statement1 = new iam.PolicyStatement({
619-
effect: Effect.ALLOW,
619+
effect: iam.Effect.ALLOW,
620620
principals: [new iam.ArnPrincipal('arn')],
621621
actions: ['events:PutEvents'],
622622
sid: 'statement1',
623623
resources: [bus.eventBusArn],
624624
});
625625

626626
const statement2 = new iam.PolicyStatement({
627-
effect: Effect.ALLOW,
627+
effect: iam.Effect.ALLOW,
628628
principals: [new iam.ArnPrincipal('arn')],
629629
actions: ['events:DeleteRule'],
630630
sid: 'statement2',
@@ -649,12 +649,28 @@ describe('event bus', () => {
649649

650650
// THEN
651651
expect(() => bus.addToResourcePolicy(new iam.PolicyStatement({
652-
effect: Effect.ALLOW,
652+
effect: iam.Effect.ALLOW,
653653
principals: [new iam.ArnPrincipal('arn')],
654654
actions: ['events:PutEvents'],
655655
}))).toThrow('Event Bus policy statements must have a sid');
656656
});
657657

658+
test('set dead letter queue', () => {
659+
const app = new App();
660+
const stack = new Stack(app, 'Stack');
661+
const dlq = new sqs.Queue(stack, 'DLQ');
662+
new EventBus(stack, 'Bus', {
663+
deadLetterQueue: dlq,
664+
});
665+
666+
Template.fromStack(stack).hasResourceProperties('AWS::Events::EventBus', {
667+
DeadLetterConfig: {
668+
Arn: {
669+
'Fn::GetAtt': ['DLQ581697C4', 'Arn'],
670+
},
671+
},
672+
});
673+
});
658674
test('Event Bus with a customer managed key', () => {
659675
// GIVEN
660676
const app = new App();

0 commit comments

Comments
 (0)