AWS Ошибка лямбда-слоев при вызове API "не удается найти модуль" - PullRequest
1 голос
/ 20 апреля 2020

Я пытаюсь использовать слои безсерверной лямбды, я смотрю туто об этом, но я получаю сообщение об ошибке "не могу найти модуль ..."

service: aws-nodejs 

package:
  exclude:
    - .gitignore
    - package.json
    - .git/**

provider:
  name: aws
  profile: sandbox
  runtime: nodejs12.x

layers:
  testLayer:
    path: testLayer
    compatibleRuntimes:
      - nodejs12.x
    allowedAccounts:
      - '*'

functions:
  hello:
    handler: handler.hello
    layers:
      -  arn:aws:lambda:us-east-1:*:layer:testLayer:15
    events:
      - http:
          path: test
          method: get
          cors: true

При развертывании я в моем терминале нет ошибок, и на AWS я вижу свой слой, и когда я его загружаю, у меня есть пакет. json с зависимостью от момента и папка node_modules с моментом

Мой обработчик. js выглядит следующим образом:

'use strict';
module.exports.hello = async (event, context) => {
    const moment = require('moment')
    const a = moment('2016-01-01')
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Hey' + a
    }),
  };
};

Структура моих файлов:

testLayer/
   node_modules/
      moment/
   package.json
serverless.yml
handler.js
package.json

Есть ли у вас какие-либо представления о том, что я делаю неправильно?

1 Ответ

1 голос
/ 20 апреля 2020

Да, возможно, что ваши узловые модули есть в вашем лямбда-слое, и тем не менее лямбда выдает ошибку «не могу найти модуль ..».

Это может происходить, потому что созданный вами zip не соответствует структуре каталогов, указанной в документации AWS.

Согласно официальной документации:

Including Library Dependencies in a Layer
You can move runtime dependencies out of your function code by placing them in a layer. Lambda runtimes include paths in the /opt directory to ensure that your function code has access to libraries that are included in layers.

To include libraries in a layer, place them in one of the folders supported by your runtime.

Node.js – nodejs/node_modules, nodejs/node8/node_modules (NODE_PATH)

Example AWS X-Ray SDK for Node.js

xray-sdk.zip
└ nodejs/node_modules/aws-xray-sdk

Убедитесь, что в вашем zip-файле содержится правильная структура каталогов, иначе попробуйте импортировать модули от /opt/your_node_module_directory

...