Working effectively with AWS Lambda - Logs
AWS Lambda is one of the principal serverless platform.
It does not need many introductions, but in short allows to execute code without operating any server. Hence, the serverless name.
It is one of my favourite platform to develop software and I often suggest it as possible solution whenever I am consulting with clients.
While AWS Lambda is a great platform to operate software, I find that it could improve as a platform to develop software on.
Indeed, whenever a new project starts and AWS Lambda is chosen as computing platform, my first though is not about how to deploy the code but how to create a local environment so that engineers can iterate quickly in local machines.
Sometimes, however it is inevitable to iterate over code using a live AWS Lambda.
It might be because it is just short iteration, and it is not worth to spin up a local environment.
Or maybe the Lambda is running against a webhook that is very cumbersome to replicate locally.
Whatever the case, the simplest way to iterate and debug code in AWS Lambda is using logs.
By default, logs from AWS Lambda are sent to Cloudwatch.
Unfortunately, reading the logs from CloudWatch using the AWS Console is usually slow and cumbersome. Sometimes I had to wait even up to 5 minutes to actually get the logs show up in the AWS Console.
A better solution is to use the AWS Cli.
Surprisingly, this is not mentioned in the official AWS documentation.
Once you have configured your cli with:aws configure —profile $account_name
streaming the logs from AWS Lambda is rather simple.
You will need to know the log group associated with your lambda function, this can be checked in the AWS Cloudwatch console or constructed following the pattern:/aws/lambda/$FunctionName
Hence, if your Lambda is called FooBarEntrypoint
the log group will be /aws/lambda/FooBarEntrypoint
The last step it is to stream the logs of your function to your terminal:
aws logs tail /aws/lambda/$function_name \
--profile $account_name \
--follow
This command will start streaming all the logs in your terminal.
It should not be used against functions that are under high load as it can easily overload.
The logs are still not as fast and immediate as if it was a production environment, there are still multiple layers that the data needs to go through before reaching the console. But they are definitely faster than using the console and provide a much better experience.