CDK automated zip creation of Lambda function with custom library dependencies
See original GitHub issueUse Case
AWS CDK currently supports 3 kinds of “Assets”:
- InlineCode - useful for one-line-lambdas
- AssetCode - one-file lambdas without dependencies
- S3Code - existing lambda packages already uploaded in an S3 bucket
There is, however, no support for more complex lambda function which require third party dependencies. So, in this case I need to zip by myself my Lambda function with all it’s custom libraries. However, I’m also working with Chalice, and there they solved that issue by automating the zip creation (which is awesome!).
While doing research on the matter, I stumbled across this: https://gitlab.com/josef.stach/aws-cdk-lambda-asset This great guy solved the matter in less than 150 lines by creating a new construct which creates a zip for you from your lambda and adds only the custom libraries (he doesn’t add the aws libraries).
I think it would benefit the community a lot if CDK took the same approach as Chalice in the matter and formalized this process, instead of having each developers add a few steps per Lambda function to the build process in their projects. It would be more native and easier to maintain.
Another bonus here is that his solution allows to keep track of dependencies and version control since you have a requirements.txt file.
Proposed Solution
Implement a new construct which works in the same matter this guy wrote: https://gitlab.com/josef.stach/aws-cdk-lambda-asset
He described his solution as such:
CDK AssetCode which builds lambda function and produces a ZIP file with dependencies.
Lambda function is built either in Docker or natively when running on Linux.
There is, however, no support for more complex lambda function which require third party dependencies.
This repository presents one possible approach to lambda packaging.
The construct is aware of libraries bundled in the AWS lambda runtime and automatically removes those for you to save space.
It also counts with compiled C dependencies such as NumPy and takes care of library stripping.
Other
- 👋 I may be able to implement this feature request
- ⚠️ This feature might incur a breaking change
This is a 🚀 Feature Request
Issue Analytics
- State:
- Created 4 years ago
- Reactions:45
- Comments:14 (5 by maintainers)
Top GitHub Comments
Here is a simple solution I found. First I created a top-level directory to store all my lambda functions. Second, I added package.json with some quick scripts which allow me to install npm modules for all my lambdas store at a top-level lambda directory. I then use the “aws-s3-assets” to manage and upload the lambda bundle and this easily worked with lambda.Code.fromBucket.
#7898 relies on docker and its for nodejs, why closing it? seems to be for now the best solution is using what josef.stach created.