Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(assertions): NestedStacks inside NestedCloudAssemblys fail Template.fromStack() #24004

Closed
Styerp opened this issue Feb 4, 2023 · 3 comments · Fixed by #25006
Closed

(assertions): NestedStacks inside NestedCloudAssemblys fail Template.fromStack() #24004

Styerp opened this issue Feb 4, 2023 · 3 comments · Fixed by #25006
Labels
@aws-cdk/assertions Related to the @aws-cdk/assertv2 package bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@Styerp
Copy link
Contributor

Styerp commented Feb 4, 2023

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 Styerp added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 4, 2023
@github-actions github-actions bot added the @aws-cdk/assertions Related to the @aws-cdk/assertv2 package label Feb 4, 2023
@Styerp
Copy link
Contributor Author

Styerp commented Feb 4, 2023

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

@Styerp Styerp changed the title (assertions): NestedStacks inside a Stage in an App fail construction with .fromStack() (assertions): NestedStacks inside NestedCloudAssemblys fail Template.fromStack() Feb 4, 2023
@peterwoodworth
Copy link
Contributor

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 peterwoodworth added p1 effort/small Small work item – less than a day of effort and removed needs-triage This issue or PR still needs to be triaged. labels Feb 7, 2023
Styerp added a commit to Styerp/aws-cdk that referenced this issue Apr 10, 2023
@mergify mergify bot closed this as completed in #25006 Apr 18, 2023
@github-actions
Copy link

⚠️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.

mergify bot pushed a commit that referenced this issue Apr 18, 2023
#25006)

…emplates

Templates are placed inside a path described by their closest assembly. Using this assembly lets nested stack templates resolve, regardless of their stage depth. Previously, they would fail to resolve if they were in a stage other than the app.

Closes #24004.
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/assertions Related to the @aws-cdk/assertv2 package bug This issue is a bug. effort/small Small work item – less than a day of effort p1
Projects
None yet
2 participants