Can bash script be written inside a AWS Lambda function

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

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.

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

回答1:

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.

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.



回答2:

As you mentioned, AWS does not provide a way to write Lambda function using Bash.

To work around it, if you really need bash function, you can "wrap" your bash script within any languages.

Here is an example with 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.



回答3:

I just was able to capture a shell command uname output using Amazon Lambda - Python.

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/



回答4:

Its possible using the 'child_process' node module.

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!