Fazi
08/12/2021, 2:18 PM[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:
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 addthdxr
08/12/2021, 2:19 PMFazi
08/12/2021, 2:19 PMFazi
08/12/2021, 2:19 PMthdxr
08/12/2021, 2:20 PMFazi
08/12/2021, 2:52 PMthdxr
08/12/2021, 2:53 PMindex.py
referencing code from outside this folder?Fazi
08/12/2021, 2:55 PMindex.py
references lib.py
and some packages defined in requirements.txt
Fazi
08/12/2021, 2:55 PM_from_ src.utilities.lib _import_ BOTO_CONFIG, push_to_sns
thdxr
08/12/2021, 2:59 PMsrcPath
as src
and handler to lambdas/skills_sync/index.handler
thdxr
08/12/2021, 2:59 PMsrc
as wellFazi
08/12/2021, 3:00 PMthdxr
08/12/2021, 3:00 PMskills_sync
folder, not sure if pip
can help resolve thoseFazi
08/12/2021, 3:26 PMsrcPath
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 importsthdxr
08/12/2021, 3:57 PMFrank
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 depenciesFrank
srcPath
are not included in the zip.Frank
src.utilities.lib
with respect to the skills_sync
folder?thdxr
08/12/2021, 9:22 PMsrc
isn't anywhere in the bundleFazi
08/12/2021, 10:07 PMFrank
Frank
Fazi
08/12/2021, 10:10 PMSST: 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.Frank
Fazi
08/13/2021, 9:58 AMutilities
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.Frank
Frank
Frank