use*_*806 27 bash amazon-web-services aws-lambda
我可以在Lambda函数中编写一个bash脚本吗?我在aws文档中读到它可以执行用Python,NodeJS和Java 8编写的代码.
在一些文件中提到可能使用Bash但没有具体证据支持它或任何例子
小智 19
可能有帮助的东西,我正在使用Node来调用bash脚本.我使用以下代码作为处理程序将脚本和nodejs文件以zip格式上传到lambda.
exports.myHandler = function(event, context, callback) {
const execFile = require('child_process').execFile;
execFile('./test.sh', (error, stdout, stderr) => {
if (error) {
callback(error);
}
callback(null, stdout);
});
}Run Code Online (Sandbox Code Playgroud)
您可以使用回调来返回所需的数据.
Tho*_* L. 13
正如您所提到的,AWS没有提供使用Bash编写Lambda函数的方法.
要解决它,如果你真的需要bash函数,你可以用任何语言"包装"你的bash脚本.
以下是Java的一个示例:
Process proc = Runtime.getRuntime().exec("./your_script.sh");
Run Code Online (Sandbox Code Playgroud)
根据您的业务需求,您应该考虑使用本机语言(Python,NodeJS,Java)来避免性能损失.
mtu*_*tti 11
AWS最近宣布了“ Lambda运行时API和Lambda层”,这两个新功能使开发人员能够构建自定义运行时。因此,现在可以直接在Lambda中甚至运行bash脚本而不会受到黑客攻击。
由于这是一项非常新的功能(2018年11月),因此尚无太多资料,仍然需要完成一些手动工作,但是您可以查看此Github存储库作为示例开始(免责声明:我没有测试)。在bash中的示例处理程序下面:
function handler () {
EVENT_DATA=$1
echo "$EVENT_DATA" 1>&2;
RESPONSE="{\"statusCode\": 200, \"body\": \"Hello World\"}"
echo $RESPONSE
}
Run Code Online (Sandbox Code Playgroud)
这实际上为在Lambda中运行任何编程语言提供了可能性。这是有关发布自定义Lambda运行时的AWS教程。
我只是能够uname使用Amazon Lambda - Python 捕获shell命令输出.
下面是代码库.
from __future__ import print_function
import json
import commands
print('Loading function')
def lambda_handler(event, context):
print(commands.getstatusoutput('uname -a'))
Run Code Online (Sandbox Code Playgroud)
它显示了输出
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
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看链接 - https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/
根据此处的公告, AWS 现在支持自定义运行时。我已经测试了 bash 脚本并且它有效。您只需要创建一个新的 lambda 并选择runtime类型Custom,它将创建以下文件结构:
mylambda_func
|- bootstrap
|- function.sh
Run Code Online (Sandbox Code Playgroud)
示例Bootstrap:
#!/bin/sh
set -euo pipefail
# Handler format: <script_name>.<function_name>
# The script file <script_name>.sh must be located in
# the same directory as the bootstrap executable.
source $(dirname "$0")/"$(echo $_HANDLER | cut -d. -f1).sh"
while true
do
# Request the next event from the Lambda Runtime
HEADERS="$(mktemp)"
EVENT_DATA=$(curl -v -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next")
INVOCATION_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)
# Execute the handler function from the script
RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")
# Send the response to Lambda Runtime
curl -v -sS -X POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$INVOCATION_ID/response" -d "$RESPONSE"
done
Run Code Online (Sandbox Code Playgroud)
示例handler.sh:
function handler () {
EVENT_DATA=$1
RESPONSE="{\"statusCode\": 200, \"body\": \"Hello from Lambda!\"}"
echo $RESPONSE
}
Run Code Online (Sandbox Code Playgroud)
PS 但是在某些情况下,由于环境限制,您无法实现所需的功能,这种情况需要 AWS Systems Manager Run command根据您ScheduledTasks在 ECS 集群中更熟悉或定期使用的内容,OpsWork(Chef/Puppet)。
有关 bash 以及如何压缩和发布它的更多信息,请查看以下链接:
可以使用“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);
});
Run Code Online (Sandbox Code Playgroud)
这将显示当前工作目录并列出文件。
| 归档时间: |
|
| 查看次数: |
31379 次 |
| 最近记录: |