How to import a python module in multiple lambda function

+1 vote
I have a couple of AWS Lambda functions. I have my  functions in a separate file called functions.py. I want to import this module in all of my AWS Lambda functions. I am unable to find a place to store this module (functions.py), so that when I make a change in this module I don't have to change anything in my Lambda functions.
Mar 26, 2019 in AWS by ravi
7,891 views

1 answer to this question.

+1 vote

setup your helper function in your file system like this:

pathToSomewhere/my/functions.py
pathToSomewhere/my/__init__.py
pathToSomewhere/setup.py

Where __init__.py is:

from .helper_functions import *

and setup.py is

from setuptools import setup

setup(name='my',
      version='0.10000',
      description='My functions',
      url='http://github.com/user/example',
      license='Proprietary',
      author='Null',
      author_email='null@example.com',
      packages=['my'],
      install_requires=['boto3'],
      zip_safe=False)

Now let's package up my_helper. From pathToSomewhere/ run:

python setup.py sdist

I'm assuming you already know how to create and upload a virtual environment for running your lambda function. If not, let me know.

Now let's install my_helper into the virtual env of your lambda function. Let's assume your virtual environment is called worker_env

./worker-env/bin/pip install file://pathToSomewhere/my

Now zip up worker-env and your actual lambda script and upload that.

This will work fine.

answered Mar 26, 2019 by Esha

Related Questions In AWS

+1 vote
1 answer

How to configure a Lambda function to access resources in a VPC?

Hi@akhtar, You can configure a Lambda function to ...READ MORE

answered Aug 27, 2020 in AWS by MD
• 95,440 points
1,858 views
0 votes
2 answers

how to import numpy and pandas inside aws lambda function?

this approach worked for me: https://medium.com/@melissa_89553/how-to- ...READ MORE

answered Oct 10, 2020 in AWS by anonymous
11,021 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

How to kill a lambda function in AWS?

$ aws lambda put-function-concurrency --function-name my-function --reserved-concurrent-executions ...READ MORE

answered Mar 28, 2019 in AWS by Shashank
• 1,370 points
22,593 views
+2 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP