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

寵の児 提交于 2020-05-29 09:08:26

问题


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.


回答1:


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.




回答2:


Solution based on Jason's Answer:

const AWS = require('aws-sdk');
const ssm = new AWS.SSM();


exports.handler = async (event,context) => {

AWS.config.update({region:'us-east-1'});
  const params = {
  DocumentName: 'AWS-RunShellScript', /* required */
  InstanceIds: ['i-xxxxxxxxxxxxxx'],
  Parameters: {
    'commands': [
      'mkdir /home/ec2-user/testDirectory',
      'php /home/ec2-user/helloWorld.php'
      /* more items */
    ],
    /* '<ParameterName>': ... */
  }
};


  const ssmPromise = new Promise ((resolve, reject) => {
    ssm.sendCommand(params, function(err, data) {
  if (err) {
    console.log("ERROR!");
    console.log(err, err.stack); // an error occurred
    context.fail(err);
  }
  else {
  console.log("SUCCESS!");
  console.log(data);
  context.succeed("Process Complete!");
  }            // successful response
  });
});


console.log(ssmPromise);   


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

  return response;
};


来源:https://stackoverflow.com/questions/60470604/why-can-my-code-run-in-a-standard-node-js-file-but-not-in-a-aws-lambda-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!