Create and Test Python AWS Lambda Function Locally

  • Last updated Apr 25, 2024

AWS Lambda is a serverless service where we can deploy and run our application code or backend service without worrying about managing resources the code will require. AWS Lambda is auto-managed which takes care of all the compute resources, including maintenance of server and operating system, code monitoring and logging, capacity provisioning and auto scaling.

AWS Lambda runs code only when it is needed. As AWS Lambda scales automatically, it is capable of handling any number of requests from a few hundred every day to thousands of requests per second.

To run code in AWS Lambda, we must provide the code in one of the languages that Lambda supports. AWS Lambda supports many languages such as Java, Python, Node.js, Ruby, C#, Go, etc, through the use of runtime. The runtime provides a language-specific environment, suitable for running specific language code.

In this tutorial, we will learn how to create and test a Python AWS Lambda function locally using Visual Studio Code IDE.

We assume that you have an AWS account and have the Visual Studio Code IDE already installed on your computer.

Follow the steps below to complete this tutorial:

  1. The first thing, you'll need to do is install AWS CLI, AWS SAM CLI, Docker.
  2. Next, set AWS CLI credentials.
  3. Launch the Visual Studio Code IDE and install AWS Toolkit extension as shown in the example below:
  4. Next, you must see AWS icon on the left navigation bar. You should also see a Docker icon there if it is already installed on your computer as shown in the image below:
  5. To create a new Python AWS Lambda application in Visual Studio Code IDE, do the following:
    • Click AWS icon and right click on Lambda as shown in the image below:
    • Choose Create Lambda SAM Application.
    • Choose python3.8 as your Runtime.
    • Choose AWS SAM Hello World (A basic SAM app):
    • Choose a folder where you want to create your AWS Lambda function.
  6. To test AWS Lambda application locally, do as follows:
    • Open events.json file which should be inside the events folder of your Lambda application folder. Remove everything from the events.json folder and add the following to it:
    • {"numbers":[5,7]}
    • Open app.py file inside the hello_world folder of your Lambda application. Remove everything from the file and add the following to it:
    • def lambda_handler(event, context):
          print(event)
          numbers = event['numbers']
          response = {"addition":sum(numbers[0],numbers[1]), "multiplication":multiply(numbers[0],numbers[1])}
          return {
              "statusCode": 200,
              "body": response,
          }
      
      def sum(a,b):
          return a+b
      
      def multiply(a,b):
          return a * b
    • Next, run the following command via terminal from inside your Lambda project folder to call and test your Lambda function:
    • sudo sam local invoke -e ./events/event.json HelloWorldFunction

      Here, -e is event which is pointed to the event.json file and HelloWorldFunction is the function name given in the template.yaml file.

    • You should see the output like this:
    • {'numbers': [5, 7]}
      END RequestId: 20cce281-757f-499e-a08f-4b85eddb7ag6
      REPORT RequestId: 20cce281-757f-499e-a08f-4b85eddb7ag6 Init Duration: 0.58 ms Duration: 165.64 ms Billed Duration: 166 ms Memory Size: 128 MB Max Memory Used: 128 MB
      {"statusCode": 200, "body": {"addition": 12, "multiplication": 35}}