首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建CDK NestedStack?

如何创建CDK NestedStack?
EN

Stack Overflow用户
提问于 2019-11-12 06:51:19
回答 3查看 7.5K关注 0票数 5

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

代码语言:javascript
复制
// 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字符串包含一个逗号分隔的列表,我们应该为每个元素创建一个嵌套堆栈。

代码语言:javascript
复制
// 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
  }
}

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

代码语言:javascript
复制
/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

请问我做错了什么?

EN

回答 3

Stack Overflow用户

发布于 2019-12-13 02:01:45

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

代码语言:javascript
复制
new PipelineStack(this, pipeline)
票数 4
EN

Stack Overflow用户

发布于 2019-11-17 23:11:21

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

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

票数 1
EN

Stack Overflow用户

发布于 2019-11-24 12:34:33

你就不能试试这个吗?如果它不起作用,我很抱歉。我没有试过。只是基于错误,这需要在父栈的范围内。

新建流水线(this,PipelineStack);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58809705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档