Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can my code run in a standard Node.js file, but not in a AWS Lambda Function?

What I'm trying to do is create a lambda function where the function calls two commands on an ec2 instance. When I had trouble running this code in a lambda function, I removed the code from the exports.handler() method and ran the code in a standalone node.js file in the same ec2 instance and I was able to get the code to work. The command I ran was 'node app.js'.

exports.handler = async (event) => {

  const AWS = require('aws-sdk')
  AWS.config.update({region:'us-east-1'});

  var ssm = new AWS.SSM();

  var params = {
  DocumentName: 'AWS-RunShellScript', /* required */
  InstanceIds: ['i-xxxxxxxxxxxxxxxx'],
  Parameters: {
    'commands': [
      'mkdir /home/ec2-user/testDirectory',
      'php /home/ec2-user/helloWorld.php'
      /* more items */
    ],
    /* '<ParameterName>': ... */
  }
};
ssm.sendCommand(params, function(err, data) {
  if (err) {
    console.log("ERROR!");
    console.log(err, err.stack); // an error occurred
  }
  else {
  console.log("SUCCESS!");
  console.log(data);
  }            // successful response
});


  const response = {
    statusCode: 200,
    ssm: ssm
  };

  return response;
}; 

I figured that it could have been a permissions related issue, but the lambda is apart of the same vpc that the ec2 instance is in.

like image 554
iii Avatar asked Sep 15 '25 20:09

iii


1 Answers

You're trying to combine async/await with callbacks. That won't work in a lambda AWS Lambda Function Handler in Node.js. The reason it's working locally, or in a node server, is because the server is still running when the function exits, so the callback still happens. In a Lambda the node process is gone as soon as the lambda exits if you are using async (or Promises), so the callback is not able to be fired.

like image 106
Jason Wadsworth Avatar answered Sep 17 '25 10:09

Jason Wadsworth