Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(events-targets): imported sqs queue cannot be used as a rule dlq (#28165) #28285

Merged
merged 9 commits into from
Dec 21, 2023
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,
},
},
],
});
});