I want to use SAM to locally test and deploy a lambda function and layer.
How can I import node_modules in layers instead of node_modules in function code?
Now, when I import modules installed in the layer's directory node_modules instead of the function directory node_modules in the function code, I get the following error when I execute cd functions-stack && sam build --beta-feature
: Esbuild Failed: Could not resolve <module name>
.
Directory
functions-stack/
template.yml
my-function/
.aws-sam/
build/ <- empty
app.ts
node_modules/
layers-stack/
template.yml
nodejs/
node_modules/
package.json
functions-stack/my-function/template.yml
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: my-function
CodeUri: ./my-function/
Handler: app.lambdaHandler
Runtime: nodejs14.x
Architectures:
- x86_64
Layers:
- arn:aws:lambda:ap-northeast-1:123456789:layer:NodejsLayer:1
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: "es2020"
Sourcemap: true
EntryPoints:
- app.ts
functions-stack/my-function/app.ts
import { APIGatewayProxyEvent } from 'aws-lambda'
// installed in layers-stack/node_modules
// Esbuild Failed: ✘ [ERROR] Could not resolve "axios"
import axios from 'axios'
export const lambdaHandler = async (event: APIGatewayProxyEvent) => { return }