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.
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].