Skip to content

Enhance Update Logic for Individually Packaged Lambdas #9732

Closed

Description

@bojanraic

Use case description

I have a simple project with 3 functions which I am choosing to package individually.
On top of that, they all use a common layer for NodeJS dependencies.

When I deploy for the first time, 4 zips are generated (1 layer, 3 function zips).
Subsequently, when I update 1 of the 3 Lambda functions, 3 function zips are uploaded, 1 layer zip is skipped since it hasn't changed.
It would be great if the same kind of change detection that allows us to skip unmodified layers could be applied to functions as well.
So in this scenario, If only 1 lambda function is changed, then definitely its zip hash will have changed, but the other 2 functions would have remained the same (again, they are packaged individually). Meaning, ideally only one update should be detected and thus only one zip file uploaded for deployment.

There are hints at improving some of this logic in #6580, but I am not sure If this use case would be completely covered if some of the proposed changes mentioned there are implemented.

Would appreciate comments/feedback regarding this.

Activity

  1. pgrzesik commented on Jul 12, 2021

    @pgrzesik
    Contributor

    Hello @bojanraic, thanks for reporting. Could you please share a reproducible example? At the moment we're detecting if zip did no change and if that's the case it's not re-uploaded. Could you check if in fact the zips for your individually packed functions are not changing?

  2. bojanraic commented on Jul 12, 2021

    @bojanraic
    Author

    Hi @pgrzesik, thanks for the reply.
    I will include some code and log snippets from a test project a while back. Hope that will help.

    Serverless version: 2.46.0

    Change that triggered the deployment:

    Showing 1 changed file with 2 additions and 2 deletions.
    +2 −2 src/func-03/handler.js

    module.exports.main = async () => ({
         statusCode: 200,
         body: JSON.stringify(
           {
             message: "Hello from FuncThree!",
           },
           null,
           2
         ),
       });

    CI/CD Output

    Serverless: Packaging service...
    Serverless: Excluding development dependencies...
    Serverless: Excluding development dependencies...
    Serverless: Excluding development dependencies...
    Serverless: Excluding development dependencies...
    

    Here layer upload is skipped, but all 3 function zips are uploaded

    Serverless: Layer nodeModules is already uploaded.
    Serverless: Uploading CloudFormation file to S3...
    Serverless: Uploading artifacts...
    Serverless: Skip uploading nodeModules
    Serverless: Uploading service funcOne.zip file to S3 (944 B)...
    Serverless: Uploading service funcTwo.zip file to S3 (681 B)...
    Serverless: Uploading service funcThree.zip file to S3 (309 B)...
    

    And then in CFN, only FuncThree gets versioned, which is good.

    Serverless: Validating template...
    Serverless: Updating Stack...
    Serverless: Checking Stack update progress...
    CloudFormation - UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - sls-demo-dev
    CloudFormation - UPDATE_IN_PROGRESS - AWS::Lambda::Function - FuncOneLambdaFunction
    CloudFormation - UPDATE_IN_PROGRESS - AWS::Lambda::Function - FuncTwoLambdaFunction
    CloudFormation - UPDATE_IN_PROGRESS - AWS::Lambda::Function - FuncThreeLambdaFunction
    CloudFormation - UPDATE_COMPLETE - AWS::Lambda::Function - FuncOneLambdaFunction
    CloudFormation - UPDATE_COMPLETE - AWS::Lambda::Function - FuncThreeLambdaFunction
    CloudFormation - UPDATE_COMPLETE - AWS::Lambda::Function - FuncTwoLambdaFunction
    CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Version - FuncThreeLambdaVersionZSd0Yf2wHQViwjEdTHG0ZWBnJC6IGj198VEvxLd0C4U
    CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Version - FuncThreeLambdaVersionZSd0Yf2wHQViwjEdTHG0ZWBnJC6IGj198VEvxLd0C4U
    CloudFormation - CREATE_COMPLETE - AWS::Lambda::Version - FuncThreeLambdaVersionZSd0Yf2wHQViwjEdTHG0ZWBnJC6IGj198VEvxLd0C4U
    CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1623591786731
    CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1623591786731
    CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Deployment - ApiGatewayDeployment1623591786731
    CloudFormation - UPDATE_COMPLETE_CLEANUP_IN_PROGRESS - AWS::CloudFormation::Stack - sls-demo-dev
    CloudFormation - DELETE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1623591609436
    CloudFormation - DELETE_SKIPPED - AWS::Lambda::Version - FuncThreeLambdaVersione0nHIkpbiieiprlyryWe9vSaFg25Bjzxr8FCOu2e9U
    CloudFormation - DELETE_COMPLETE - AWS::ApiGateway::Deployment - ApiGatewayDeployment1623591609436
    CloudFormation - UPDATE_COMPLETE - AWS::CloudFormation::Stack - sls-demo-dev
    Serverless: Stack update finished...
    

    So the behavior of uploading packaged artifacts vs actually updating AWS resources is a bit counterintuitive.

    I would have expected 1 zip (funcThree.zip) to be uploaded, which would be in line with the actual code change (FuncThree being the only one that was changed, as confirmed by CFN versioning only FuncThree).

    I'm not sure if that's something that can be improved to be more consistent or if it is the intended behavior or some kind of restriction/limitation.
    Looking forward to hearing back from you.

  3. pgrzesik commented on Jul 12, 2021

    @pgrzesik
    Contributor

    Thanks for additional details @bojanraic 👍 Two additional questions:

    1. Are you using integration with Serverless Dashboard (do you have org and app configured in your config file)?
    2. Could you check the zip files and determine if only the one for funcThree in your case changed?

    I'll try to reproduce your problem and will report back with my findings.

  4. bojanraic commented on Jul 13, 2021

    @bojanraic
    Author

    Thanks for your reply. See my answers below.

    1. Are you using integration with Serverless Dashboard (do you have org and app configured in your config file)?

    No.

    1. Could you check the zip files and determine if only the one for funcThree in your case changed?

    My AWS Sandbox where I tested this is gone. I may not be able to get to setting it up in the next few days, but will keep you posted once I get a definitive confirmation which zips have actually changed.

    I can give you a ci/cd log snippet for a previous run, and only funcThree zip size is different.

    Serverless: Uploading service funcOne.zip file to S3 (944 B)...
    Serverless: Uploading service funcTwo.zip file to S3 (681 B)...
    Serverless: Uploading service funcThree.zip file to S3 (311 B)...
    

    Namely, 311 B vs 309 B in the subsequent commit/deployment for funcThree, whereas funcOne and funcTwo zip sizes did not change.

    If we suspect that 3 zips are uploaded because all 3 did indeed change, then how come only one function got a new version (based on CFN-related logs) and not all 3?

    Not sure how much the zip size comparison helps, but I will try to set it up again later this week, test it a bit more and compare zips more carefully to be more precise.

    Thanks.

  5. pgrzesik commented on Jul 14, 2021

    @pgrzesik
    Contributor

    Thanks a lot @bojanraic - I've managed to reproduce your exact issue and while we compare hashes of already uploaded files, including zip packages (

    async checkIfDeploymentIsNecessary(objects, funcLastModifiedDate) {
    ), it seems like we only skip uploading them when not a single one of them changed as during upload we do not compare the files anymore ( ). This is not the case for layers, as for layers we have smarter logic that checks if anything in layer changed:
    if (layerObject.artifactAlreadyUploaded) {

    I believe it would be great to not reupload packages that did not change at all by comparing remote hashes of zip packages beforehand, however, given that we have some work planned on changing how packaging logic works, I'm not sure if this is the best time to introduce smaller changes to the logic as this one. @medikoo What is your opinion here?

  6. medikoo commented on Jul 14, 2021

    @medikoo
    Contributor

    @pgrzesik I think the only reliable fix to that is addressing #8666 (and I consider this issue as duplicate of that)

    As not uploading some files will break our rollback feature, or do you see some way to do that in non-breaking way?

  7. bojanraic commented on Jul 14, 2021

    @bojanraic
    Author

    Thanks for confirming this, @pgrzesik!
    I've seen several issues discussing packaging/upload optimizations, but wanted to share this scenario and provide some logs/outputs.
    Please feel free to close it as a duplicate if you wish, and I will continue to keep an eye on the other related issue(s).
    Cheers!

  8. pgrzesik commented on Jul 15, 2021

    @pgrzesik
    Contributor

    Thanks a lot @medikoo - I did not find the related issue and it's indeed a duplicate of this one. As for the rollback - to be honest I was not aware of how that feature works in details but you're right that in current form the change I'm proposing is going to break it so that's a no-go.

    I think the approach outlined in the linked issue is probably the best way to go, however, if I'm not mistaken also require adjustments to how our current rollback feature works. I think we should continue further discussion in the #8666 so I'm going to close this one.

    Thanks once again @bojanraic for reporting 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions