SAM: In function code, import layer node_modules

0

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 }
已提问 2 年前1212 查看次数
1 回答
0

You need to change your import as

import Axios from "axios";

已回答 2 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则