Quick serverless && lambda debugging tip

TLDR;

How do you quickly and easily check the logs being spit out of your lambda without constantly going to CloudWatch UI Console?

Simply use the terminal and run this command:

serverless logs -f YOUR_LAMBDA_FUNCTION_NAME -s YOUR_ENVIRONMENT --startTime 3h --tail

Every new log will be appended in your terminal.

Long explanation

Well there is not much to explain in that command.

TAIL is a common command to monitor data streams and display new information as it is written.
Of course AWS CloudWatch has it, and of course Serverless Framework wraps the functionality to our benefit.

Why am i telling you something so simple?

Just dropping this quick tip here as a reminder that even the most obvious things you do every day, might not be known by other team mates, doesn't matter if less or more experienced than us.

Some time ago, we had a weird bug on one of our application and I joined a couple of colleagues investigating the issue.

As we were (remotely) pair programming/debugging I noticed a rather tedious and slow approach at checking the logs of our Lambdas that consisted in manually checking the Log Groups of that Lambda on CloudWatch using the AWS UI console.
Every time the Lambda was invoked, the developer was clicking around in the console, finding the log group and scrolling to the logs

Nothing bad with it, I just find it boring and slow. I hate clicking around, and my wrist hates it too. That's why I rather use the terminal and this simple command:

serverless logs -f YOUR_LAMBDA_FUNCTION_NAME -s YOUR_ENVIRONMENT --startTime 3h --tail

(this assumes that you are using serverless, but something similar exists if you just want to directly use AWS CLI)

This command, and this way of debugging the logs of the lambda I am working on is routine for me, but apparently was not known by my colleague. As soon as I shared it, he was more than happy, and grateful, to start using it.

Sharing is caring

I never thought necessary to publicly advertise this little trick in my team, but I was wrong, and as soon as we had our weekly "Learnings of the week" Lightning talks session, it was clear that something so simple, was indeed not so known, and by sharing it, it made our work easier and faster.

We work in a field that is constantly changing, we work on different projects, different stacks, we are different kind of person and therefore different kind of developers ( some like UI tools, some like more the terminal).

  • Don't be shy in sharing your knowledge.
  • Don't be ashamed if you don't know something
  • Don't be afraid of asking.
  • Don't keep your learning for yourself to gain advantage on others, we work in a team and we grow as a team.
  • Never assume what you might find trivial is known by others.

Hope it helps

17