I am trying to create a CDK deployment that uses nested stacks:
// 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
});
The pipelines
string contains a comma-separated list for which we should create a nested stack for each element.
// 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
}
}
When I try to deploy the parent stack, I get the following error:
/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)
This appears to me to be configured as per the documentation here: https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-cloudformation
What am I doing wrong please?
You need to set the scope
of the nested stacks to the parent stack (this
since it is being defined in the parent constructor), so change the nested stack definition to:
new PipelineStack(this, pipeline)
You can refer to the doc about usage of nested stack of Cloudformation.
Though this feature is marked as stable, there is still having block issue for using it. You can NOT share the VPC declaration among nested stacks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With