Search code examples
javascriptnode.jsjsonaws-lambdaserverless-framework

Node JS Serverless Api running fine in offline mode but not when deployed to AWS Lambda


I have written a code to stop and start the ec2 instances. I am passing data through postman in json like this to the post api in node JS.

{
    "action" : "stop",
    "instance_id" : "i-xxxxxxxxxxxxxxxx"
}

This is my code.

"use strict";

require("dotenv").config();

// load the SDK for JavaScript
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "us-west-2" });

// Create EC2 service object
var ec2 = new AWS.EC2({ apiVersion: "2016-11-15" });

module.exports.trigger = async (event) => {
  const body = JSON.parse(event.body);

  let id = body.instance_id;
  let action = body.action;

  var params = {
    InstanceIds: [id],
    DryRun: true,
  };

  if (action.toUpperCase() === "START") {
    // Call EC2 to start the selected instances
    ec2.startInstances(params, function (err, data) {
      if (err && err.code === "DryRunOperation") {
        console.log("inside start");
        params.DryRun = false;
        ec2.startInstances(params, function (err, data) {
          if (err) {
            console.log("Error", err);
            // issue = err;
          } else if (data) {
            console.log("Success", data.StartingInstances);
            // out = data.StartingInstances;
          }
        });
      } else {
        console.log("You don't have permission to start instances.");
      }
    });
  } else if (action.toUpperCase() === "STOP") {
    // Call EC2 to stop the selected instances
    ec2.stopInstances(params, function (err, data) {
      if (err && err.code === "DryRunOperation") {
        params.DryRun = false;
        ec2.stopInstances(params, function (err, data) {
          if (err) {
            console.log("Error", err);
            // issue = err;
          } else if (data) {
            console.log("Success", data.StoppingInstances);
            // out = data.StoppingInstances;
          }
        });
      } else {
        console.log("You don't have permission to stop instances", err);
      }
    });
  }

  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: "Go Serverless v3.0! Your function executed successfully!",
        input: event,
      },
      null,
      2
    ),
  };
};

This code is working fine when i am doing sls offline and running the code locally in my localhost. But when I deploy the code to lambda . The code is running fine but it is not starting or stopping the instance".

I don't know what could possibly be the reason.

it is not giving any error also . I don't know why it is returning the response without even executing the steps in between.


Solution

  • The resolution to this question was to use promises in the code.Something like this

    await ec2
          .stopInstances(params)
          .promise(function (err, data) {
            if (err) {
              console.log("Error", err);
              // issue = err;
            } else if (data) {
              console.log("Success", data.StoppingInstances);
              // out = data.StoppingInstances;
            }
          })