gpt4 book ai didi

amazon-web-services - 如何创建CDK NestedStack?

转载 作者:行者123 更新时间:2023-12-03 16:38:04 25 4
gpt4 key购买 nike

我正在尝试创建使用嵌套堆栈的 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

请问我做错了什么?

最佳答案

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

new PipelineStack(this, pipeline)

关于amazon-web-services - 如何创建CDK NestedStack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58809705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com