aws / aws/aws-cdk

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

Open
#30,530 2 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-events bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### 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](https://github.com/aws/aws-cdk/issues/18821#issuecomment-2071291771)

### 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:

```ts
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.

Contributor guide

Open the contributing guide

Research direction

Start by running the TypeScript reproduction with two stacks, then inspect the aws-events Rule.addTarget path and the SqsQueue target handling for cross-stack references. Done means the example synthesizes without a circular dependency and a regression test covers passing a Queue from another stack.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.