bash 脚本可以写在 AWS Lambda 函数中吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34629574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can bash script be written inside a AWS Lambda function
提问by Hardik Kamdar
Can I write a bash script inside a Lambda function? I read in the aws docs that it can execute code written in Python, NodeJS and Java 8.
我可以在 Lambda 函数中编写 bash 脚本吗?我在 aws 文档中读到它可以执行用 Python、NodeJS 和 Java 8 编写的代码。
It is mentioned in some documents that it might be possible to use Bash but there is no concrete evidence supporting it or any example
一些文件中提到可能可以使用 Bash,但没有具体的证据支持它或任何示例
回答by Daniel Cortés
Something that might help, I'm using Node to call the bash script. I uploaded the script and the nodejs file in a zip to lambda, using the following code as the handler.
可能有帮助的东西,我正在使用 Node 来调用 bash 脚本。我将 zip 中的脚本和 nodejs 文件上传到 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);
});
}
You can use the callback to return the data you need.
您可以使用回调来返回您需要的数据。
回答by mturatti
AWS recently announced the "Lambda Runtime API and Lambda Layers", two new features that enable developers to build custom runtimes. So, it's now possibile to directly run even bash scripts in Lambda without hacks.
AWS 最近宣布了“Lambda 运行时 API 和 Lambda 层”,这两项新功能使开发人员能够构建自定义运行时。因此,现在甚至可以在 Lambda 中直接运行 bash 脚本而无需黑客攻击。
As this is a very new feature (November 2018), there isn't much material yet around and some manual work still needs to be done, but you can have a look at this Github repofor an example to start with (disclaimer: I didn't test it). Below a sample handler in bash:
由于这是一个非常新的功能(2018 年 11 月),目前还没有太多材料,仍然需要完成一些手动工作,但您可以查看这个 Github 存储库作为开始的示例(免责声明:我没有测试过)。在 bash 中的示例处理程序下方:
function handler () {
EVENT_DATA=
echo "$EVENT_DATA" 1>&2;
RESPONSE="{\"statusCode\": 200, \"body\": \"Hello World\"}"
echo $RESPONSE
}
This actually opens up the possibility to run any programming language within a Lambda. Here it is an AWS tutorialabout publishing custom Lambda runtimes.
这实际上开启了在 Lambda 中运行任何编程语言的可能性。这是一个关于发布自定义 Lambda 运行时的AWS 教程。
回答by Thomas L.
As you mentioned, AWS does not provide a way to write Lambda function using Bash.
正如您提到的,AWS 没有提供使用 Bash 编写 Lambda 函数的方法。
To work around it, if you really need bash function, you can "wrap" your bash script within any languages.
为了解决这个问题,如果你真的需要 bash 函数,你可以用任何语言“包装”你的 bash 脚本。
Here is an example with Java:
下面是一个 Java 的例子:
Process proc = Runtime.getRuntime().exec("./your_script.sh");
Depending on your business needs, you should consider using native languages(Python, NodeJS, Java) to avoid performance loss.
根据您的业务需求,您应该考虑使用原生语言(Python、NodeJS、Java)以避免性能损失。
回答by Naveen Vijay
I just was able to capture a shell command uname
output using Amazon Lambda - Python.
我刚刚能够uname
使用 Amazon Lambda - Python捕获 shell 命令输出。
Below is the code base.
下面是代码库。
from __future__ import print_function
import json
import commands
print('Loading function')
def lambda_handler(event, context):
print(commands.getstatusoutput('uname -a'))
It displayed the output
它显示了输出
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
For More information check the link - https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/
有关更多信息,请查看链接 - https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/
回答by kisHoR
Its possible using the 'child_process' node module.
它可以使用“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);
});
This will display the current working directory and list the files.
这将显示当前工作目录并列出文件。
回答by Muhammad Soliman
AWS supports custom runtimes now based on this announcement here. I already tested bash script and it worked. All you need is to create a new lambda and choose runtime
of type Custom
it will create the following file structure:
根据此处的公告,AWS 现在支持自定义运行时。我已经测试了 bash 脚本并且它有效。您只需要创建一个新的 lambda 并选择runtime
类型Custom
,它将创建以下文件结构:
mylambda_func
|- bootstrap
|- function.sh
Example Bootstrap
:
示例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 "function handler () {
EVENT_DATA=
RESPONSE="{\"statusCode\": 200, \"body\": \"Hello from Lambda!\"}"
echo $RESPONSE
}
")/"$(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
Example handler.sh
:
示例handler.sh
:
P.S. However in some cases you can't achieve what's needed because of the environment restrictions, such cases need AWS Systems Manager to Run command
, OpsWork (Chef/Puppet) based on what you're more familiar with or periodically using ScheduledTasks
in ECS cluster.
PS 但是在某些情况下,由于环境限制,您无法实现所需的功能,这种情况下需要 AWS Systems Manager 来Run command
,OpsWork(Chef/Puppet)基于您更熟悉或ScheduledTasks
在 ECS 集群中定期使用的内容。
More Information about bash and how to zip and publish it, please check the following links:
有关 bash 以及如何压缩和发布它的更多信息,请查看以下链接:
回答by Shajibur Rahman
Now you can create Lambda functions written in any kind of language by providing a custom runtime which teaches the Lambda function to understand the syntax of the language you want to use.
现在,您可以通过提供自定义运行时来创建以任何类型的语言编写的 Lambda 函数,该运行时教 Lambda 函数理解您要使用的语言的语法。
You can follow this to learn more AWS Lambda runtimes
您可以按照此了解更多AWS Lambda 运行时