How to import own module within aws lambda function

Viewed 3795

I am trying to import my own module but I am getting error:

Unable to import module 'lambda_function': attempted relative import with no known parent package

lambda_function.py

enter image description here

Own modulename.py

enter image description here

2 Answers

You import it as if you were to import any other Python module. In other words don't do this:

from .name import *

but do this:

from name import show_name

For example:

enter image description here

The contents of name.py:

def my_name():
    print("Your name goes here.")

Don't forget to Deploy your function after making changes.

An alternative approach that I find works well if your lambda function is a module is to save it as a module. So something like:

my_awesome_service
|--lambda_function.py
|--name.py
included_module_1
included_module_2

And then invoke using:

my_awesome_service.lambda_function.lambda_handler

Then the relative imports work and you don't need any changes to your code

Related