簡體   English   中英

當需要外部模塊時,不會部署使用無服務器的 AWS Lambda 函數

[英]Deploying an AWS Lambda function using serverless does not deploy when external modules are required

我目前正在嘗試使用無服務器部署 AWS lambda 函數。 如果我不包含任何外部模塊,我可以獲得正確部署並響應的功能,但是一旦我嘗試包含 dynogels 和 Joi:

const dynogels = require("dynogels");
const Joi = require("@hapi/joi");

serverless deploy命令不會通過“打包外部模塊”

包括外部模塊時的完整輸出:

❯ sls deploy
{ addHandler: './addHandler.js' }
Serverless: Bundling with Webpack...
   1 module
Serverless: No external modules needed
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service game-data-service.zip file to S3 (1.9 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...
Service Information
service: game-data-service
stage: dev
region: ap-southeast-2
stack: game-data-service-dev
resources: 11
api keys:
  None
endpoints:
  POST - XXXXXXXXXXX/endpoint
functions:
  addData: game-data-service-dev-addData
layers:
  None
Serverless: Updated basepath mapping.
Serverless Domain Manager Summary
Domain Name
  XXXXXXXXXXX.com
Distribution Domain Name
  Target Domain: XXXXXXXXXXX
  Hosted Zone Id: XXXXXXXXXXX
Serverless: Removing old service artifacts from S3...
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.

包含外部模塊時的完整輸出:

❯ sls deploy
{ addHandler: './addHandler.js' }
Serverless: Bundling with Webpack...
   3 modules
Serverless: Package lock found - Using locked versions
Serverless: Packing external modules: dynogels@^9.1.0, @hapi/joi@^17.1.0

軟件包已安裝。 我不明白為什么這沒有部署。 有什么辦法可以從部署中看到一些錯誤? --verbose似乎沒有提供太多額外的信息。 謝謝你的幫助!

編輯serverless.yml

service: game-data-service

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: ap-southeast-2
  environment:
    SERVICE_NAME: ${self:service}
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
        - dynamodb:BatchGetItem
      Resource: "arn:aws:dynamodb:ap-southeast-2:*:*"
    - Effect: Allow
      Action:
        - codepipeline:StartPipelineExecution
      Resource: "arn:aws:codepipeline:ap-southeast-2:*:*"
    - Effect: Allow
      Action:
        - s3:PutObject
        - s3:PutObjectAcl
      Resource: "arn:aws:s3:::XXXXXXXXXXX-assets-bucket/*"

plugins:
  - serverless-domain-manager
  - serverless-webpack

custom:
  stage: ${opt:stage, self:provider.stage}
  admin_arn: XXXXXXXXXX
  user_arn: XXXXXXXXXXX
  domains:
    prod: XXXXXXXXXXX.com
    staging: XXXXXXXXXXX.com
    dev: XXXXXXXXXXX.com
  webpack:
    webpackConfig: "webpack.config.js"
    includeModules: true # Node modules configuration for packaging
    packagePath: "../package.json"

  customDomain:
    basePath: gameData
    domainName: ${self:custom.domains.${self:custom.stage}}
    stage: "${self:custom.stage}"
    createRoute53Record: true

##############################################################
# Functions
##############################################################

functions:
  addData:
    handler: addHandler.hello
    events:
      - http: POST add

webpack.config.js

const slsw = require("serverless-webpack");
const nodeExternals = require("webpack-node-externals");
var path = require("path");
function resolve(dir) {
  return path.join(__dirname, dir);
}

console.log(slsw.lib.entries);
module.exports = {
  entry: slsw.lib.entries,
  target: "node",
  // Since 'aws-sdk' is not compatible with webpack,
  // we exclude all node dependencies
  externals: [nodeExternals()],
  resolve: {
    alias: {
      "@": resolve("../lib"),
      "=": resolve("../db")
    }
  },
  optimization: {
    minimize: false
  },
  mode: slsw.lib.webpack.isLocal ? "development" : "production",
  stats: "minimal"
};

addHandler.js

"use strict";

import { success, failure } from "@/response";
// import schema from "=/schema";
// import gameSchema from "=/gameSchema";

// const dynogels = require("dynogels");
// const Joi = require("@hapi/joi");
const cowsay = require("cowsay");


export const hello = (event, context, callback) => {
  console.log("Running ADD");
  console.log(cowsay.say({ text: "Mooodule" }));
  const body = JSON.parse(event.body);

  const table = body["table"];
  const data = body["data"];
  if (body["table"] == null) {
    callback(
      null,
      failure({
        error: "You must provide the table name in the body"
      })
    );
  }
  console.log(`Body recieved for table ${table}:`, data);
  callback(null, success({ message: `Data added to ${table}` }));
  // try {
  //   gameSchema[event.pathParameters.table].create(body, (err, res) => {
  //     if (err) {
  //       callback(null, failure({ error: err }));
  //       return;
  //     }

  //     callback(null, success(res));
  //   });
  //   callback(null, success(res));
  // } catch (err) {
  //   callback(null, failure({ error: err }));
  //   return;
  // }
};

嘗試將Lambda 層用於您的 SAM 應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM