简体   繁体   English

在没有输出文件的情况下调用aws lambda

[英]Invoking aws lambda without output file

I'm trying to invoke a lambda on AWS using CLI:我正在尝试使用 CLI 在 AWS 上调用 lambda:

aws lambda invoke --function-name GetErrorLambda --payload '{"body":"{\"Id\":[\"321\",\"123\"]}"}' \output.

I would like to know if there's a way to print the output on the cli instead of create a file.我想知道是否有办法在 cli 上打印输出而不是创建文件。

Thanks in advance.提前致谢。

stdout and stderr are basically files so can be used for arguments that expect one stdout 和 stderr 基本上是文件,因此可用于期望一个的参数

aws lambda invoke --function-name GetErrorLambda --payload '{"body":"{\"Id\":[\"321\",\"123\"]}"}' /dev/stdout

though in this case the rest of the commands output will overlap with it, so probably better to cat it later anyways虽然在这种情况下,其余的命令输出将与它重叠,所以以后可能更好地对其进行分类

It's not possible to output directly to the terminal after invoking a lambda function.调用 lambda 函数后无法直接输出到终端。 This is likely by design as the output could easily be greater than the buffer size for a window.这可能是设计使然,因为输出很容易大于窗口的缓冲区大小。

A simple workaround would be to simply 'cat' out the contents of the output file following the cli command like so:一个简单的解决方法是在 cli 命令之后简单地“cat”输出文件的内容,如下所示:

aws lambda invoke --function-name GetErrorLambda --payload '{"body":"{\\"Id\\":[\\"321\\",\\"123\\"]}"}' \\output. && cat outputFileName.txt

I use following command:我使用以下命令:

aws lambda invoke --function-name name --payload '{...}' /dev/stdout 2>/dev/null

The idea is redirect command output to stderr and the function result to stdout .这个想法是将命令输出重定向到stderr并将函数结果重定向到stdout

You can use your current tty as the outfile, which allows command output to still be redirected:您可以使用当前的 tty 作为输出文件,这允许仍然重定向命令输出:

> aws lambda invoke --function-name name --payload '{}' $(tty) >/dev/null
LAMBDA_OUTPUT

Using /dev/stdout for the outfile sort of works.使用/dev/stdout进行/dev/stdout排序。 Issue #1: the command output gets mixed up with it:问题 1:命令输出与它混淆:

> aws lambda invoke --function-name name --payload '{}' /dev/stdout
{ACTUAL_OUTPUT
  "StatusCode": 200,
  "ExecutedVersion": "$LATEST"
}

Issue #2: If you try to redirect the stdout then you've just directed the lambda result as well.问题 2:如果您尝试重定向标准输出,那么您也刚刚定向了 lambda 结果。 You can at least separate them by piping to cat :您至少可以通过管道将它们分开cat

> aws lambda invoke --function-name name --payload '{}' /dev/stdout | cat
ACTUAL_OUTPUT{
  "StatusCode": 200,
  "ExecutedVersion": "$LATEST"
}

I would output it to a file and then cat it, but also redirect the regular output so the only thing printed is from the lambda invocation:我会将它输出到一个文件,然后对其进行 cat,但也会重定向常规输出,因此唯一打印的内容来自 lambda 调用:

aws lambda invoke --function-name GetErrorLambda --payload '{"body":"{\"Id\":[\"321\",\"123\"]}"}' out >>log && cat out

You can also add && echo to the end of it to echo a new line.您还可以在其末尾添加&& echo以回显新行。

Using a Bourne shell and for systems that support /dev/fd/<n> , you can duplicate stdout to a new file descriptor and use it for aws output:使用 Bourne shell 和支持/dev/fd/<n>的系统,您可以将 stdout 复制到新的文件描述符并将其用于 aws 输出:

exec 3>&1; aws lambda invoke --function-name Func /dev/fd/3 >/dev/null

You can also use that with pipeline:您还可以将其与管道一起使用:

{ exec 3>&1; aws lambda invoke --function-name Func /dev/fd/3 >/dev/null; } | cat

Or with command substitution:或者使用命令替换:

out=$({ exec 3>&1; aws lambda invoke --function-name Func /dev/fd/3 >/dev/null; })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM