- Newest
- Most votes
- Most comments
- First, correct the layer structure. AWS Lambda layers need to follow a specific directory structure:
src/layers/sample-layer/
└── nodejs/
└── node_modules/
└── lodash/
- Modify your template.yaml to include the layer in your function:
Resources: SampleLayer: Type: AWS::Serverless::LayerVersion Properties: LayerName: SampleLayer Description: A shared layer for reusable code ContentUri: src/layers/sample-layer CompatibleRuntimes: - nodejs18.x HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: src/handlers/ Handler: hello-from-lambda.handler Runtime: nodejs18.x Layers: - !Ref SampleLayer # Add this line to reference the layer
- In your function code, modify the import statement:
// Instead of ES modules import const _ = require('lodash');
- Create a esbuild configuration file (esbuild.config.js) in your project root:
const esbuild = require('esbuild'); esbuild.build({ bundle: true, entryPoints: ['src/handlers/hello-from-lambda.js'], external: ['lodash'], // Mark layer dependencies as external format: 'cjs', platform: 'node', target: 'node18', outdir: 'dist', }).catch(() => process.exit(1));
- Update your package.json to include the build script:
{ "scripts": { "build": "esbuild --bundle src/handlers/hello-from-lambda.js --platform=node --target=node18 --external:lodash --outdir=dist" } }
-
Make sure your function's package.json doesn't include lodash as a dependency (it should only be in the layer's package.json).
-
Build and deploy:
sam build sam deploy
Additional tips:
- For local testing, you can use the SAM CLI's local invoke:
sam local invoke HelloWorldFunction
- If you're using TypeScript, make sure to add type definitions:
npm install --save-dev @types/lodash
- To verify the layer is properly included, you can check the Lambda console after deployment.
Common issues to check if you're still having problems:
- Ensure the layer and function are using compatible Node.js versions
- Verify the layer directory structure is exactly as shown above
- Make sure the layer's package.json has the correct dependencies
- Check that the function's IAM role has permissions to access the layer
If you need to debug the layer locally:
# Build the layer separately sam build SampleLayer # Check the built layer contents ls -la .aws-sam/build/SampleLayer
This approach should work with your existing setup and allow you to use layers effectively with SAM CLI.
To add a layer to your SAM hello-world example for Node.js and resolve the issue you're facing, you need to make a few adjustments to your configuration and structure. Here's how you can modify your setup:
- In your SAM template, make sure you're referencing the layer in your function definition. Add the
Layersproperty to your function:
HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: hello-world/ Handler: app.lambdaHandler Runtime: nodejs18.x Layers: - !Ref SampleLayer
- Ensure your layer structure is correct. For Node.js, the structure should be:
src/layers/sample-layer/nodejs/
Place your node_modules folder directly under this nodejs folder.
- In your function code, instead of using ES6 import syntax, use CommonJS require:
const _ = require('lodash');
- When building your SAM application, use the
--use-containerflag to ensure the build process uses a Docker container that mimics the Lambda environment:
sam build --use-container
This approach ensures that your layers are properly included and accessible to your Lambda function during the build process.
- If you're still encountering issues, you might need to adjust your esbuild configuration. You can do this by adding an
esbuild.config.jsfile in your function's directory with the following content:
const esbuild = require('esbuild'); esbuild.build({ entryPoints: ['app.js'], bundle: true, outfile: 'dist/app.js', external: ['aws-sdk', '/opt/*'], platform: 'node', target: 'node18', }).catch(() => process.exit(1));
This configuration tells esbuild to treat anything in /opt (where Lambda layers are mounted) as external.
By following these steps, you should be able to successfully add and use layers in your SAM application. Remember that when deployed, Lambda will automatically make the layer contents available in the /opt directory, so your code will be able to access the dependencies without needing to change import paths.
Sources
Increase efficiency using Lambda layers with AWS SAM - AWS Serverless Application Model
Working with layers for Node.js Lambda functions - AWS Lambda
Building Lambda layers in AWS SAM - AWS Serverless Application Model
Using AWS SAM with layers - AWS Lambda
Unfortunately this AI response is not helpful.
Relevant content
- asked 4 years ago
- asked 9 months ago
- AWS OFFICIALUpdated 3 years ago

I need to be able to use ES modules. Assuming those work, does this mean I have to keep a list of externals for all my functions? I have 50+ functions with hundreds of imports so that doesn't seem sustainable.