Can an AWS Lambda function call another normal python function from a python file?

Viewed 2283

I have a python file that has a bunch of functions. For each function, I have to create a respective AWS lambda function so that the AWS Lambda function calls that normal function. Is there a way to achieve this? For ex ->

Normal function:

def add_num(input1, input2):
   return input1 + input2

Now I want an AWS lambda function to call this above function. How can I do this?

1 Answers

Yes, you can.

There are two popular option to make your custom libraries available for your lambda function.

  1. Bundle your function's custom libraries with your deployment package:

You need to create a deployment package if you use the Lambda API to manage functions, or if you need to include libraries and dependencies other than the AWS SDK. You can upload the package directly to Lambda, or you can use an Amazon S3 bucket, and then upload it to Lambda.

  1. Create a lambda layer with your libraries and other dependencies you require. The advantage of this approach over 1 is that you can reuse common code across multiple lambda function:

With layers, you can use libraries in your function without needing to include them in your deployment package.

With either of the above options, you can import your libraries as you usually do on a local workstation. For example:

import myutils

def handler_name(event, context): 

    some_value = myutils.add_num(1, 2)
    ...
    return some_value
Related