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

feat(lambda-layer-awscli): Dynamically load asset for AwsCliLayer, with bundled fallback #21938

Closed

Conversation

madeline-k
Copy link
Contributor

@madeline-k madeline-k commented Sep 7, 2022

This PR introduces a change to dynamically load the large Asset zip file used by the AwsCliLayer construct from a separate package: https://www.npmjs.com/package/@aws-cdk/asset-awscli-v1/. This change is part of aws/aws-cdk-rfcs#430

This may not work in some customer's environments, so we will release this feature in two steps.

  1. This PR, which adds all of the dynamic-loading logic, but bundles the separate @aws-cdk/asset-awscli-v1 package into aws-cdk-lib to use as a fallback for customers whose build environments are not able to install from npm. For customers who will experience a breaking change when that fallback is removed, warnings and CLI notices will be printed for them linking to instructions to update and prepare for the version without the fallback. The first phase will not significantly affect the size of the aws-cdk-lib package.
  2. Remove the fallback, and actually reduce the size of aws-cdk-lib.

Remaining:

  • test that verify no-network and jsii-target lang scenarios - In Progress by @kaizencc
  • Add language-specific instructions for using explicit dependency as a workaround

When this is released, we will also create a CLI Notice. The PR for that notice is open here: cdklabs/aws-cdk-notices#21

The github issue will be here: #22470


All Submissions:

Adding new Unconventional Dependencies:

  • This PR adds new unconventional dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link

gitpod-io bot commented Sep 7, 2022

@madeline-k madeline-k marked this pull request as draft September 7, 2022 04:56
@github-actions github-actions bot added the p2 label Sep 7, 2022
@aws-cdk-automation aws-cdk-automation requested a review from a team September 7, 2022 04:56
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Sep 7, 2022
@madeline-k madeline-k changed the title feat: Dynamically load asset for AwsCliLayer, with bundled fallback feat(lambda-layer-awscli): Dynamically load asset for AwsCliLayer, with bundled fallback Sep 7, 2022
@madeline-k madeline-k changed the base branch from madeline-k/lambda/fromAssetConstruct to main September 20, 2022 16:20
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The Pull Request Linter fails with the following errors:

❌ Features must contain a change to a README file.
❌ Features must contain a change to an integration test file and the resulting snapshot.

PRs must pass status checks before we can provide a meaningful review.

@aws-cdk-automation aws-cdk-automation dismissed their stale review September 27, 2022 16:49

Pull Request updated. Dissmissing previous PRLinter Review.

Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The Pull Request Linter fails with the following errors:

❌ Features must contain a change to a README file.
❌ Features must contain a change to an integration test file and the resulting snapshot.
❌ CLI code has changed. A maintainer must run the code through the testing pipeline (git fetch origin pull/21938/head && git push -f origin FETCH_HEAD:test-main-pipeline), then add the 'pr-linter/cli-integ-tested' label when the pipeline succeeds.

PRs must pass status checks before we can provide a meaningful review.

@madeline-k madeline-k added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Sep 27, 2022
@madeline-k
Copy link
Contributor Author

This change is exempt integ test changes because one hard requirement is that it does not change the behavior or content of existing stacks.

@aws-cdk-automation aws-cdk-automation dismissed their stale review September 27, 2022 16:53

Pull Request updated. Dissmissing previous PRLinter Review.

Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The Pull Request Linter fails with the following errors:

❌ Features must contain a change to a README file.
❌ CLI code has changed. A maintainer must run the code through the testing pipeline (git fetch origin pull/21938/head && git push -f origin FETCH_HEAD:test-main-pipeline), then add the 'pr-linter/cli-integ-tested' label when the pipeline succeeds.

PRs must pass status checks before we can provide a meaningful review.

@aws-cdk-automation aws-cdk-automation dismissed their stale review September 27, 2022 16:54

Pull Request updated. Dissmissing previous PRLinter Review.

Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The Pull Request Linter fails with the following errors:

❌ Features must contain a change to a README file.
❌ CLI code has changed. A maintainer must run the code through the testing pipeline (git fetch origin pull/21938/head && git push -f origin FETCH_HEAD:test-main-pipeline), then add the 'pr-linter/cli-integ-tested' label when the pipeline succeeds.

PRs must pass status checks before we can provide a meaningful review.

packages/@aws-cdk/lambda-layer-awscli/lib/awscli-layer.ts Outdated Show resolved Hide resolved
Comment on lines 116 to 120
if (assetPackage) {
// ask for feedback here
const asset = new assetPackage.AwsCliAsset(scope, `${id}-asset`);
code = lambda.Code.fromBucket(asset.bucket, asset.s3ObjectKey);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It all happens at runtime in JavaScript, which doesn't have types. So: we can check for the presence of the symbol, but never its type.

if (!(assetPackage as any).AwsCliAsset) {
  throw new Error('Ruh roh');
}

This is probably good enough, though we might need to stick an API version somewhere...

packages/@aws-cdk/lambda-layer-awscli/lib/awscli-layer.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/lambda-layer-awscli/lib/awscli-layer.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/lambda-layer-awscli/lib/awscli-layer.ts Outdated Show resolved Hide resolved
}
availableVersion = AwsCliLayer.requireWrapper(path.join(assetPackagePath, '../../package.json'), logs).version;

if (targetVersion === availableVersion) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we care exactly? Do we need semver in here to check range compatibility?

Copy link
Contributor

Choose a reason for hiding this comment

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

Needs to log if the version is there but we rejected it btw. That's good info to know.

packages/@aws-cdk/lambda-layer-awscli/lib/awscli-layer.ts Outdated Show resolved Hide resolved
@madeline-k madeline-k requested a review from a team October 12, 2022 15:44
Comment on lines +74 to +75
"bundledDependencies": [
"semver"
Copy link
Contributor Author

@madeline-k madeline-k Oct 12, 2022

Choose a reason for hiding this comment

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

I'm not sure I did this correctly... Adding the non-JSII package as a dependency forced me to add it here as well. And then having a bundled dependency led me to add it to the "nohoist" list in our top-level package.json using yarn build --fix. It's strange because semver is already a dependency of aws-cdk-lib, so this will get ignored in the release anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Potentially, we could add a utility function in @aws-cdk/core that does the version range check and uses the semver bundled there. Just available to TS/JS, nothing else would be good enough to start. Something like:

export function versionMatchesRange(version: string, range: string) {
  // Use semver here
}

@aws-cdk-automation
Copy link
Collaborator

The pull request linter fails with the following errors:

❌ CLI code has changed. A maintainer must run the code through the testing pipeline (git fetch origin pull/21938/head && git push -f origin FETCH_HEAD:test-main-pipeline), then add the 'pr-linter/cli-integ-tested' label when the pipeline succeeds.

PRs must pass status checks before we can provide a meaningful review.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: e97124c
  • Result: FAILED
  • Build Logs (available for 30 days)

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

cp $dir/lib/layer.zip ./lib

target_version=$(node -pe 'require("@aws-cdk/asset-awscli-v1/package.json").version')
echo "export const TARGET_VERSION = '${target_version}';" > lib/asset-package-version.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo "export const TARGET_VERSION = '${target_version}';" > lib/asset-package-version.ts
echo "export const RECOMMENDED_VERSION = '${target_version}';" > lib/asset-package-version.ts

?

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Modulo tiny things I think this looks good!

}

if (fallback) {
Annotations.of(this).addWarning(`[ACTION REQUIRED] Your CDK application is using ${this.constructor.name}. Add a dependency on ${AwsCliLayer.assetPackageName}, or the equivalent in your language, to remove this warning. See https://github.com/aws/aws-cdk/issues/22470 for more information.`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Annotations.of(this).addWarning(`[ACTION REQUIRED] Your CDK application is using ${this.constructor.name}. Add a dependency on ${AwsCliLayer.assetPackageName}, or the equivalent in your language, to remove this warning. See https://github.com/aws/aws-cdk/issues/22470 for more information.`);
Annotations.of(this).addWarning(`[ACTION REQUIRED] Your CDK application is using ${this.constructor.name} and synthesizes in an environment where ${AwsCliLayer.assetPackageName} cannot be automatically downloaded. Add a dependency on ${AwsCliLayer.assetPackageName}, or the equivalent in your language to remove this warning. See https://github.com/aws/aws-cdk/issues/22470 for more information.`);

@corymhall
Copy link
Contributor

@madeline-k / @kaizencc should this be closed?

@kaizencc kaizencc marked this pull request as draft December 6, 2022 20:25
@kaizencc
Copy link
Contributor

kaizencc commented Dec 6, 2022

Closing for now. @madeline-k had thoughts to poc a different solution, but can revive it later.

@kaizencc kaizencc closed this Dec 6, 2022
@RomainMuller RomainMuller deleted the madeline-k/reduce-package-size/lambda-layer-awscli branch July 4, 2023 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS. p2 pr/do-not-merge This PR should not be merged at this time. pr-linter/exempt-integ-test The PR linter will not require integ test changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants