4

I am currently trying to deploy an AWS lambda function using serverless. I can get the function to deploy correctly and respond if I don't include any external modules, however as soon as I attempt to include dynogels and Joi:

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

the serverless deploy command will not get past "packing external modules"

The full output when not including external modules:

❯ 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.

The full output when including external modules:

❯ 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

The packages are installed. I can't see why this isn't deploying. Is there any way I can see some errors from the deploy? --verbose doesn't seem to provide much extra information. Thanks for your help!

Edit 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;
  // }
};
TomHill
  • 614
  • 1
  • 10
  • 26
  • Where is the error? – hephalump Feb 24 '20 at 00:00
  • @hephalump - there is no error message, it shows "Packing external modules" and that's the last output I recieve from it. It then does not deploy the function. – TomHill Feb 24 '20 at 00:06
  • Very strange. What version of serverless are you using? I have never heard of this behaviour. It always produces an output unless manually exited. Have you tried to isolate it to a single dependency? So just do Joi or dynogels, not both. Are you able to include any other dependencies? – hephalump Feb 24 '20 at 00:13
  • @hephalump I have tried using only dynogels and only Joi, same issue. I have tried installing and saving cowsay from npm and using a completely different dependency - same issue. I have tried including my own js file and that seems to work fine ... – TomHill Feb 24 '20 at 00:23
  • What version of `SLS` are you using? Also, what package manager are you using? – hephalump Feb 24 '20 at 00:24
  • Are you able to share the code? The webpack config, function, and serverless.yml...? – hephalump Feb 24 '20 at 00:26
  • SLS: Framework Core: 1.64.0 (standalone) Plugin: 3.4.0 SDK: 2.3.0 Components Core: 1.1.2 Components CLI: 1.4.0 package manager is npm 6.13.7 I will edit question to include the files you mentioned – TomHill Feb 24 '20 at 00:30
  • Can you add the function code, please. – hephalump Feb 24 '20 at 00:39
  • @hephalump done so ... however I think I just realised i'm not including the node dependencies with webpack node externals. I think that is my issue. – TomHill Feb 24 '20 at 00:42
  • @TomHill have you found a solution for this? – John Mar 31 '20 at 18:00

1 Answers1

0

Try to use the Lambda Layers for your SAM application.

Adnan Mohib
  • 331
  • 5
  • 16