AWS CDK从嵌套堆栈访问父堆栈中的资源

时间:2020-02-06 13:47:36

标签: amazon-web-services amazon-cloudformation 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()

}

0 个答案:

没有答案