How to create CDK NestedStack?

Related searches

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:


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:

What am I doing wrong please?

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!

NestedStack — AWS Cloud Development Kit 1.54.0 documentation, CloudFormation updates the resources of modified nested stacks, but does not “cdk deploy”), but rather only synthesized as a template and uploaded as an the parameters passed to CloudFormation when this nested stack is created. You create a nested stack within another stack by using the NestedStack construct. As your infrastructure grows, common patterns can emerge in which you declare the same components in multiple templates. You can separate out these common components and create dedicated templates for them.

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)

How to create CDK NestedStack?, 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� After updating the AWS CDK, the AWS CDK Toolkit (CLI) reports a mismatch with the AWS Construct Library. The version of the AWS CDK Toolkit (which provides the cdk command) must be at least equal to the version of the AWS Construct Library. The Toolkit is intended to be backward compatible within the same major version; the latest 1.x version

Can you just try this? Sorry if it didn't work. I didn't try it out. Just based on the error, this needs to be in the scope of parent stack.

new PipelineStack(this, pipeline);

A better way to structure AWS CDK projects around Nested Stacks, Create a simple nested stack for the VPC with 2 subnets in it. Call it skeletonStack , and save this in the file . Now there is a VPC, and you can start deploying the components inside it. Create a simple Nested Stack with the necessary component (e.g. EC2 Instance). billingMode? Type: Billing Mode (optional, default: PROVISIONED if replicationRegions is not specified, PAY_PER_REQUEST otherwise) Specify how you are charged for read and write throughput and how you manage capacity.

Nested stack support � Issue #239 � aws/aws-cdk � GitHub, GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together. Sign up. The result is JSON, not YAML, and cdk.out is .gitignored, but it can still be helpful in verifying that your CDK code is generating the correct CloudFormation templates. (cdk diff may be the best option for seeing how CDK code changes will change your infrastructure). cdk deploy and CDK in GitLab CI pipelines

Using nested stacks to declare common components is considered a best practice.. Certain stack operations, such as stack updates, should be initiated from the root stack rather than performed directly on nested stacks themselves.

Hi, I'm trying to create a CloudFormation template which uses nested stacks. First of all I have to say that I don't use cdk deploy in order to deploy my template. What I do is generate .json templates inside the code calling app.synth() and executing .js file.