Skip to content

Commit

Permalink
fix(events-targets): imported sqs queue cannot be used as a rule dlq (#…
Browse files Browse the repository at this point in the history
…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*
  • Loading branch information
JeromeGuyon committed Dec 21, 2023
1 parent 36bb696 commit 588b106
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
14 changes: 11 additions & 3 deletions packages/aws-cdk-lib/aws-events-targets/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,18 @@ export function addToDeadLetterQueueResourcePolicy(rule: events.IRule, queue: sq
/**
* Whether two string probably contain the same environment dimension (region or account)
*
* Used to compare either accounts or regions, and also returns true if both
* are unresolved (in which case both are expted to be "current region" or "current account").
* Used to compare either accounts or regions on a best effort basis. If we cannot tell definitively
* that the dimensions are in different environments, we will pass.
*
* For example, returns true if both are unresolved (in which case both are expected to be
* "current region" or "current account").
*
* Also returns true if one is unresolved (in which case we expect the unresolved dimension to match
* the resolved dimension, but it is up to the user to ensure this). Returning true here makes sure
* that we are not overly aggressive in producing a synth-time error.
*
* @internal
*/
function sameEnvDimension(dim1: string, dim2: string) {
return [TokenComparison.SAME, TokenComparison.BOTH_UNRESOLVED].includes(Token.compareStrings(dim1, dim2));
return [TokenComparison.SAME, TokenComparison.ONE_UNRESOLVED, TokenComparison.BOTH_UNRESOLVED].includes(Token.compareStrings(dim1, dim2));
}
38 changes: 37 additions & 1 deletion packages/aws-cdk-lib/aws-events-targets/test/sqs/sqs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Match, Template } from '../../../assertions';
import * as events from '../../../aws-events';
import * as kms from '../../../aws-kms';
import * as sqs from '../../../aws-sqs';
import { App, Duration, Stack } from '../../../core';
import * as ssm from '../../../aws-ssm';
import { App, CustomResource, Duration, Stack } from '../../../core';
import * as cxapi from '../../../cx-api';
import * as targets from '../../lib';

Expand Down Expand Up @@ -401,3 +402,38 @@ test('specifying retry policy with 0 retryAttempts', () => {
],
});
});

test('dead letter queue is imported', () => {
const stack = new Stack();
const queue = new sqs.Queue(stack, 'MyQueue', { fifo: true });
const rule = new events.Rule(stack, 'MyRule', {
schedule: events.Schedule.rate(Duration.hours(1)),
});

const dlqArn = 'arn:aws:sqs:eu-west-1:444455556666:queue1';
const deadLetterQueue = sqs.Queue.fromQueueArn(stack, 'MyDeadLetterQueue', dlqArn);

// WHEN
rule.addTarget(new targets.SqsQueue(queue, {
deadLetterQueue,
}));

Template.fromStack(stack).hasResourceProperties('AWS::Events::Rule', {
ScheduleExpression: 'rate(1 hour)',
State: 'ENABLED',
Targets: [
{
Arn: {
'Fn::GetAtt': [
'MyQueueE6CA6235',
'Arn',
],
},
Id: 'Target0',
DeadLetterConfig: {
Arn: dlqArn,
},
},
],
});
});

0 comments on commit 588b106

Please sign in to comment.