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?

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

6 Comments

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.
@Adishukla Thanks. You have to tick next to answer upvote/downvote buttons to accept:-)
stackoverflow.com/questions/62404733/… @Marcin can you look at this one? TIA
@Adishukla I provided and answer. Please don't forget to accept the old one if you can.
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.