title: How to create CDK NestedStack?
body: <p>I am trying to create a CDK deployment that uses nested stacks:</p>
  <pre class="lang-ts prettyprint-override"><code>// 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(&quot;pipelines&quot;);
  new PipelineParentStack(app, 'PipelineParentStack', {
      pipelines: pipelines
  });
  </code></pre>
  <p>The <code>pipelines</code> string contains a comma-separated list for which we should create a nested stack for each element.</p>
  <pre class="lang-ts prettyprint-override"><code>// nested stacks sit within parent stack
  import cdk = require('@aws-cdk/core');
  import cfn = require('@aws-cdk/aws-cloudformation');
  import {Construct} from &quot;@aws-cdk/core&quot;;
  
  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(&quot;,&quot;);
        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
    }
  }
  </code></pre>
  <p>When I try to deploy the parent stack, I get the following error:</p>
  <pre><code>/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.&lt;anonymous&gt; (/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)
  </code></pre>
  <p>This appears to me to be configured as per the documentation here: <a href="https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-cloudformation" rel="nofollow noreferrer">https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-cloudformation</a></p>
  <p>What am I doing wrong please?</p>