3

I have an AWS DynammoDB lambda which triggers by a DynamoDB stream. All implementation has been done in JS with ClaudiJS. When the lambda is deployed with the claudia create command there is no issue.

The problem is when the same function is deployed with GoCD pipeline using a dockerized build server following error occurs when the lambda function gets called.

module initialization error: Error
at Error (native)
at Object.fs.openSync (fs.js:641:18)
at Object.fs.readFileSync (fs.js:509:33)
at Object.Module._extensions..js (module.js:578:20)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)

Now I have spent over 10 hours and I have no idea of resolving this issue. Can someone please help me?

Lambda uses Node 6.10 and I use babel to transpile to node 6.10 . Tried withnode:boron and ubuntu:16.04 images as builder images for Docker.

Dulaj Atapattu
  • 448
  • 6
  • 23

3 Answers3

0

I spent more than a day on this issue. At the end, I have tried almost all possible approaches and finally solved the issue by switching to Serverless from ClaudiaJS. For use of everyone I will mention here the approaches I tried with the results.

  1. Used the same localhost environment inside the build docker container used by the GoCD pipeline (Same node version, same yarn version, Ubuntu 16:04). But issue was still there.
  2. Removed docker and setup the GoCD pipeline to run directly on the build server (Again used the same node version, same yarn version, Ubuntu 16:04 as I used in my local machine). But again there was no lock and issue was there without any change.
  3. Committed the node_modules folder and build folder of my local machine to the git repository and used the same node_modules and build files withing the GoCD pipeline without executing yarn and without transpiling the code on the build server. But nothing changed.

Finally, I switched to Serverless framework. In the 1st attempt I used Serverless with babel and without webpack even though serverless recommendation is to use webpack. But again the same issue was occurred when the lambda is deployed with the pipeline. The I changed the configuration to use webpack with serverless. Then all the issues were resolved and lambda was deployed successfully. This is the webpack.config.js I used at the end.

const path = require('path');

const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');

const build = {
  entry: slsw.lib.entries,
  resolve: {
    extensions: ['.js'],
  },
  target: 'node',
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
          },
        ],
      },
    ],
  },
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  optimization: {
    // Do not minimize the code.
    minimize: false,
  },
  performance: {
    // Turn off size warnings for entry points
    hints: false,
  },
  externals: [nodeExternals()],
};

module.exports = build;
Dulaj Atapattu
  • 448
  • 6
  • 23
0

This error occurs me when my serverless instance is going to read a .json file and extract a json object out of it. so i created that json object inside the script as a json object. then everything was fine.. I used basic configuration for webpack.config

noone
  • 6,168
  • 2
  • 42
  • 51
  • How did you try to read the file? Anyway, the error I have mentioned occurs before my actual code starts to execute. So it seems like your case is something different than mine. – Dulaj Atapattu Jun 10 '18 at 11:44
  • no of course the express server doesn't start. it cant find the configuration file(config.json). so I added the configuration json object inside the node source as a json object – noone Jun 10 '18 at 20:45
  • Yes, you're correct. But the problem here is I don't have such a file. – Dulaj Atapattu Jun 11 '18 at 11:38
  • this exception thrown because it tries to read a file in lambda function. so i instead of reading file i put the contents of the file inside the node script – noone Jun 11 '18 at 11:44
0

Let me prefix this by saying that I do not have specific experience with GoCD, but I have run into this error in other contexts.

One potential cause for this error is a file permissions issue in deploying the code to the VM.

The error is a generic error which means that a Lambda function was unable to start. You can receive this error inside of AWS as well. Unfortunately the logging level that you see with GoCD appears to be at the same level as AWS CloudWatch, which is not very good and does not tell you what prevented Lambda from starting. You need more logging to determine the exact cause in your situation.

If you do happen to experience this error in AWS, open your Lambda function. At the top of the AWS page there should be a dropdown with a "Test" button next to it.

AWS Lambda Test Dropdown

Open the dropdown and select "Configure Test Events." You will have to craft this test to match your specific lambda function. Next, select the new test and click the "Test" button. Lambda will show you a success or failure message with details from the call.

In my case, we had scripted the upload with the AWS sam utility on a linux box, and the file permissions were not quite right (the error was something about "permission denied, open '/var/task/index.js'").

EpicVoyage
  • 604
  • 7
  • 20