Search code examples
amazon-web-servicesaws-samaws-sam-cli

HelloWorld not reading event from STDIN


I want to get the SAM CLI to read an event from standard in.

From 1 I should be able to:

sam local generate-event s3 [put/delete] --bucket <bucket> --key <key> | sam local invoke <function logical id>

This implies that I should be able to invoke my lambda from the command line by piping an event into the STDIN for sam local invoke

Well, take this for example:

# CREATE A HELLO_WORLD SAM PROJECT
sam init \
  --no-interactive \
  --name sam-read-stdin \
  --runtime python3.7 \
  --dependency-manager pip \
  --app-template hello-world

cd sam-read-stdin

# FORCE THE LAMBDA TO PRINT THE EVENT
sed 's/#     raise e/print\("EVENT %s" % event\)/' -i hello_world/app.py

# BUILD INSIDE DOCKER CONTAINER
sam build --use-container

# INVOKE USING THE STDIN PIPE
sam local generate-event apigateway aws-proxy | \
  sam local invoke HelloWorldFunction

# ALWAYS PRINTS THIS RESULT
# EVENT {}

What do I need to do so that the printout EVENT {} is instead from sam local generate-event?

Thank you in advance!


Solution

  • With release 31 of aws-sam they changed the syntax for reading an event from stdin. You now need to do it like this:

    sam local generate-event s3 [put/delete] --bucket <bucket> --key <key> | sam local invoke <function logical id> -e -
    

    Notice the "-e -" at the end.

    See https://github.com/awslabs/aws-sam-cli/releases/tag/v0.31.0