Lambda dependencies in CDK
When building serverless projects it is typically recommended to have both the application code and infrastructure described as a single code base. This way any changes can be made atomicly, especially with CloudFormation.
One solution for this is using [AWS SAM] and writing templates in CloudFormation. There are pre-build templates that can be used. Any of the supported SAM languages will pick up on typical dependency files such as
requirements.txt for Python or
package.json for NodeJS and bundle the dependencies in the lambda artifact.
For the CDK something similar can be done. It is possible to use the bundling component of the Code class of aws-lambda. An example can be found here
custom_function = aws_lambda.Function(
The way this works is very similar to [AWS SAM]. In the background it fires up a docker container, usually a lambci image. Installs any dependencies in a given directory, then these dependencies can be copied into the
/asset-output directory where the lambda code also lives.
The CDK approach to this is even more flexible because you can specify your own docker images, and commands to run. So you can use langauges not natively supported by [AWS SAM].
Also, for Python and NodeJS there are pre-made constructs that do this natively.