AWS Lambda 函数子目录中的处理程序未运行

caz*_*zer 5 amazon-web-services node.js aws-lambda

我在 Lambda 上遇到了一个非常不幸的错误:

Unable to import module 'lib/index': Error
at require (internal/module.js:20:19)
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为肯定有一个名为“handler从...导出”的函数lib/index,不确定整个子目录的事情是否对其他人来说是一个问题,所以我想问一下。

sam-template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Does something crazy
Resources:
  SomeFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: lib/index.handler
      Role: arn:aws:iam::...:role/lambda-role
      Runtime: nodejs6.10
      Timeout: 10
      Events:
        Timer:
          Type: Schedule
          Properties:
            Schedule: rate(1 minute)
Run Code Online (Sandbox Code Playgroud)

模块结构

|-- lib
|     `-- index.js
`-- src
      `-- index.js
Run Code Online (Sandbox Code Playgroud)

我将它嵌套在这里是因为我在构建过程中使用以下摘录来转译 ES6 package.json"build": "babel src -d lib"

构建规范.yaml

version: 0.1
phases:
    install:
        commands:
            - npm install
            - aws cloudformation package --template-file sam-template.yaml --s3-bucket some-bucket --output-template-file compiled-sam-template.yaml
    build:
        commands:
            - npm run build
    post_build:
        commands:
            - npm prune --production
artifacts:
    files:
        - 'node_modules/**/*'
        - 'lib/**/*'
        - 'compiled-template.yaml'
Run Code Online (Sandbox Code Playgroud)

caz*_*zer 5

该命令正在传送构建的资产,该资产在所示代码的阶段aws cloudformation package运行。install将其移至post_buildwill 确保它捕获所需的所有内容,包括lib/index有问题的:

post_build:
  commands:
    - npm prune --production
    - aws cloudformation package ...
Run Code Online (Sandbox Code Playgroud)