One of the biggest pain points we hear from developers moving into the serverless world, is the slower feedback loop while developing.
In this post, I'm going to walk through some of the ways I speed up building and deploying live serverless services.
The serverless framework has a ton of hidden gems to speed up your dev cycle.
sls deploy function
Many users aren't aware of the
serverless deploy function command and instead use
serverless deploy each time their code has changed.
What is the difference?
sls deploy re-deploys the entire stack through cloud formation and can be noticeably slooowww.
On the flip side,
sls deploy function only zips up the code (& any dependencies) and updates the lambda function only. This is much much faster than waiting for an entire stack update.
So, when developing you can use
sls deploy function -f myFuncName for speedier code changes in your live AWS account.
One note: If you make any changes to
serverless.yml config, like changing endpoint paths or updating custom resources, etc that will require a full
sls deploy to update the entire stack.
If you have only make changes to your function code,
sls deploy function -f myFuncName will work just fine and be much snappier.
# Redeploy entire stack through cloud formation sls deploy # Redeploy only the code + dependencies to update the AWS lambda function sls deploy function -f myFuncName
Debugging remote code in the serverless world can be tricky. Luckily
console.log and lambda functions go together like peanut butter and jelly.
One of my favorite serverless framework commands is the
sls logs command. It will pull the logs from your remote function directly into the terminal.
This is handy for debugging errors or inspecting what the
context args contain.
It's even more useful when you tail the logs and get a live update as you are pinging your live functions.
# View logs of myFuncName and tail via -t flag serverless logs -f myFuncName -t
I will pop open a new terminal window and run
sls logs -f funcName -t and then ping my lambda function with Postman (or my UI) and live debug the function.
Having 2 terminal windows open and combining
sls deploy function -f funcName for quicker code changes and watching the live logs with
sls logs -f funcName -t is super easy to do and speeds up my feedback loop.
Now you might be asking, what about offline emulation? It's absolutely a way to speed up dev cycles without having to re-deploy anything.
With the serverless offline plugin you can speed up local dev is by emulating AWS lambda and API Gateway locally when developing your Serverless project.
Install the plugin
npm install serverless-offline --save-dev
Then add the
plugins key in
plugins: - serverless-offline
Now you should have the
serverless offline commands available when running
Serverless has a lot of useful plugins to test code locally before deploying to a remote environment. This helps developers save time of unnecessary deploys.
PS! How do you speed up your dev flow?!
David Wells is a developer at Serverless, Inc.