37

I'm trying to invoke a lambda on AWS using CLI:

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.

Thanks in advance.

Chacko
  • 1,506
  • 1
  • 20
  • 42
BernardoMorais
  • 571
  • 2
  • 6
  • 14

6 Answers6

62

stdout and stderr are basically files so can be used for arguments that expect one

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

Vlad
  • 629
  • 1
  • 5
  • 3
  • 3
    This is the exact answer – ayehia Nov 13 '18 at 11:28
  • 3
    This is a better answer but note that you will get both the return value from your lambda function printed to stdout along with the return of the call to 'invoke'. – TomH Dec 21 '18 at 16:44
  • Any idea how to get he payload accepted in version 2 that is not base64 and not coming from a file? – Nick Aug 13 '20 at 14:40
  • See: `cli_binary_format=raw-in-base64-out` ~/.aws/config . https://docs.aws.amazon.com/cli/latest/userguide/cliv2-migration.html#cliv2-migration-binaryparam – Nick Aug 13 '20 at 14:44
23

It's not possible to output directly to the terminal after invoking a lambda function. 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:

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

Adam Thomason
  • 1,118
  • 11
  • 23
13

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.

iBug
  • 35,554
  • 7
  • 89
  • 134
Mike Shauneu
  • 3,201
  • 19
  • 21
10

You can use your current tty as the outfile, which allows command output to still be redirected:

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

Using /dev/stdout for the outfile sort of works. Issue #1: the command output gets mixed up with it:

> 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. You can at least separate them by piping to cat:

> aws lambda invoke --function-name name --payload '{}' /dev/stdout | cat
ACTUAL_OUTPUT{
  "StatusCode": 200,
  "ExecutedVersion": "$LATEST"
}
Mark Melville
  • 783
  • 8
  • 13
  • aws lambda invoke --function-name greetingsOnDemand --payload "$payload" $(tty) >/dev/null --- Windows 10 git bash - [Errno 2] No such file or directory: '/dev/pty0' – Serhii Kushchenko Nov 05 '21 at 18:09
2

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:

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; })
  • This can be done without the `;` in the same invocation: `aws lambda invoke --function-name Func /dev/fd/3 3>&1 >/dev/null` – ateijelo May 17 '23 at 15:42
0

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:

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.

odie5533
  • 562
  • 3
  • 5