首页 > 解决方案 > AWS CDK 从嵌套堆栈访问父堆栈中的资源

问题描述

我正在尝试将 Arn 传递给在父堆栈中创建的资源,以便在嵌套堆栈中使用。aws-cdk 文档指出:

当嵌套堆栈引用父堆栈中的资源时,CloudFormation 参数将自动添加到嵌套堆栈并从父堆栈分配

但是,尝试引用在父堆栈中创建的资源会导致Circular dependency between resources:错误。

将资源的引用从父堆栈传递到嵌套堆栈的最佳方法是什么?

object TestTemplateApp extends App {


  class MainStack(parent: Construct)
    extends Stack(parent, "Main") {

    //some other resources
    val firstNestedStack = new FirstNestedStack(this)

  }


  class FirstNestedStack(parent: Construct)
    extends NestedStack(parent, "Batch") {


    //some other resources

    val s3Bucket = Bucket.Builder
      .create(this, "id")
      .bucketName(("example"))
      .build()

    val BucketArn = s3Bucket.getBucketArn

    val secondNested = new SecondNestedStack(this, BucketArn)
  }

  class SecondNestedStack(parent: Construct, bucketArn: String) extends NestedStack(parent, "second") {

    //some other resources

    val secondS3Bucket = Bucket.Builder
      .create(this, s" S3WorkspaceBucket")
      .bucketName(s"$bucketArn example") // assume we want to use the ARN of the s3 bucket from the parent in the bucket in the nested stack
      .build()
  }

  val cdkApp = CDKApp.Builder.create().outdir("/tmp/test_template").build()
  val mainStack = new MainStack(cdkApp)
  cdkApp.synth()

}

标签: amazon-web-servicesamazon-cloudformationaws-cdk

解决方案


推荐阅读