28
votes

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.

4

4 Answers

49
votes

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

17
votes

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

12
votes

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.

2
votes

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"
}