Repository navigation
Faster deployment by skipping unchanged lambda layers - #7680
Conversation
|
EDIT: I finally figured out how to stub correctly in 981c8c4
|
|
@medikoo I added |
medikoo
left a comment
There was a problem hiding this comment.
@exoego that looks great! I've proposed just few minor, mostly stylistic changes, after that I believe we'll be ready to go
I added enhancement and cat/dx label instead of refactoring, since this will redyce deployment time and therefore enhance developer experimences with Lambda layer.
Feel free to change labels if my suggestion does not fit to labeling policy.
Yes, I'm still coining right meaning for them. In general idea was thatenhancement should be about some new small feature, config option etc. and anything that doesn't change anything on that level should be considered refactor.
Still I agree, that in this case, there's a visible impact for user, as performance is visibly improved, so it deserves to be counted as enhancement (while refactor is usually swept away under carpet, as something minor and not having direct effect for user).
I propose to mark it with perf (just introduced) label, which also will be pushed with perf commit message scope and mentioned in version changelog.
I believe all this label organization rules should be common effort, so let me know what you think.
| const layerNames = this.serverless.service.getAllLayers(); | ||
| artifactFilePaths = artifactFilePaths.concat( | ||
| _.map(layerNames, name => { | ||
| _.flatMap(layerNames, name => { |
There was a problem hiding this comment.
Let's minimize lodash dependency (resort only when there's no convenient native counterpart). I think this case can be addresed by .map().filter(Boolean)
| 'Wild' | ||
| )}LambdaLayerPermission`; | ||
| }, | ||
| getLambdaLayerArtifactPath(packagePath, layerName) { |
There was a problem hiding this comment.
I think util this doesn't fit well naming.js, which is more about name generation for resource and other entities.
Probably best location for it is lib/plugins/aws/utils
There was a problem hiding this comment.
Oops, forgot to add new file. Fixed in bb0e5d3
| this.packagePath, | ||
| layerName | ||
| ); | ||
| const layerArtifactBinary = fs.readFileSync(layerArtifactPath); |
There was a problem hiding this comment.
Let's not use sync counterparts of async functions, they block process (we already have some perf issues cause of that)
| return this.provider | ||
| .request('CloudFormation', 'describeStacks', { StackName: stackName }) | ||
| .then(data => { | ||
| const lastHash = _.find(data.Stacks[0].Outputs, { |
There was a problem hiding this comment.
Do you mean native Array#find method ?
Outputs is object, which does not have find method, I think.
There was a problem hiding this comment.
I believe Outputs is an array of objects, also docs state that if I see correctly: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFormation.html#describeStacks-property
There was a problem hiding this comment.
Ahh, you are right !!
I was seeing Outputs in CloudFormation template, which is plain object.
| const lastHash = _.find(data.Stacks[0].Outputs, { | ||
| OutputKey: layerHashOutputLogicalId, | ||
| }); | ||
| const compiledCloudFormationTemplate = this.serverless.service.provider |
There was a problem hiding this comment.
style nit: We can exit here if lastHash is not found
| const layerS3keyOutputLogicalId = this.provider.naming.getLambdaLayerS3KeyOutputLogicalId( | ||
| layerName | ||
| ); | ||
| const lastS3Key = _.find(data.Stacks[0].Outputs, { |
| this.serverless.cli.log(`Layer ${layerName} is already uploaded.`); | ||
| } | ||
| }) | ||
| .catch(e => { |
There was a problem hiding this comment.
Let's put it as second callback to previous then, otherwise we're catching here also error eventually coming from first then callback and that is unwanted
| }) | ||
| .catch(e => { | ||
| if (e.message.indexOf('does not exist') > -1) { | ||
| return BbPromise.resolve(); |
There was a problem hiding this comment.
Let's do just return (it's equivalent and noise free)
| compileLayers() { | ||
| const allLayers = this.serverless.service.getAllLayers(); | ||
| return BbPromise.each(allLayers, layerName => this.compileLayer(layerName)); | ||
| return BbPromise.each(allLayers, layerName => { |
There was a problem hiding this comment.
Let's not rely on BbPromise functions that do not have native counterparts (we plan dropping bluebird dependency, as soon as we drop support for Node.js v6, with v2)
|
@exoego is this ready to take? If so, please re-request review. Thank you! |
|
Yes, ready to re-review. |
| return layerObject.package.artifact; | ||
| if (layerObject.artifactAlreadyUploaded) { | ||
| this.serverless.cli.log(`Skip uploading ${name}`); | ||
| return ''; |
There was a problem hiding this comment.
If we don't want to return anything, but ESLint complains let's return null
| this.provider.serverless.service, | ||
| this.provider.naming | ||
| ); | ||
| }).filter(name => name !== '') |
| this.serverless.cli.log(`Layer ${layerName} is already uploaded.`); | ||
| }, | ||
| e => { | ||
| if (e.message.indexOf('does not exist') > -1) { |
There was a problem hiding this comment.
Let's rely on str.includes instead
Codecov Report
@@ Coverage Diff @@
## master #7680 +/- ##
==========================================
+ Coverage 87.99% 88.00% +0.01%
==========================================
Files 244 245 +1
Lines 9209 9254 +45
==========================================
+ Hits 8103 8144 +41
- Misses 1106 1110 +4
Continue to review full report at Codecov.
|
|
@medikoo Ready to re-review. |
What did you implement
Closes #7516
This PR allows serverless framework to skip uploading unmodified layers, then use the previously-deployed existing layers.
Deployment time will be reduced, thanks to avoid unnecessary upload and creating new layer version.

This chart shows my experiment of deployment time reduction.
Skipping is based on the SHA1 hash value of the layer zip.
The hash value and S3 key of the previously-uploaded layer zip will be exposed in CloudFormation stack output, like below example:
Since hash and S3Key of previous deployment is exposed and shared, all team members gains deployment time boost: if one developer in a team deployed the layer, then another developer can skip uploading when the layer is not changed.
How can we verify it
npm install -g exoego/serverless#skip-layersserverless.yamlandlayer-dir/nodejs/package.jsonis belowand
{ "dependencies": { "aws-sdk": "^2.672.0", "elasticsearch": "^16.7.1", "node-fetch": "2.6.0", "sharp": "^0.25.2" } }time sls deployhandler.jsa bit (if no update, sls skip the entire deployment`).time sls deployTodos
Useful Scripts
npm run test:ci--> Run all validation checks on proposed changesnpm run lint:updated--> Lint all the updated filesnpm run lint:fix--> Automatically fix lint problems (if possible)npm run prettier-check:updated--> Check if updated files adhere to Prettier confignpm run prettify:updated--> Prettify all the updated filesIs this ready for review?: YES
Is it a breaking change?: NO