Hi, I am deploying my stack to production. Howev...
# help
f
Hi, I am deploying my stack to production. However, viewing the cloudwatch logs, I get the following issue:
Copy code
[ERROR] Runtime.ImportModuleError: Unable to import module 'index': No module named 'src' Traceback (most recent call last):

2021-08-12T15:06:09.638+01:00
My function definition is as follows:
Copy code
const skillsSyncFunction = new Function(this, 'SkillsSyncFunction', {
      srcPath: "src/lambdas/skills_sync",
      handler: 'index.handler',
      timeout: 30,
      environment: env,
      permissions: ["ssm", skillsTable, bulkAssignOrchestratorTopic],
      description: "Blah Blah"
    });
Folder structure:
MyApp/src/lambdas/skills_sync/index.py
Everything works locally in the
Debug_Stack
I should add
t
Hey just verifying your runtime is set to python?
f
Yes that is correct
Python 3.8
t
can you show me the contents of your src/lambdas/skills_sync folder?
f
@thdxr, is this being caused by how the function is being zipped?
t
Is
index.py
referencing code from outside this folder?
f
index.py
references
lib.py
and some packages defined in
requirements.txt
EDIT: apologies @thdxr, it also references an external file as follows:
_from_ src.utilities.lib _import_ BOTO_CONFIG, push_to_sns
t
That is probably the issue. I'm not super familiar with python but can you try setting the
srcPath
as
src
and handler to
lambdas/skills_sync/index.handler
Might need to move requirements to
src
as well
f
Ok thanks - I will give this a go
t
@Frank can probably suggest a better solution, I'm just unfamiliar with how python is bundled for production. The issue right now is the bundle doesn't contain the code outside of the
skills_sync
folder, not sure if
pip
can help resolve those
f
Setting
srcPath
 as 
src
 and handler to 
lambdas/skills_sync/index.handler
leads to the following error upon deployment:
[ERROR] Runtime.ImportModuleError: Unable to import module 'lambdas/skills_sync/index': No module named 'src'
definitely seems to be an issue with the bundling and imports
t
hm ok I'll let frank chime in when he has a moment
f
Bundling for Python is rather straight forward, on
sst deploy
• SST installs all the dependencies from the
requirements.txt
in a Docker environment compatible with the Lambda runtime environment; then • SST zips up the
srcPath
along with the installed depencies
So anything not inside the
srcPath
are not included in the zip.
@Fazi, can I see where is 
src.utilities.lib
 with respect to the 
skills_sync
 folder?
t
I had him move use src as the srcPath and it seemed like there was still an issue finding the dep. I think the way it's being imported isn't working since
src
isn't anywhere in the bundle
f
@Frank, This was the old directory structure:
f
I see.. lemme give it a quick try
@Fazi Which version of SST r u on?
f
@Frank I am on the following:
Copy code
SST: 0.37.1
CDK: 1.111.0
As an aside: If I have the following structure, with imports as
_from_ utilities.lib _import_ BOTO_CONFIG, push_to_sns
and
_from_ skills_sync_lib _import_ run_task
it all seems to work fine (basically removing the folders and flattening all the lambda functions into the
src
directory.
f
I see. I think I got the gist of it. Lemme give it a try.
f
@Frank @thdxr, I think I have figured this issue out. My understanding is below in case other users have this issue and search the threads (please correct me if I am wrong): As you both were alluding to, the
utilities
folder, or anything else that needs to be imported must sit inside the
srcPath
. This is because the entire
srcPath
is zipped up and uploaded to AWS (as per your commends above). If any module imports lie outside the
srcPath
, the module will not exist on the AWS lambda filesystem directory and throw a "not found" error. If there is one module (such as
utilities
) shared by several lambda functions, the easiest way is just put all the
lambda_function.py
files in the
srcPath
, along with the
utilities
module (as per the first image above) and import as follows:
_from_ utilities.lib _import_ BOTO_CONFIG
The disadvantage of this approach is (I think) that all the lambda functions will be zipped up and placed in the AWS lambda filesystem for each function (replication of code). The alternative approach is to stick with the original folder layout, and write a custom bundler that copies the
utilities
folder to the lambda function directory during
deploy
, but then that would mean changing around the imports inside the
lambda_function.py
files as the
utilities
folder would have moved, which would be tricky. Alternatively, the
utilities
folder can be replicated inside the folder containing the
lambda_function.py
file that contains the handler.
f
Hi @Fazi, thanks for the detailed explanation.
The bundling approach you described makes a lot of sense. It’s how SST bundles functions with Node.js runtime. SST uses a JS bundler called esbuild, it will traverse all the `import`s in the code, and bundle them together.
I’m not too familiar with python. Is there an existing bundler that does something as you described?