0

I created 2 Lambda Functions from AWS Resources menu in Cloud9.

I wanna have common modules for these Functions.

I made New Folder for the common modules and add the path ~/environment/python/lib to PYTHONPATH of Preferences -> Python Support.

I did Run main function with Lambda(local).

But I got Unable to import module 'main/lambda_function': No module named 'common'.

Can I set up PYTHONPATH for Lambda Function in Cloud9?

Or is there a better practice for having common modules for several Lambda Functions?

My folder tree:

root
|-python
  |-lib     <- for uploading to Lambda Layers
    common.py
|-app
  |-main
    |-lambda_function.py
  |-main2
    |-lambda_function.py

my lambda_function.py:

import common
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
isexxx
  • 727
  • 1
  • 6
  • 23
  • Common modules should be placed in a layer and multiple functions should use that same layer. – therj Dec 14 '19 at 03:51
  • yes, but layers don't seem refered in Cloud9. https://stackoverflow.com/questions/54976291/aws-cloud9-referencing-lambda-layer-locally – isexxx Dec 14 '19 at 04:04

1 Answers1

0

Just output the PYTHONPATH in your python file before you do anything else:

import os
print(os.environ['PYTHONPATH'])

import ...

def lambda_handler(event,context):
  ...

You will see as part of the Function Logs that it outputs the environment variable as /var/runtime. Even if you set that path through the Cloud9 settings, you will discover just as I did that those settings do not hold.

I was hoping to setup all my layers under ~/environment/layers/layername, so I would have for example all my libraries and dependencies for scikit-learn under: ~/environment/layers/scikit-learn/python/... and I would use that same scikit-learn directory to actually create my layer with for deployment remotely.

But that did not work.

YoYo
  • 9,157
  • 8
  • 57
  • 74