bash脚本可以写入AWS Lambda函数内
问题描述:
我可以在Lambda函数内写入bash脚本吗?我在aws文档中读到它可以执行用Python,NodeJS和Java 8编写的代码。bash脚本可以写入AWS Lambda函数内
在某些文档中提到可能使用Bash,但没有支持它或任何示例的具体证据
答
我刚刚能够使用Amazon Lambda - Python捕获shell命令uname
输出。
下面是代码库。
from __future__ import print_function
import json
import commands
print('Loading function')
def lambda_handler(event, context):
print(commands.getstatusoutput('uname -a'))
它显示的输出
START RequestId: 2eb685d3-b74d-11e5-b32f-e9369236c8c6 Version: $LATEST
(0, 'Linux ip-10-0-73-222 3.14.48-33.39.amzn1.x86_64 #1 SMP Tue Jul 14 23:43:07 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux')
END RequestId: 2eb685d3-b45d-98e5-b32f-e9369236c8c6
REPORT RequestId: 2eb685d3-b74d-11e5-b31f-e9369236c8c6 Duration: 298.59 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 9 MB
欲了解更多信息检查链接 - https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/
答
至于你提到的AWS不提供方式使用bash编写lambda表达式。
要解决这个问题,如果你真的需要bash函数,你可以用任何语言“包装”你的bash脚本。
下面是与Java的例子:
Process proc = Runtime.getRuntime().exec("./your_script.sh");
根据您的业务需求,你应该考虑使用本地语言(Python和的NodeJS,Java)来避免性能损失。
答
东西可能会有帮助,我使用Node来调用bash脚本。我使用下面的代码作为处理程序,将脚本和nodejs文件上传到lambda的zip中。
exports.myHandler = function(event, context,callback) {
const execFile = require('child_process').execFile;
const child = execFile('./test.sh', (error, stdout, stderr) => {
if (error) {
callback(error);
}
callback(null,stdout);
});
}
您可以使用回调返回你所需要的数据。
答
它可能使用'child_process'节点模块。
const exec = require('child_process').exec;
exec('echo $PWD && ls', (error, stdout, stderr) => {
if (error) {
console.log("Error occurs");
console.error(error);
return;
}
console.log(stdout);
console.log(stderr);
});
这将显示当前工作目录并列出文件。
你想写一个Lambda函数内的bash脚本?或者使用bash脚本作为Lambda函数?两者都不同。 – helloV
@helloV我想将bash脚本用作Lambda函数 – user5241806
让我知道我提出的解决方案是否适合您。 – helloV