2

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?

Adi shukla
  • 173
  • 3
  • 11

1 Answers1

4

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
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • I tried using the layers concept. I made a requirements file for my packages. I installed those packages in a target folder and then uploaded it on aws lambda to make a layer. Requirements file was -> asgiref==3.2.3 certifi==2019.11.28 chardet==3.0.4 cloudevents==0.2.4 decorator==4.4.1 Django==3.0 idna==2.8 jaeger-client==4.2.0 jsonpath-ng==1.4.3 pbr==5.4.4 ply==3.11 pytz==2019.3 requests==2.22.0 six==1.13.0 sqlparse==0.3.0 urllib3==1.25.7 aws-xray-sdk mysql-connector-python gunicorn But it is still not able to import module django in the lambda function. – Adi shukla Jun 16 '20 at 08:29
  • @Adishukla Thanks. You have to tick next to answer upvote/downvote buttons to accept:-) – Marcin Jun 16 '20 at 08:38
  • https://stackoverflow.com/questions/62404733/unable-to-import-modules-from-aws-lambda-layer @Marcin can you look at this one? TIA – Adi shukla Jun 16 '20 at 08:59
  • @Adishukla I provided and answer. Please don't forget to accept the old one if you can. – Marcin Jun 16 '20 at 10:43
  • since I am a new user, I have fewer reputation points. I have already accepted it. Whenever it will cross the threshold value it will automatically get accepted – Adi shukla Jun 16 '20 at 11:40
  • @Adishukla did you build the Python module zip using the same version of Python that you are running with the Lambda function (i.e. Python 3.9) and even more importantly did you do this on a Linux machine? Building it on a Windows machine won't work (oh how I have tried). There are some good tutorials on how to do this with AWS Cloud9 if you don't have a Linux machine handy at home. – Joseph U. Jun 22 '23 at 20:32