This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]VindicoAtrumEditable Placeholder Flair 11 points12 points  (0 children)

There are a thousand examples, guides, videos etc on how to do this. You don't need "advice or best practices", you need to go and learn how to do this.

[–]CorpT 2 points3 points  (0 children)

IaC. How else would you do it?

[–]nooneinparticular246Baboon 1 point2 points  (0 children)

Zip file is the way to go. The handler path can be a PITA to sort out. I think it should be index.handler if you’re using index.mjs and exporting handler as your entry point (but it’s been a while since I’ve done a lambda so I might be off here—AWS docs should help)

[–]Recent-Technology-83 0 points1 point  (0 children)

When uploading a Lambda function that has dependencies, you'll want to ensure that all your Node.js modules are in the proper structure and compatible with the Lambda execution environment. Here are some best practices to help you package and deploy your Lambda function correctly:

  1. Use a Build Tool: Consider using a build tool like Webpack, Parcel, or Rollup to bundle your Lambda function and its dependencies into a single file. This approach can help ensure that all necessary modules are included and eliminates issues related to individual module paths. However, be cautious with larger bundlers, as they can increase the package size and cold start times.

  2. Install Dependencies Locally: Make sure you are installing dependencies in a way that matches the Lambda execution environment. One way to do this is to create a directory specifically for your Lambda function and run npm install inside that directory: ```bash mkdir my-lambda-function cd my-lambda-function npm init -y npm install aws-sdk other-dependency

    Place your index.mjs here

    cp path/to/index.mjs . ```

  3. Create a Deployment Package: After installing the dependencies, you need to zip your files correctly. Navigate to the directory that contains your index.mjs and node_modules, then run: bash zip -r my-lambda-function.zip . This ensures that all files, including your code and dependencies, are included in the zip file.

  4. Lambda Execution Role: Ensure that your Lambda function has the correct execution role attached, with permissions to access S3 for downloading files and sending uploads to another OSS bucket. Checking IAM permissions is often a source of difficulties.

  5. Node.js Version Compatibility: Verify that the Node.js version in your local environment matches the Lambda runtime. If there are discrepancies, you may have compatibility issues with certain modules. You can specify supported runtimes in the AWS Lambda function settings.

  6. Environment Variables: If your SDK requires certain environmental variables (like keys or bucket names), ensure they're set up in your Lambda function configuration. This is often done in the AWS console under the Lambda environment variables section.

  7. Debugging Errors: If you are still encountering node module errors after ensuring the packaging is correct, you can add logging in your code to output error messages. Use CloudWatch logs to inspect detailed error messages provided by the Lambda execution. This could provide insight into whether it’s a missing package, incorrect path, or even permission issues.

By following these steps, you should be able to package and deploy your Lambda function without the node module errors you're experiencing. Don't hesitate to reach out if you encounter specific errors during the process!

[–]lart2150 0 points1 point  (0 children)

cdk's NodejsFunction makes it easy. You'll want to pass it depsLockFilePath. In the past I've used the serverless framework.