S3 LambdaDestination creates a cyclic reference
See original GitHub issueSetting S3 Bucket notification to a Lambda function in the other stack creates a cyclic reference.
Reproduction Steps
import * as cdk from "@aws-cdk/core";
import * as lambda from "@aws-cdk/aws-lambda";
import * as s3 from "@aws-cdk/aws-s3";
import * as s3n from "@aws-cdk/aws-s3-notifications";
const app = new cdk.App();
const stack1 = new cdk.Stack(app, "stack1");
const stack2 = new cdk.Stack(app, "stack2");
const lambdaFunction = new lambda.Function(stack1, "lambdaFunction", {
code: lambda.Code.fromInline("whatever"),
handler: "index.handler",
runtime: lambda.Runtime.NODEJS_10_X
});
const bucket = new s3.Bucket(stack2, "bucket");
bucket.addObjectCreatedNotification(new s3n.LambdaDestination(lambdaFunction));
$ cdk synth
Error Log
/workspaces/cdk-sample/node_modules/@aws-cdk/core/lib/stack.ts:639
throw new Error(`'${target.node.path}' depends on '${this.node.path}' (${cycle.join(', ')}). Adding this dependency (${reason}) would create a cyclic reference.`);
^
Error: 'stack2' depends on 'stack1' (stack2 -> stack1/lambdaFunction/Resource.Arn, "stack2/bucket/Notifications/Resource" depends on "stack1/lambdaFunction/AllowBucketNotificationsFromstack2bucketA161360C"). Adding this dependency (stack1 -> stack2/bucket/Resource.Arn) would create a cyclic reference.
at Stack._addAssemblyDependency (/workspaces/cdk-sample/node_modules/@aws-cdk/core/lib/stack.ts:639:15)
at Object.addDependency (/workspaces/cdk-sample/node_modules/@aws-cdk/core/lib/deps.ts:44:20)
at Stack.addDependency (/workspaces/cdk-sample/node_modules/@aws-cdk/core/lib/stack.ts:343:5)
at Stack.prepareCrossReference (/workspaces/cdk-sample/node_modules/@aws-cdk/core/lib/stack.ts:906:24)
at Stack.prepare (/workspaces/cdk-sample/node_modules/@aws-cdk/core/lib/stack.ts:749:37)
at Function.prepare (/workspaces/cdk-sample/node_modules/@aws-cdk/core/lib/construct.ts:81:28)
at Function.synth (/workspaces/cdk-sample/node_modules/@aws-cdk/core/lib/construct.ts:40:10)
at App.synth (/workspaces/cdk-sample/node_modules/@aws-cdk/core/lib/app.ts:141:36)
at process.<anonymous> (/workspaces/cdk-sample/node_modules/@aws-cdk/core/lib/app.ts:120:45)
at Object.onceWrapper (events.js:300:26)
Environment
- CLI Version : 1.20.0
- Framework Version: 1.20.0
- OS : All
- Language : TypeScript
Other
The resources for the notification seem to have been attached to the stack for Lambda Function and it is causing cyclic reference. Instead, it should be attached to the stack for S3 Bucket.
Passing explicit scope at fn.addPermission()
would solve the problem, but I’m not sure it is an appropriate fix or not.
- https://github.com/aws/aws-cdk/blob/v1.20.0/packages/@aws-cdk/aws-s3-notifications/lib/lambda.ts#L17-L21
- https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Permission.html
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (2 by maintainers)
Top Results From Across the Web
aws cdk - Circular reference when using new s3notifications ...
Adding this dependency (${reason}) would create a cyclic reference. ); I have incomingBucket in stack A, and incomingHandler in stackB, this ...
Read more >Resolving circular dependency in provisioning of Amazon S3 ...
Solution using custom resource We can avoid the circular dependency demonstrated earlier by creating the S3 bucket without any notification ...
Read more >Getting around circular CloudFormation Dependencies: S3 ...
So we have to decouple things. We can do that by first creating the bucket and the lambda function and bringing them together...
Read more >@aws-cdk/aws-s3-notifications | Yarn - Package Manager
Fast, reliable, and secure dependency management. ... example shows how to send a notification to an SNS topic when an object is created...
Read more >Using S3 Event Notifications in AWS CDK - Complete Guide
Lambda Destination for S3 Bucket Notifications in AWS CDK # · We created a lambda function, which we'll use as a destination for...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
+1 I’m having the same issue when using
SqsDestination
.Hi @rinfield, thanks for reporting this. We will update this issue when there is movement.