CDK-S3通知导致循环引用错误 [英] CDK - S3 notification causing cyclic reference error

查看:104
本文介绍了CDK-S3通知导致循环引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个堆栈中创建一个S3存储桶,将其传递到另一个堆栈中,然后使用它在sns或sqs上创建通知.这是一个分解后的代码示例.

I want to create an S3 bucket in one stack, pass it into another and then use it to create a notification onto an sns or sqs. Here is an example of broken down code would look like.

堆栈1

export class BucketStack extends BaseStack {

  public readonly mynBucket: Bucket;


  constructor(scope: App, id: string, props?: StackProps) {
    const properties: StackProps = {
      env: {
        region: StackConfiguration.region,
      },
    };
    super(scope, id, properties);
    this.myBucket = this.createMyBucket();
  }


  private createMyBucket() {
   // create and return bucket
}

堆栈2

import * as s3n from '@aws-cdk/aws-s3-notifications';

export class ServiceStack extends BaseStack {
  constructor(scope: App, id: string, myBucket: Bucket) {
    const properties: StackProps = {
      env: {
        region: StackConfiguration.region,
      },
    };

    super(scope, id, properties);

    const topic = new Topic(this, 'Topic');
  
    myBucket.addEventNotification(EventType.OBJECT_CREATED_PUT, new s3n.SnsDestination(topic));

错误是错误:"my-bucket-stack"取决于"my-service-stack"(取决于Lambda资源.取决于资源.).添加此依赖性(my-service-stack-> my-bucket-stack/myBucket/Resource.Arn)将创建循环引用.

推荐答案

错误消息表明您使用的是Lambda.

The error message indicates, that you use a Lambda.

Lamdba的定义在哪里?

Where is that Lamdba definition?

您打算如何使用SNS?

What are you trying to do with SNS?

我假设您想应用模式(S3 PUT->通知-> Lambda),因为您没有发布完整的代码(包括Lambda)本身.

I assume that you wanted to apply the pattern (S3 PUT -> Notification -> Lambda) because you didn't post the full code (including the Lambda) itself.

继续我的假设,让我向您展示当前面临的问题:

Proceeding with my assumption, let me show you the issue what you're currently facing:

与CDK一起使用的普通云形成本身无法解决此问题,因此您需要创建一个自定义资源来解决此问题.但是,AWS CDK可以解决此问题,因为它会自动创建所需的自定义资源和正确的创建顺序!

Plain Cloud Formation, which is being used under the hood with CDK, can't tackle that by itself and you would need to create a Custom Resource to overcome this issue. AWS CDK can tackle this though, because it's creating the required Custom Resource and the correct creation sequence automatically!

而不是使用 s3n.SnsDestination ,而是使用Lambda Destination,如代码片段所示.您可以轻松地将所有内容提取到单独的堆栈中,因为问题不应该与单独的堆栈有关.

Instead of using the s3n.SnsDestination, use the Lambda Destination as shown in the snippet. You can easily extract everything in separate stacks, because the issue shouldn't be with separating stacks.

export class TestStack extends Stack {
  constructor(scope: cdk.Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const bucket = new Bucket(this, "your-bucket-construct-id",{
       // ...
    });

     // Lambda
    const lambda = new Function(this, 'the-triggered-lambda', {
      code: Code.asset(path.join(__dirname,  '../src/your-lambda-folder')),
      handler: 'index.handler',
      runtime: Runtime.NODEJS_12_X,
    });

    // S3 -> Lambda
    bucket.addEventNotification(EventType.OBJECT_CREATED_PUT, new LambdaDestination(lambda));
  }
}

如果这是在回答您的问题还是遇到任何问题,请告诉我.

Let me know, if that's answering your question or if you're facing any issues.

这篇关于CDK-S3通知导致循环引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆