CDK tool does not include installed external packages in virtualenvs when deploying
See original GitHub issueReproduction Steps
from glom import glom
$ cdk deploy
Error Log
I have a python code that imports glom. I used poetry add glom
to install it. Lamda function couldn’t find it. I tried pip install glom
, Lambda function still encountered an error.
The error is
[ERROR] Runtime.ImportModuleError: Unable to import module 'auto_tag': No module named 'glom'
Environment
- **CLI Version : 1.18.0 (build bc924bc)
- **Framework Version: Python 3.7
- **OS : MAC OSX
- **Language : Python
Other
By following this solution, I got it to work. However, my project now contains external library that has to be committed to the git project. It looks messy. https://stackoverflow.com/questions/58855739/how-to-install-external-modules-in-a-python-lambda-function-created-by-aws-cdk
I got it to work by running pip install glom --target lambda
where lambda is the directory configured in this block
eventHandler = _lambda.Function(
self,
'resourceTagger',
runtime = _lambda.Runtime.PYTHON_3_7,
code = _lambda.Code.asset('lambda'), <------- THIS
handler = 'auto_tag.handler'
)
But look how messy it is now - https://i.imgur.com/I5YMwtR.png
Are there other ways to do it something that we don’t have to install it in the path specified in _lambda.Code.asset
?
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 4 years ago
- Comments:9
Top GitHub Comments
@kgunny There is a way to test your lambda function locally, it requires AWS SAM installed
N.B. I’ve had to modify the template to point to the correct asset in cdk.out in the past. Not sure if it’s still the case.
Here’s the
mock.sh
script I created to run within my cdk python app project for local testing:Your layers worked like a charm! Thanks a lot! 😃