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

(aws-events): circular dependency error when providing cross-stack Queue to Rule.addTarget() #30530

Open
dmeehan1968 opened this issue Jun 12, 2024 · 2 comments
Labels
@aws-cdk/aws-events Related to CloudWatch Events bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@dmeehan1968
Copy link

Describe the bug

Reopens to #18821

Original bug report reproduced below

Given one stack that creates a Queue
When I consume that Queue in another stack using Rule.addTarget(queue)
Then I get a circular dependency error

Expected Behavior

No circular dependency error.

Current Behavior

Error: 'QueueProducer' depends on 'QueueConsumer' (QueueProducer -> QueueConsumer/MyRule/Resource.Arn). Adding this dependency (QueueConsumer -> QueueProducer/MainQueue/Resource.Arn) would create a cyclic reference.

Reproduction Steps

import { App, Stack } from "aws-cdk-lib";
import {Queue} from "aws-cdk-lib/aws-sqs";
import { EventBus, Rule } from "aws-cdk-lib/aws-events";
import { SqsQueue } from "aws-cdk-lib/aws-events-targets";

const app = new App();

// Stack A
const queueProducerStack = new Stack(app, "QueueProducer");
const mainQueue = new Queue(queueProducerStack, "MainQueue");

// Stack B
const queueConsumerStack = new Stack(app, "QueueConsumer");
const eventBus = new EventBus(queueConsumerStack, "EventBus");
const eventRule = new Rule(queueConsumerStack, "MyRule", {
  eventBus,
  eventPattern: {
    account: [Stack.of(queueConsumerStack).account],
  },
});
const sqsQueue = new SqsQueue(mainQueue);
eventRule.addTarget(sqsQueue);

Possible Solution

No response

Additional Information/Context

Workaround: Use the queueArn to create a cross-stack reference, e.g.

Queue.fromQueueArn(mainQueue.queueArn)

credit

CDK CLI Version

2.145.0

Framework Version

No response

Node.js Version

18.18.1

OS

macOS 14.5

Language

TypeScript

Language Version

5.4.5

Other information

In my own encounter, its interesting to note that the error message seems to mention another queue, not the one that I'm actually referencing (and yes, I checked to make sure these were discrete and correct).

Code:

    rule.addTarget(new SqsQueue(state.payloadDecoder.queue))

Error:

Error: 'Prod/L8-state' depends on 'Prod/L8' (Prod/L8-state -> Prod/L8/Ingest/Decoder/Rule/Resource.Arn). Adding this dependency (Prod/L8 -> Prod/L8-state/NormalisedUplinks/Queue/Resource.Arn) would create a cyclic reference.

The payload decoder rule is receiving a reference to the payloadDecoder.queue, but the error references the normalisedUplinks.queue, which is also created in the other stack (I have a state stack, and a compute stack). Compute stack creates the rule, and references the queue in the state stack.

@dmeehan1968 dmeehan1968 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 12, 2024
@github-actions github-actions bot added the @aws-cdk/aws-events Related to CloudWatch Events label Jun 12, 2024
@khushail khushail added needs-reproduction This issue needs reproduction. p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jun 12, 2024
@khushail
Copy link
Contributor

Hi @dmeehan1968 , thanks for reporting this. I see this past issue is still occurring , marking current one as P2 as there is a workaround available as mentioned.

@khushail khushail removed the needs-reproduction This issue needs reproduction. label Jun 12, 2024
@dmeehan1968
Copy link
Author

dmeehan1968 commented Jun 13, 2024

@khushail It appears that the workaround causes the stateful stack to have CfnOutput's associated with the source queue, held by the stateless stack but exported from the stateful stack. This causes a deadlock that prevents changes to the queue, because a reference is held by the stateless stack.

I think the workarond to the lock is to create a second queue, pass that to the stateless stack in order to free the attribute reference, and then on a subsequent deploy to remove the previous stack.

I think this is a common problem with cross stack resources, I've experienced similar with DynamoDB tables, and thus might affect SQS Queues as well. I'm not sure whether there is best practice guidance on how to avoid this beyond what I mention above.

Screenshot 2024-06-12 at 11 24 10

Medium Article describing the process of releasing cross stack resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-events Related to CloudWatch Events bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

No branches or pull requests

2 participants