- Newest
- Most votes
- Most comments
The error you're encountering is likely due to the way Lambda packages and deploys your function. When you're testing locally using SAM (Serverless Application Model), it's trying to simulate the Lambda environment, but it seems that the '@azure/identity' module is not being included in your deployment package.
Here are a few things to check and try:
-
Make sure you've run 'npm install' in your project directory to install all the dependencies listed in your package.json file.
-
When using SAM to test locally, it doesn't automatically include node_modules. You need to ensure that your dependencies are properly packaged. Try running 'sam build' before 'sam local invoke' to package your function correctly.
-
Check if you're using the correct Node.js runtime version that's compatible with all your dependencies.
-
Ensure that your Lambda function's handler is correctly specified in your SAM template (template.yaml or template.json file).
-
If you're using ES modules (indicated by "type": "module" in package.json), make sure your import statements are correct. For CommonJS, use require() instead of import.
-
Try adding a "prepare" script in your package.json to install dependencies: "scripts": { "prepare": "npm install" }
-
If the issue persists, you might need to explicitly include the node_modules in your deployment package. You can do this by modifying your SAM template to include a custom packaging instruction.
Remember, when you deploy to AWS Lambda, you need to include all dependencies in the deployment package. Local testing with SAM should mimic this environment, but sometimes additional configuration is needed to ensure all modules are properly included.
Sources
Locally invoke Lambda functions with AWS SAM - AWS Serverless Application Model
Working with AWS Lambda functions using the AWS Toolkit - AWS Cloud9
Troubleshoot deployment issues in Lambda - AWS Lambda
Relevant content
- Accepted Answerasked 10 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 25 days ago
Addtional note: I use AWS CDK to deploy the Lambda function. I have received some answers that I should package node_modules in the Lambda function with the actual code or put it in the layer wrapping the Lambda function. However, I still don't understand why I have to do that because with other packages, simply define them in the package.json is sufficient?
Sorry if the answer is obvious, I'm new to AWS so can someone please kindly explain?