Skip to content

Conversation

@nija-at
Copy link
Contributor

@nija-at nija-at commented Jan 8, 2021

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

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.
@nija-at nija-at requested a review from a team January 8, 2021 10:44
@nija-at nija-at self-assigned this Jan 8, 2021
@gitpod-io
Copy link

gitpod-io bot commented Jan 8, 2021

@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Jan 8, 2021
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Jan 8, 2021
@nija-at nija-at added the pr/do-not-merge This PR should not be merged at this time. label Jan 8, 2021
// 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.
Copy link
Contributor

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?

Copy link
Contributor

@rix0rrr rix0rrr Jan 8, 2021

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> = {};
Copy link
Contributor

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.

@nija-at nija-at added pr-linter/exempt-readme The PR linter will not require README changes and removed pr/do-not-merge This PR should not be merged at this time. labels Jan 8, 2021
@nija-at nija-at changed the title feat(lambda): AssetCode instance can be used in multiple stacks chore(lambda): throw a relevant error message when asset is used across stacks Jan 11, 2021
@nija-at nija-at removed the pr-linter/exempt-readme The PR linter will not require README changes label Jan 11, 2021
@nija-at
Copy link
Contributor Author

nija-at commented Jan 11, 2021

@rix0rrr - I've changed my mind on this. Something feels wrong in doing this cache and allowing an asset to be used anywhere (any App, any Stack). It feels like it might come back to bite me.

Instead, I've updated the code to throw a more relevant error message instead of the conspicuous one.

@nija-at nija-at requested review from a team and rix0rrr January 11, 2021 11:26
@rix0rrr rix0rrr added the pr/do-not-merge This PR should not be merged at this time. label Jan 11, 2021
Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nija-at nija-at removed the pr/do-not-merge This PR should not be merged at this time. label Jan 11, 2021
@mergify
Copy link
Contributor

mergify bot commented Jan 11, 2021

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-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: b4c2de5
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Jan 11, 2021

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).

@mergify mergify bot merged commit cc745f8 into master Jan 11, 2021
@mergify mergify bot deleted the nija-at/lambda-asset branch January 11, 2021 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

@aws-cdk/aws-lambda Related to AWS Lambda contribution/core This is a PR that came from AWS.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants