Create custom log for AWS lambda function.
Logging is an important part of an application development. A log file helps a developer to identify any sort of errors exist in an application and also, we can use log for debugging and to know consistency of an application as well.
Today we will discuss how we can create log for difference purpose in AWS serverless service like lambda. To do so we will follow 3 steps.
In this articles we will follow 3 steps.
1) Create IAM role for lambda function.
2) Create lambda function and create test event.
3) Implement python logging module and see log in cloud watch.
1) Create IAM role for lambda function.
Before creating a lambda function, at first, we will create an Identity and Access Management (IAM) role for our lambda function. In the search bar of AWS, we can search by keyword “IAM”, on the search result click on “IAM” to navigate to IAM dashboard.
On the left side of IAM dashboard, we will get roles link. By clicking on roles, we will get a button called create role. After clicking on create role, we are now able to create our role. For today’s example I will create a role for AWS service and I will select use cases Lambda.
Now we can click on next permission that will bring us policies add options. Now we will filter a policy called AWSLambdaBasicExecutionRole , we will select it and click on next tags button. Add tags is optional, so we can skip and click on button call next review button. In review section we now give our role name. For an example I have added role name is ‘logging-example-role’.
2) Create lambda function and create test event.
Now we will go in AWS lambda to create a Lambda function.
In order to create lambda function, we have to select this lambda link, then a create function button will appear and we have to tap on it. Before tapping the button, we will provide basic information.
After creating function, we will get a lambda_function.py file which is generated by AWS platform.
Now we will test this function. For that please click on test button, after clicking on test button we will get a panel to create the test event. We are able to create up to 10 test events. Setting an event name we can set a key string and create a test event like.
If we again click on test button we will get a response like below.
You may noticed with response function logs also generated. To see this log in CloudWatch , Lets click on Monitor and then click on the button called view logs in CloudWatch .
After go in cloud watch log we will see a log streams has create for out login test function.
After click on this log stream link we will get log message which has generated by AWS platform.
3) Implement python logging module and see log in cloud watch.
Now question is how we can push our own log message in cloud watch for different purposes?
For that we will use python module called logging. Logging provides a set of convenience functions for simple logging usage. These are debug(), info(), warning(), error() and critical(). Let execute logging module in our existing python function.
import json import logging logger = logging.getLogger() logger.setLevel(logging.INFO) def lambda_handler(event, context): logger.info("This is a info log message") # TODO implement return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') }
In the above code, at first, we have imported logging. Note that Loggers should NEVER be instantiated directly, but always through the module-level function logging.getLogger(), for that we have written 3rd line.
In 4th line we have setLevel. There has below levels in logging.
CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET.
I have used info() function in python function to generate a message for application info if we need in log. Let deploy our code then test again and see cloud watch log again.
by this time we will get a log message like below
Now, if we need debug function we can change our level
logger.setLevel(logging.DEBUG) Now if we need both info and debug we can set level NOTSET.import json import logging logger = logging.getLogger() logger.setLevel(logging.NOTSET) def lambda_handler(event, context): logger.debug("debug for check") logger.info("info for check") # TODO implement return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') } After test you will get below log message
In cloud watch log
That’s it. For more details about logging please read python documentation.