aws-cdk: cli: cdk synth doesn't output yaml when having dependency stacks

❓ General Issue

I have a LambdaStack depending on NetworkStack’s vpc. There is the code

LambdaStack

import cdk = require('@aws-cdk/core');
import lambda = require('@aws-cdk/aws-lambda');
import iam = require('@aws-cdk/aws-iam');
import vpc = require('@aws-cdk/aws-ec2')

interface LambdaStackProps extends cdk.StackProps {
  vpc: vpc.Vpc
}

export class LambdaStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props: LambdaStackProps) {
    super(scope, id, props);
    // define lambdas by using props.vpc
  }
}

NetworkStack

import cdk = require('@aws-cdk/core');
import ec2 = require("@aws-cdk/aws-ec2");
import iam = require("@aws-cdk/aws-iam");


interface NetworkStackProps extends cdk.StackProps { }

export class NetworkStack extends cdk.Stack {
  public readonly vpc: ec2.Vpc;
  constructor(scope: cdk.Construct, id: string, prop: NetworkStackProps) {
    super(scope, id, prop);
    this.vpc = new ec2.Vpc(some code here);
  }
}

app

const networkStack = new NetworkStack(app, 'network-stack-dev', { env: UsEast1Env });
const lambdaStack = new LambdaStack(app, 'lambda-stack-dev', { 
  env: UsEast1Env,
  vpc: networkStack.vpc
});

I tried to generate yaml file for LambdaStack by executing

cdk synth lambda-stack-dev > template.yaml

Then I got

Including dependency stacks: network-stack-dev
Successfully synthesized to /Users/whao/Developer/serverless-dev/cdk.out
Supply a stack name (network-stack-dev, lambda-stack-dev) to display its template.

I tried cdk synth network-stack-dev lambda-stack-dev but it gave the same output. I am kinda confused with what Supply a stack name (network-stack-dev, lambda-stack-dev) to display its template. means

I have read issue #3259 but I didn’t find it helpful. Forgive me if I am asking stupid questions.

Environment

  • CDK CLI Version: 1.4.0 (build 175471f)
  • Module Version: N/A
  • OS: macOS 10.14.6
  • Language: TypeScript

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 28
  • Comments: 20 (6 by maintainers)

Commits related to this issue

Most upvoted comments

@sacag I was struggling with this myself and I found that if I added the --exclusively, -e flag to the synth command, I could get it to generate the YAML for the dependent stack. Hope that helps.

bumping this up as a p1 to prioritize a fix

We’re still seeing this issue in 1.19.0 - what’s the likelihood of it being picked up? It’d be great to be able to synth stacks without relying on cdk.out and having to convert the generated json into yaml for readability

EDIT: After looking through the code in aws-cdk, it appears that what’s happening is upstream stacks are being synthesized too, even if you specify a particular stack. Adding the -e flag sets exclusively to true, and only synthesizes the specified stack. It’s not clear from the docs that the list of stacks to synthesize is also the list of stacks it tries to print, so if there’s a dependency, it’ll try to synthesize those also, and fail to print to stdout because there are more than one.

I’d suggest updating the printed text on packages/aws-cdk/bin/cdk.ts:334 to suggest that -e might help if there are dependencies.

+1

put the hack code at the end of app.ts can generate all yaml files in cdk.out:


import {serializeStructure} from "aws-cdk/lib/serialize";
import { ISynthesisSession, Stack} from "@aws-cdk/core";
import * as path from "path";

...

const old = Stack.prototype._synthesizeTemplate;
const nw = function (this: Stack, session: ISynthesisSession) {
    old.apply(this, [session])

    const builder = session.assembly;
    const template = this._toCloudFormation();
    const outPath = path.join(builder.outdir, this.templateFile + ".yaml");
    fs.writeFileSync(outPath, serializeStructure(template, false));
}

Stack.prototype._synthesizeTemplate = nw

Hello @nija-at - do you know if there is a solution to generate YAML for dependent stacks?

@dabarrell - thanks for the suggestion! We’ll change for this to explain this better.

Also, we’re fairly good at reviewing and accepting pull requests, in case you’d like to help us out.

As a workaround, both the synthesized stack is present in the cdk.out folder in JSON format. That should unblock anyone who wants to find the synthesized stacks.

@jayrmotta - the issue you’re pointing out is being tracked in #3705. This one is about cdk synth not producing the expected synthesized yaml.