From 588b1064c289295c5acce1b991ef8dcd7c8bec3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20GUYON?= Date: Thu, 21 Dec 2023 18:15:42 +0100 Subject: [PATCH] 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* --- .../aws-events-targets/lib/util.ts | 14 +++++-- .../aws-events-targets/test/sqs/sqs.test.ts | 38 ++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/packages/aws-cdk-lib/aws-events-targets/lib/util.ts b/packages/aws-cdk-lib/aws-events-targets/lib/util.ts index ab279d03f4b60..7d8162df20d60 100644 --- a/packages/aws-cdk-lib/aws-events-targets/lib/util.ts +++ b/packages/aws-cdk-lib/aws-events-targets/lib/util.ts @@ -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)); } diff --git a/packages/aws-cdk-lib/aws-events-targets/test/sqs/sqs.test.ts b/packages/aws-cdk-lib/aws-events-targets/test/sqs/sqs.test.ts index 3c979d1b0fe49..4bdd8f633e6a5 100644 --- a/packages/aws-cdk-lib/aws-events-targets/test/sqs/sqs.test.ts +++ b/packages/aws-cdk-lib/aws-events-targets/test/sqs/sqs.test.ts @@ -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'; @@ -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, + }, + }, + ], + }); +});