I've set up several Lambda functions in my CDK code, and while some work perfectly and run the bundling command, one of them isn't functioning as expected. The functions are very similar—aside from the names and a few environment variables—but this particular one isn't installing any of the required imports. It deploys without any errors, but the packages just aren't there. Here's the code for the problematic Lambda function:
```javascript
const collectionLoader = new lambda.Function(this, "CollectionLoader", {
code: lambda.Code.fromAsset(
path.join(__dirname, "../src/collection_loader"),
{
bundling: {
image: lambda.Runtime.PYTHON_3_12.bundlingImage,
command: [
"bash",
"-c",
"pip install -r requirements.txt -t /asset-output && cp -au . /asset-output",
],
},
},
),
environment: {
MAX_ATTEMPTS: "5",
BUCKET_NAME: dataBucket.bucketName,
DLQ_URL: dlq.queueUrl,
},
handler: "handler.lambda_handler",
runtime: lambda.Runtime.PYTHON_3_12,
vpc,
securityGroups: [privateSG],
timeout: cdk.Duration.seconds(300),
});
```
I also included the code for a function that works fine:
```javascript
const otherFunction = new lambda.Function(this, "OtherFunction", {
code: lambda.Code.fromAsset(
path.join(__dirname, "../src/other-function"),
{
bundling: {
image: lambda.Runtime.PYTHON_3_12.bundlingImage,
command: [
"bash",
"-c",
"pip install -r requirements.txt -t /asset-output && cp -au . /asset-output",
],
},
},
),
environment: {
DIFFERENT_ENVIRONMENT_VARIABLE
},
handler: "facets.handler",
runtime: lambda.Runtime.PYTHON_3_12,
vpc,
securityGroups: [privateSG],
timeout: cdk.Duration.seconds(300),
});
```
I'm struggling to figure out why the packages aren't being installed and would appreciate any insights!
3 Answers
Make sure your requirements.txt actually lists all the necessary packages. If everything looks good there, it's possible that the bundling command might not be executing correctly. It's worth checking.
Have you tried running the bundling command manually? I also recommend checking out the alpha construct; it might give you a clearer error message. If your requirements are similar, what's actually different between them might give a clue too!
I suspect the asset-output path could be causing issues since you're using Docker for bundling. You might want to try switching to a different construct or adjusting that output path to see if that changes anything.
I compared the working code and the non-working code, and they look almost identical aside from the source directory. I'm not sure switching the path is the root of the issue.

Yep, I've checked that. All the packages are listed in requirements.txt, but there's still nothing after deployment. In the console, I can see that the working function has all its packages, while this one just has the txt file and the handler.