aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

(assertions): NestedStacks inside NestedCloudAssemblys fail `Template.fromStack()`

Open Styerp opened this issue 3 years ago • 3 comments

Describe the bug

Templates aren't created for NestedStacks that reside in a Stage below the App root.

My application defines a (custom) pipeline. Each wave of my pipeline is a Stage. These stages have Stacks as deployment units, any of which can contain NestedStack constructs. When testing against the NestedStacks, all templates fail to generate, as they don't include the stage assembly inside the path.

Expected Behavior

Templates are created and can be tested against.

Current Behavior

Tests fail with "ENOENT: no such file or directory" errors, as the path is incomplete.

Reproduction Steps

I wrote this so I could run it in my Node REPL, so it uses requires.

const cdk = require("aws-cdk-lib")
const fs = require("fs");
const path = require("path");

const app = new cdk.App("Root");
const stage = new cdk.Stage(app, "Trunk");
const stack = new cdk.Stack(stage, "Branch");
const nested1 = new cdk.NestedStack(stack, "Leaf1");
const nestedConstruct = new cdk.aws_s3.Bucket(nested1, "NestedBucket", {})


const nested2 = new cdk.NestedStack(stack, "Leaf2");
const templateNest1 = cdk.assertions.Template.fromStack(nested1) //fails - ENOENT: no such file or directory, open '/private/var/folders/lj/2pctvcb518dcg10t7m9kk660f4_slb/T/cdk.out03txcp/intermediateTopLevelNest12B0AFB29.nested.template.json'

const fullAssembly = app.synth();
// Walk down the construct tree to find the last stage in this construct's path. I'm sure there's a nicer way to do this.
const closestStageToStack = nested1.node.scopes.reduce(function(prev, current){
  if(cdk.Stage.isStage(current)) return current
  else return prev;
})
const workingTemplateFile = fs.readFileSync(path.join(fullAssembly.directory, "assembly-" + closestStageToStack.stageName, nested1.templateFile)).toString("utf-8")
const goodTemplate = cdk.assertions.Template.fromString(workingTemplateFile)
console.log(goodTemplate.template)

Possible Solution

Inclusion of intermediate assemblies in the path specifying nested stacks resolves the problem, at least for single layers of nesting.

Additional Information/Context

No response

CDK CLI Version

2.40.0

Framework Version

No response

Node.js Version

16

OS

OSX

Language

Typescript

Language Version

Typescript (4.9)

Other information

No response

Styerp avatar Feb 04 '23 00:02 Styerp

Presumably the culprit is here, where no levels of nesting underneath the root are considered.

Styerp avatar Feb 04 '23 01:02 Styerp

Thanks for reporting this, I've been able to reproduce and confirm the issue.

Thank you as well for the suggested change and PR, we'll try to take a look and provide feedback when possible 🙂

peterwoodworth avatar Feb 07 '23 01:02 peterwoodworth

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Apr 18 '23 14:04 github-actions[bot]