为什么我的代码可以 运行 在标准 Node.js 文件中,但不能在 AWS Lambda 函数中?
Why can my code run in a standard Node.js file, but not in a AWS Lambda Function?
我想做的是创建一个 lambda 函数,该函数在 ec2 实例上调用两个命令。当我在 lambda 函数中遇到 运行 这段代码时,我从 exports.handler() 方法和 运行 相同的独立 node.js 文件中删除了代码ec2 实例,我能够让代码工作。我 运行 的命令是 '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;
};
我认为这可能是与权限相关的问题,但 lambda 与 ec2 实例所在的同一 vpc 不同。
您正在尝试将 async
/await
与回调相结合。这在 lambda AWS Lambda Function Handler in Node.js 中行不通。它在本地或在节点服务器中工作的原因是因为当函数退出时服务器仍然是 运行,所以回调仍然发生。在 Lambda 中,如果您使用 async
(或 Promises),一旦 lambda 退出,节点进程就会消失,因此无法触发回调。
基于 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;
};
我想做的是创建一个 lambda 函数,该函数在 ec2 实例上调用两个命令。当我在 lambda 函数中遇到 运行 这段代码时,我从 exports.handler() 方法和 运行 相同的独立 node.js 文件中删除了代码ec2 实例,我能够让代码工作。我 运行 的命令是 '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;
};
我认为这可能是与权限相关的问题,但 lambda 与 ec2 实例所在的同一 vpc 不同。
您正在尝试将 async
/await
与回调相结合。这在 lambda AWS Lambda Function Handler in Node.js 中行不通。它在本地或在节点服务器中工作的原因是因为当函数退出时服务器仍然是 运行,所以回调仍然发生。在 Lambda 中,如果您使用 async
(或 Promises),一旦 lambda 退出,节点进程就会消失,因此无法触发回调。
基于 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;
};