Skip to content

Commit 588b106

Browse files
authored
fix(events-targets): imported sqs queue cannot be used as a rule dlq (#28165) (#28285)
This PR fixes the bug where imported SQS queue cannot be used as Rule DeadLetterQueue, since fromQueueArn can resolve region and account from v2.109.0 Closes #28165 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 36bb696 commit 588b106

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

packages/aws-cdk-lib/aws-events-targets/lib/util.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,18 @@ export function addToDeadLetterQueueResourcePolicy(rule: events.IRule, queue: sq
140140
/**
141141
* Whether two string probably contain the same environment dimension (region or account)
142142
*
143-
* Used to compare either accounts or regions, and also returns true if both
144-
* are unresolved (in which case both are expted to be "current region" or "current account").
143+
* Used to compare either accounts or regions on a best effort basis. If we cannot tell definitively
144+
* that the dimensions are in different environments, we will pass.
145+
*
146+
* For example, returns true if both are unresolved (in which case both are expected to be
147+
* "current region" or "current account").
148+
*
149+
* Also returns true if one is unresolved (in which case we expect the unresolved dimension to match
150+
* the resolved dimension, but it is up to the user to ensure this). Returning true here makes sure
151+
* that we are not overly aggressive in producing a synth-time error.
152+
*
145153
* @internal
146154
*/
147155
function sameEnvDimension(dim1: string, dim2: string) {
148-
return [TokenComparison.SAME, TokenComparison.BOTH_UNRESOLVED].includes(Token.compareStrings(dim1, dim2));
156+
return [TokenComparison.SAME, TokenComparison.ONE_UNRESOLVED, TokenComparison.BOTH_UNRESOLVED].includes(Token.compareStrings(dim1, dim2));
149157
}

packages/aws-cdk-lib/aws-events-targets/test/sqs/sqs.test.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { Match, Template } from '../../../assertions';
22
import * as events from '../../../aws-events';
33
import * as kms from '../../../aws-kms';
44
import * as sqs from '../../../aws-sqs';
5-
import { App, Duration, Stack } from '../../../core';
5+
import * as ssm from '../../../aws-ssm';
6+
import { App, CustomResource, Duration, Stack } from '../../../core';
67
import * as cxapi from '../../../cx-api';
78
import * as targets from '../../lib';
89

@@ -401,3 +402,38 @@ test('specifying retry policy with 0 retryAttempts', () => {
401402
],
402403
});
403404
});
405+
406+
test('dead letter queue is imported', () => {
407+
const stack = new Stack();
408+
const queue = new sqs.Queue(stack, 'MyQueue', { fifo: true });
409+
const rule = new events.Rule(stack, 'MyRule', {
410+
schedule: events.Schedule.rate(Duration.hours(1)),
411+
});
412+
413+
const dlqArn = 'arn:aws:sqs:eu-west-1:444455556666:queue1';
414+
const deadLetterQueue = sqs.Queue.fromQueueArn(stack, 'MyDeadLetterQueue', dlqArn);
415+
416+
// WHEN
417+
rule.addTarget(new targets.SqsQueue(queue, {
418+
deadLetterQueue,
419+
}));
420+
421+
Template.fromStack(stack).hasResourceProperties('AWS::Events::Rule', {
422+
ScheduleExpression: 'rate(1 hour)',
423+
State: 'ENABLED',
424+
Targets: [
425+
{
426+
Arn: {
427+
'Fn::GetAtt': [
428+
'MyQueueE6CA6235',
429+
'Arn',
430+
],
431+
},
432+
Id: 'Target0',
433+
DeadLetterConfig: {
434+
Arn: dlqArn,
435+
},
436+
},
437+
],
438+
});
439+
});

0 commit comments

Comments
 (0)