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년 전1207회 조회
1개 답변
0

You need to change your import as

import Axios from "axios";

답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠