Repository navigation
Fix lambda version generation when only function config changes - #4510
Merged
Merged
Conversation
HyperBrain
commented
Dec 1, 2017
|
|
||
| // Read the file in chunks and add them to the hash (saves memory and performance) | ||
| return BbPromise.fromCallback(cb => { | ||
| fs.readFile(artifactFilePath, cb); |
Contributor
Author
There was a problem hiding this comment.
To myself: Switch to chunked reading as commented.
horike37
approved these changes
Dec 2, 2017
horike37
left a comment
Contributor
There was a problem hiding this comment.
That's a fantastic work 🎉 Thanks @HyperBrain
I tested locally and see creating a new version even if an only configuration of Lambda is changed.
LGTM 💯
RafalWilinski
approved these changes
Dec 3, 2017
RafalWilinski
left a comment
Contributor
There was a problem hiding this comment.
Works like a charm! 🎉 LGTM
Contributor
Author
|
Thanks for double checking and reviewing. I'll merge it now. |
|
Great job @HyperBrain. I have a question not 100% related to your pr. How can I get the version attribute from !GetAtt ApiLambdaVersion${hash}.Version |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What did you implement:
There is a bug in the lambda version creation. Currently only the ZIP file hash is used to
create the logical id for the lambda version resource.
If you just change the function configuration (e.g. memory size, VPC or an environment
variable), the version resource created in the CF template will have the same logical id as before.
This leads to a bug, because if you now deploy, the function is deployed as $LATEST, but no
new version is created, because CF will not create one as the logical id of the version resource
is the same as before.
Only if you do source changes, a new version will be created.
This behavior is wrong. Also any change in the function configuration creates a differently
behaving deployment and thus must lead to a new function version created.
The bug gets visible and breaks your deployments, if you rely on the last version instead
of $LATEST (e.g. if you use the alias plugin, tag the latest versions or use the versions to
rollback).
How did you implement it:
I added the function configuration to the hash used for generating the logical id
of the function version resource -> H(zip + config). With that change a new
version is now correctly created as soon as (1) the source (zip) changed and (2)
the function configuration changed.
Additionally I switched to reading the ZIP asynchronously (we should not use synchronous functions at all) in chunks to comply with Node so that the Node event loop is not unnecessarily stalled during this lengthy operation. This also lowers the memory footprint to a minimum and is much faster - to generate the hashes it is unnecessary to read the whole file at once!
Example:
A deployment creates this version:
After change of an environment variable in the function configuration. Notice, that
the ZIP file hash did not change:
As you see, although the code hash (zip file) stays the same, the version
resource id changed, what means, that a new version will be created now. This
is fully correct, because the last version that would have been in there before
the fix would have been substantially different than, the real last deployed
function. If you'd have tried to restore that one you're system would have been
broken badly.
Now the last Lambda version will resemble the last changed version. If you deploy
without any changes, of course no new versions will be created. That's exactly the
expected behavior.
How can we verify it:
Deploy any service. Open one of the service's functions in the AWS Lambda console and check the generated version (it should be of the same configuration as the $LATEST version).
Now change something in the configuration of the function, but leave its sources untouched.
Redeploy -> CF should now create a new version including the changed configuration. The version
should be shown in the AWS console.
I tested it with multiple of our complex projects and the version creation worked as expected.
Todos:
Is this ready for review?: YES
Is it a breaking change?: NO