'How to install dependencies of lambda functions upon cdk build with AWS CDK
When using AWS SAM I used to run build
command which would go through all of my Lambda function packages and install their dependencies (run npm install
on them).
How can I achieve the same behavior with AWS CDK? It doesn't seem to do it automatically, or am I missing something?
Solution 1:[1]
This functionality really is missing. You'll need to write your own packaging. Keep in mind that lambda dependencies must be built on a system with the same architecture as the target system in AWS (Linux) if any of the dependencies (such as Numpy) uses a shared library with native C code.
There's a Docker image available which aims to provide an environment as close to AWS as possible: lambci/lambda:build-python3.7
So if you're building on any non-Linux architecture, you might need this for some more complex lambda functions.
EDIT: I opensourced my Python code for lambda packaging: https://gitlab.com/josef.stach/aws-cdk-lambda-asset
Solution 2:[2]
You can do this pretty easily with a local build script like this:
const websiteRedirectFunction = new lambda.Function(
this,
"RedirectFunction",
{
code: lambda.Code.fromAsset(path.resolve(__dirname, "../../redirect"), {
bundling: {
command: [
"bash",
"-c",
"npm install && npm run build && cp -rT /asset-input/dist/ /asset-output/",
],
image: lambda.Runtime.NODEJS_12_X.bundlingDockerImage,
user: "root",
},
}),
handler: "index.redirect",
tracing: lambda.Tracing.ACTIVE,
runtime: lambda.Runtime.NODEJS_12_X,
}
);
Assuming you have a folder that you want to build and upload the handler and node_modules for Lambda.
From the docs:
When using lambda.Code.fromAsset(path) it is possible to bundle the code by running a command in a Docker container. The asset path will be mounted at /asset-input. The Docker container is responsible for putting content at /asset-output. The content at /asset-output will be zipped and used as Lambda code.
Solution 3:[3]
There is (currently experimental) module inside aws-cdk
which solves the problem for Python.
See more here.
Solution 4:[4]
It does not do it automatically You'll need to package those. Then you'll be able to use fromAsset or fromBucket to get connect the code to the function
Solution 5:[5]
I created a separate project using SAM put all the requirements in requirements.txt alongside your app.py
then run sam build --build-dir packaged
The packaged
directory will have the packaged artifact with dependencies.
Then all that you have to do in your cdk is ` from aws_cdk import ( core, aws_lambda as lambda_)
.....
lambdaFn = lambda_.Function(
self, "DocManAuth",
handler="app.lambda_handler",
code=lambda_.Code.asset("../auth/packaged/DocManAuthFunction"),
timeout=core.Duration.seconds(30),
runtime=lambda_.Runtime.PYTHON_3_7,
)
core.CfnOutput(self, 'Authorizer Function',
value=lambdaFn.function_name)
`
for a complete project visit docman
Solution 6:[6]
For anyone using Python. (until aws-lambda-python lib is ready)
Install the function dependencies directly into the lambda function folder of your CDK project.
pip install --target ./ -r ./requirements.txt
Requirements text is just a list of dependencies:
requests==2.27.1
Then run:
cdk deploy
Everything in the lambda function folder will be deployed:
- Dependencies,
- Function code,
- Requirements.txt, etc.
Some more details here:
https://docs.aws.amazon.com/lambda/latest/dg/python-package.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | seanWLawrence |
Solution 3 | blahblah |
Solution 4 | khornberg |
Solution 5 | Gaurav Jagavkar |
Solution 6 | comfytoday |