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 }
asked 5 months ago164 views
1 Answer
0

You need to change your import as

import Axios from "axios";

answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions