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 }
gefragt vor 2 Jahren1220 Aufrufe
1 Antwort
0

You need to change your import as

import Axios from "axios";

beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen