-
Notifications
You must be signed in to change notification settings - Fork 4.3k
chore(lambda): throw a relevant error message when asset is used across stacks #12420
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
Conversation
Although the constructor of the AssetCode doesn't imply it's real dependencies, an AssetCode currently can only be associated to consumers within a single stack. Attempting to use them across stacks results in the conspicuous error - "Cannot reference across apps. Consuming and producing stacks must be defined within the same CDK app." The correct solution here would have been to model this such that the stack dependency is made explicit on the customer facing API, perhaps by taking a 'scope' parameter. This solves the problem by caching the bound asset per stack.
| // If the same AssetCode is used multiple times, retain only the first instantiation. | ||
| if (!this.asset) { | ||
| this.asset = new s3_assets.Asset(scope, 'Code', { | ||
| const stackPath = cdk.Stack.of(scope).node.path; // use path instead of name to account for Stage. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this good enough? Wouldn't the same stack path in multiple Apps still give the same error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha well. This change DOES address the issue it's purporting to address (use same code in multiple stacks inside the same app), but it does NOT address the error you mentioned, and would still fail if you reused a single Code object among multiple unit tests (each of them having a distinct App).
Cannot reference across apps. Consuming and producing stacks must be defined within the same CDK app.
| export class AssetCode extends Code { | ||
| public readonly isInline = false; | ||
| private asset?: s3_assets.Asset; | ||
| private assets: Record<string, s3_assets.Asset> = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not make this a Map<Stack, s3_assets.Asset>?
Simpler and more direct.
|
@rix0rrr - I've changed my mind on this. Something feels wrong in doing this cache and allowing an asset to be used anywhere (any Instead, I've updated the code to throw a more relevant error message instead of the conspicuous one. |
eladb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
|
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
Although the constructor of the AssetCode doesn't imply it's real
dependencies, an AssetCode currently can only be associated to consumers
within a single stack.
Attempting to use them across stacks results in the conspicuous error -
"Cannot reference across apps. Consuming and producing stacks must be
defined within the same CDK app."
The correct solution here would have been to model this such that the
stack dependency is made explicit on the customer facing API, perhaps by
taking a 'scope' parameter.
For the moment, throw a more relevant error message so that it's obvious to
the user on what is wrong.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license