question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected cyclic reference error with lambda.addEventSource

See original GitHub issue

I tried to split into two stacks. One creates S3 Bucket, and the other creates a lambda and add event source to that from s3 of other stack. When execute cdk deploy, error messages put on console.

Reproduction Steps

source Code

import cdk = require( '@aws-cdk/core' )
import * as S3 from '@aws-cdk/aws-s3';
import * as Lambda from '@aws-cdk/aws-lambda';
import { S3EventSource } from '@aws-cdk/aws-lambda-event-sources';

const app = new cdk.App();

export class Stack1 extends cdk.Stack {
  readonly bucket: S3.Bucket;
  constructor( scope: cdk.Construct, name: string, props?: cdk.StackProps ) {
    super( scope, name, props );

    this.bucket = new S3.Bucket( this, 'tests3' );
  }
}

interface Stack2Props extends cdk.StackProps {
  bucket: S3.Bucket
}
export class Stack2 extends cdk.Stack {

  constructor( scope: cdk.Construct, name: string, props: Stack2Props ) {
    super( scope, name, props );
    const lambda = new Lambda.Function(
      this,
      'testLambda',
      {
        functionName: 'test',
        code: Lambda.Code.fromAsset( { ***source path***} ),
        handler: 'index.handler',
        runtime: Lambda.Runtime.NODEJS_8_10,
      }
    );
    lambda.addEventSource(
      new S3EventSource( props.bucket, {
        events: [S3.EventType.OBJECT_CREATED],
      } )
    );
  }
}

const stack1 = new Stack1( app, 'stack1' );
const stack2 = new Stack2( app, 'stack2', {
  bucket: stack1.bucket
} );

app.synth();

Error Log

$ ENV=dev cdk deploy
'stack2' depends on 'stack1' (stack2 -> stack1/tests3/Resource.Arn). Adding this dependency (stack1 -> stack2/testLambda/Resource.Arn) would create a cyclic reference.

Environment

  • CLI Version : 1.14.0
  • Framework Version:1.14.0
  • OS :Mac

This is 🐛 Bug Report

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
stoyan-scavacommented, Jul 17, 2022

I want to keep this issue open. Currently I keep both s3 and lambda resources in a single stack to avoid the cyclic reference.

I’d like to separate global from regional resources. Also I’d like to keep my resources in a single app.

0reactions
anjanvbcommented, Aug 26, 2022

So even if I am importing the bucket (not the name or ARN) into the stack, the below seems to do the trick.

Stack 1 - Creates mybucket. Stack 2 - Imports it as a shared resources props.mybucket

In Stack 2 instead of using props.mybucket directly in the addEventSource, I do this-

const myBucket = s3.Bucket.fromBucketName(this, 'temp-my-bkt', props.mybucket.bucketName)
        
lambdaFn.addEventSource(new S3EventSource(inputIDPBucket, {
            events: [s3.EventType.OBJECT_CREATED],
            filters: [{ prefix: 'myprefix/'}]
}));
Read more comments on GitHub >

github_iconTop Results From Across the Web

CDK - S3 notification causing cyclic reference error
The error message indicates, that you use a Lambda. Where is that Lamdba definition? What are you trying to do with SNS?
Read more >
@aws-cdk/aws-lambda-event-sources - Package Manager
When a user calls the SNS Publish API on a topic that your Lambda function is subscribed to, Amazon SNS will call Lambda...
Read more >
Resolving circular dependency in provisioning of Amazon S3 ...
In this article, I present a mechanism to resolve the circular dependency while preserving the desired outcome of auto-generated S3 bucket names ...
Read more >
Resolving circular dependencies in CDK Constructs/CFN ...
Using some construct or resource that creates a circular relation ... CDK will complain that there is a circular reference among them.
Read more >
awslabs/aws-cdk - Gitter
Arn). Adding this dependency (LampycalLambdaStack/lampycal_calendars/ApiPermission.ANY.. -> LampycalAPIStack/LamPyCal/Resource.Ref) would create a cyclic ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found