How to test the AWS Lambda function in Python locally?
Python in AWS Lambda is awesome! But, as a developer you want to make local development workflow as seamless as possible. One thing we want to do was to test the lambda locally right from Visual Studio Code rather than using Lambda console.
Prerequisites:
- AWS Lambda function in Python 3.7
- Visual Studio Code
Here are the implementation steps.
Running lambda locally:
To run lambda locally we can use python-lambda-local package, which supports all the latest versions up to Python 3.8. Install the package by running
pip install python-lambda-local
Test if it works by going to your project directory and running
python-lambda-local -f lambda_handler lambda_function.py event.json
where
- lambda_handler is the name of your handler function
- lambda_function.py is the name of your file with Python code
- event.json is the test event data
The script will provide the exact same output as the test feature in the Lambda console. An alternate better approach to doing this is using AWS SAM Local. AWS SAM Local is a CLI tool that allows you to locally test and debug your AWS Lambda functions defined by AWS Serverless Application Model (SAM) templates. Today, SAM Local supports Lambda functions written in Node.js, Java, and Python