question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[core] Multiple projects breaks CDK synth (using instanceof cross-project)

See original GitHub issue

❓ General Issue

Creating a Construct, shared by multiple stacks, in a separate project, fails to deploy in CDK version >= 1.57.0

In our setup we have multiple stacks sharing misc. Constructs. These Constructs are all in separate Typescript/NodeJS projects. We use Lerna to maintain/link dependencies between projects.

A simplified example of the structure could be:

➜ tree
.
β”œβ”€β”€ README.md
β”œβ”€β”€ lerna.json
β”œβ”€β”€ package-lock.json
β”œβ”€β”€ package.json
└── packages
    β”œβ”€β”€ myapp
    β”‚Β Β  β”œβ”€β”€ README.md
    β”‚Β Β  β”œβ”€β”€ bin
    β”‚Β Β  β”‚Β Β  └── myapp.ts
    β”‚Β Β  β”œβ”€β”€ cdk.json
    β”‚Β Β  β”œβ”€β”€ jest.config.js
    β”‚Β Β  β”œβ”€β”€ lambda
    β”‚Β Β  β”‚Β Β  └── index.ts
    β”‚Β Β  β”œβ”€β”€ lib
    β”‚Β Β  β”‚Β Β  └── myapp-stack.ts
    β”‚Β Β  β”œβ”€β”€ package-lock.json
    β”‚Β Β  β”œβ”€β”€ package.json
    β”‚Β Β  β”œβ”€β”€ test
    β”‚Β Β  β”‚Β Β  └── myapp.test.ts
    β”‚Β Β  └── tsconfig.json
    └── myconstruct
        β”œβ”€β”€ README.md
        β”œβ”€β”€ jest.config.js
        β”œβ”€β”€ lambda
        β”‚Β Β  └── index.ts
        β”œβ”€β”€ lib
        β”‚Β Β  └── index.ts
        β”œβ”€β”€ package-lock.json
        β”œβ”€β”€ package.json
        β”œβ”€β”€ test
        β”‚Β Β  └── myconstruct.test.ts
        └── tsconfig.json

(an example can be found here: https://github.com/gwriss/upgrade_bug)

Running

➜ npx lerna bootstrap

will npm-install all dependencies and link projects like this (snipped for better overview):

➜ tree -L 4
.
β”œβ”€β”€ ...
└── packages
    β”œβ”€β”€ myapp
    β”‚Β Β  β”œβ”€β”€ ...
    β”‚Β Β  β”œβ”€β”€ node_modules
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ ...
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ @aws-cdk
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ ...
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ myconstruct -> ../../myconstruct
    β”‚Β Β  β”‚Β Β  └── ...
    β”‚Β Β  └── ...
    └── myconstruct
        β”œβ”€β”€ ...
        β”œβ”€β”€ node_modules
        β”‚Β Β  β”œβ”€β”€ ...
        β”‚Β Β  β”œβ”€β”€ @aws-cdk
        β”‚Β Β  └── ...
        └── ...

Now, when the Construct in myconstruct contains a Lambda Function and the Myapp in myapp instantiate an instance of Myconstruct, the synth of the app fails:

➜ npx lerna run build
lerna notice cli v3.22.1
...
...
lerna success - myapp
lerna success - myconstruct
➜ cd packages/myapp
➜ npx cdk synth
unable to determine cloud assembly output directory. Assets must be defined indirectly within a "Stage" or an "App" scope
Subprocess exited with error 1

(an example of the setup can be found and tested here: https://github.com/gwriss/upgrade_bug)

Diving into the problem, it seems like it’s another issue with instanceof comparing two instances of the same class, but from different node_modules.

I think @tneely was onto something in https://github.com/aws/aws-cdk/issues/9546#issuecomment-671064704, but decided to close the issue without a resolution.

Another issued related to this is the β€œDuck Typing” fix of Stack in https://github.com/aws/aws-cdk/issues/10671.

⚠️ Notice Please don’t pollute the discussion with suggestions about β€œdeleting node_modules & reinstall” and β€œchecking modules are all of the same version”. That is another issue but has nothing to do with this issue!

The Question

Did I miss som crucial point in our preferred project setup? I would really like a discussion about the recommended way of organising bigger CDK apps/projects. This could hopefully lead to a new topic in developer guide.

@rix0rrr suggest a peerDependency solution for a similar problem here: https://github.com/aws/aws-cdk/commit/5e10b94aaaa87e6479e0bfcbe844d9fa68d0c1f5 but I fail to see how this would solve the problem. (please enlighten me if I missed something πŸ˜ƒ )

At the moment this is a show-stopper for our long-overdue upgrade of CDK

Reference

Environment

  • CDK CLI Version: 1.68.0 (build a6a3f46)
  • Module Version:
  • Node.js Version: v12.19.0
  • OS:
  • Language (Version): TypeScript 3.9.7

Other information

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
tobias-bardinocommented, Nov 3, 2020

I understand that CDK, as a project, does not want to fix the dependency problems with npm/lerna. Totally understandable.

Still, this will most likely become a problem lots of people will face in the future, when their CDK projects gets bigger and they need to rearrange their source code into separate projects.

@rix0rrr would it be interesting to have a section in the developer guide for organising bigger CDK projects? What is the best way to proceed with this?

2reactions
tobias-bardinocommented, Nov 3, 2020

https://github.com/aws/aws-cdk/pull/11113 will most likely fix this issue (still needs to be confirmed when CDK 1.72.0 is released)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting common AWS CDK issues
This topic describes how to troubleshoot the following issues with the AWS CDK.
Read more >
Getting Started with AWS CDK - Reflectoring
AWS Cloud Development Kit (CDK) is a framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation.
Read more >
AWS CDK Pipelines: Real-World Tips and Tricks (Part 2)
More useful tips and tricks when using AWS CDK Pipelines that go beyond the simple demos and which can be implemented in your...
Read more >
Create CDK project -.NET Application Modernization
It demonstrates a CDK app with an instance of a stack ... `dotnet build src` compile this app * `cdk ls` list all...
Read more >
A better way to structure AWS CDK projects around Nested ...
I just started breaking down a CDK app with a single core.Construct into multiple nested stacks. The nice thing about one single stack...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found