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 in AWS by ravi
144 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 by Esha

Related Questions In AWS

0 votes
1 answer

how to import numpy and pandas inside aws lambda function?

The problem is that your local numpy ...READ MORE

answered Sep 14, 2018 in AWS by Priyaj
• 56,520 points
2,546 views
0 votes
1 answer

How do I add python libraries to an AWS lambda function for Alexa?

As it is described in the Amazon ...READ MORE

answered Oct 23, 2018 in AWS by Archana
• 4,090 points
331 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 in AWS by Shashank
• 1,350 points
723 views
+1 vote
1 answer