When using serverless framework to build our applications , we depend on the framework's capability to achieve our goals.
One of the main parts of the work process is the ability to debug your code while you build your logic and in case there are some disruptions in the flow.
When working with AWS Lambda and building our application with SST framework , we can add logs around the logic of our Lambda , but also we need to bare in mind that this constant build up of logs could overwhelm AWS CloudWatch logs and increase our bills.
The solution we have adopted is the following:
There are two essentials steps to be completed.
Create custom log method in your code base
Add environment variable to the lambda you are interested to debug
Lets code:
Basically you would have a custom log logic in your codebase, which will be invoked in places.
function Log() {
if (POWERTOOLS_LOG_LEVEL === 'DEBUG') {
}
}
Then in your Lambda Function snippet you would have environmental variable POWERTOOLS_LOG_LEVEL with value INFO.
new Function(stack, "MyFunction", {
handler: "src/lambda.handler",
timeout: 10,
environment: {
POWERTOOLS_LOG_LEVEL: "INFO",
},
});
The reason is - you dont want to overwhelm AWS CloudWatch, with a constantly invoked debug logic.
Like that , whenever you need to debug you would need to change via the AWS Console ( no need of deploy) the value of your POWERTOOLS_LOG_LEVEL environmental variable to DEBUG and voila on a next Lambda invocation you would have beautiful logs on AWS CloudWatch.
Thank you for reading!
Excited to see any feedback!
Have an amazing journey in cloud world!
Top comments (0)