简体   繁体   English

为什么我的代码可以在标准 Node.js 文件中运行,而不能在 AWS Lambda 函数中运行?

[英]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.我想要做的是创建一个 lambda 函数,该函数在 ec2 实例上调用两个命令。 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.当我在 lambda 函数中运行此代码时遇到问题时,我从exports.handler() 方法中删除了代码,并在同一个 ec2 实例中的独立 node.js 文件中运行了代码,并且我能够使代码正常工作. The command I ran was 'node app.js'.我运行的命令是“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.我认为这可能是与权限相关的问题,但 lambda 是 ec2 实例所在的同一 vpc 的一部分。

You're trying to combine async / await with callbacks.您正在尝试将async / await与回调结合起来。 That won't work in a lambda AWS Lambda Function Handler in Node.js .在 Node.js中的 lambda AWS Lambda 函数处理程序中不起作用。 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.在 Lambda 中,如果您使用async (或 Promises),一旦 lambda 退出,节点进程就会消失,因此无法触发回调。

Solution based on Jason's Answer:基于 Jason 的回答的解决方案:

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;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM