aws-sam-cli
aws-sam-cli copied to clipboard
Stop Sam Local from rebuilding layers on every invoke
Currently it seems that sam local start-api will build any lambda layers on every request. This ends up creating a very slow dev experience.
Since sam build seems to be able to determine if a layer changes or not the local api behaviour not just assume the layer hasn't changed. If it does change sam build will rebuild it anyway?
it seems like passing the --warm-containers=EAGER as mentioned in this thread: https://github.com/Envek/aws-sam-typescript-layers-example/issues/11#issuecomment-1036057433 stops the layers from building each time but this isn't really a nice solution since a lot of times during testing I want to just test a single lambda I don't want to have to wait for all the lambda's to build layers again (each time I make a change to the code).
Is there another option I'm missing?
Thanks for bringing this to our attention! Tagging this as a feature request because it seems like there's a couple of ways to not build on every invoke (using eager warm containers or publishing the layer and referencing the ARN: https://github.com/aws/aws-sam-cli/issues/3725). To make sure I understand, is this feature request for only building layers when changes are detected?
@torresxb1 - yes that's correct a request to only rebuild layers when changes are detected to the layer. At least in my workflow the layers change very infrequently compared to the lambda source (and it by far takes the longest to build) so rebuilding each time really slows down the dev experience.
You're correct there are workaround as mentioned but both of them have downsides I tried to call out. So I think it would greatly help speed local dev (when using layers) if this was improved.
Finally, and this may just be my lack of understanding but why do they layers have to be rebuilt at all by the running process? When we make code changes to the lambda we need to run sam build for the changes to take effect. build seems to know already when the layer changes so why does the running container decide to rebuild the layers?
Thanks!
Does using eager warm containers actually work? I just tried the switch and get errors.
+1
+1
--warm-containers=EAGER doesn't really do the job, as soon as the function code is changed from that point onwards the layer is built on each request.
This is indeed very annoying. I have my node_modules in a layer and I rarely make any change to them but I have to wait a very long time on each lambda source code change to invoke it.
The only workaround that work for me is to deploy the stack and use a fixed arn reference for that layer. But then if I want to add a new dependency i need to do a deploy just for that... :/
+1
+1
+1
@torresxb1 any updates on this or if it can be implemented. It seems this is effecting quite a few devs. It really slows down process.
just came across this. current behaviour sucks. +1
@praneetap - is there a list of features somewhere being worked by sam accelerate team? This feature seems to be pretty highly requested and it is brutal to use sam local for dev if the layers keep rebuilding. Nearly all the issues i find related to sam and slowness is related to inefficient layer handling.
+1
+1
sam local start-api --debug --warm-containers LAZY --skip-pull-image
It resolved the issue