【问题标题】:How to create CDK NestedStack?如何创建 CDK NestedStack?
【发布时间】:2020-03-07 15:11:43
【问题描述】:

我正在尝试创建使用嵌套堆栈的 CDK 部署:

// app
#!/usr/bin/env node
import 'source-map-support/register';
import cdk = require('@aws-cdk/core');
import { PipelineParentStack } from '../lib/pipeline-stack';

const app = new cdk.App();
const pipelines : string = app.node.tryGetContext("pipelines");
new PipelineParentStack(app, 'PipelineParentStack', {
    pipelines: pipelines
});

pipelines 字符串包含一个逗号分隔的列表,我们应该为每个元素创建一个嵌套堆栈。

// nested stacks sit within parent stack
import cdk = require('@aws-cdk/core');
import cfn = require('@aws-cdk/aws-cloudformation');
import {Construct} from "@aws-cdk/core";

interface PipelineParentStackProps extends cdk.StackProps {
  pipelines: string;
}

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

    if (props) {
      const pipelinesArray = props.pipelines.split(",");
      for (let pipeline of pipelinesArray) {
        new PipelineStack(scope, pipeline)
      }
    }
  }
}

export class PipelineStack extends cfn.NestedStack {
  constructor(scope: Construct, id: string, props?: cfn.NestedStackProps) {
    super(scope, id, props);

    // The code that defines your stack goes here
  }
}

当我尝试部署父堆栈时,我收到以下错误:

/tmp/pipeline/node_modules/@aws-cdk/aws-cloudformation/lib/nested-stack.ts:227

throw new Error(`Nested stacks must be defined within scope of another non-nested stack`);
^
Error: Nested stacks must be defined within scope of another non-nested stack
at findParentStack (/tmp/pipeline/node_modules/@aws-cdk/aws-cloudformation/lib/nested-stack.ts:227:11)
at new NestedStack (/tmp/pipeline/node_modules/@aws-cdk/aws-cloudformation/lib/nested-stack.ts:87:25)
at new PipelineStack (/src/function/pipeline/lib/pipeline-stack.ts:24:5)
at new PipelineParentStack (/src/function/pipeline/lib/pipeline-stack.ts:16:9)
at Object.<anonymous> (/src/function/pipeline/bin/pipeline.ts:8:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)

在我看来,这是按照此处的文档进行配置的:https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-cloudformation

请问我做错了什么?

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation aws-cdk


    【解决方案1】:

    您需要将嵌套堆栈的scope设置为父堆栈(this,因为它是在父构造函数中定义的),因此将嵌套堆栈定义更改为:

    new PipelineStack(this, pipeline)
    

    【讨论】:

      【解决方案2】:

      你可以试试这个吗?抱歉,如果它不起作用。我没试过。仅根据错误,这需要在父堆栈的范围内。

      new PipelineStack(this, pipeline);

      【讨论】:

        【解决方案3】:

        nested stack of Cloudformation的用法可以参考文档。

        虽然此功能被标记为稳定,但仍有block issue 可以使用它。您不能在嵌套堆栈之间共享 VPC 声明!

        【讨论】:

        • 该问题已解决,链接到的文档已被弃用。
        猜你喜欢
        • 2022-07-12
        • 2021-11-27
        • 2022-01-18
        • 2021-12-21
        • 1970-01-01
        • 1970-01-01
        • 2022-08-23
        • 1970-01-01
        • 2022-06-12
        相关资源
        最近更新 更多