我正在尝试创建一个使用嵌套堆栈的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
请问我做错了什么?
发布于 2019-12-13 02:01:45
您需要将嵌套堆栈的scope设置为父堆栈(this,因为它是在父构造函数中定义的),因此将嵌套堆栈定义更改为:
new PipelineStack(this, pipeline)发布于 2019-11-17 23:11:21
您可以参考文档了解nested stack of Cloudformation的用法。
虽然这个特性被标记为稳定,但仍然有block issue可以使用它。不能在嵌套堆栈之间共享VPC声明!
发布于 2019-11-24 12:34:33
你就不能试试这个吗?如果它不起作用,我很抱歉。我没有试过。只是基于错误,这需要在父栈的范围内。
新建流水线(this,PipelineStack);
https://stackoverflow.com/questions/58809705
复制相似问题