amazon-web-servicesaws-lambdaaws-sam

Set Json object in command line instead of event.json on AWS-SAM


Normally

I can use this function for SAM

sam local invoke "MessageFunction" -e events/message.json

However I would like to set contents of event.json in commandline

So, I tried this but in vain.

echo '{"type": "Message", "user_id": 5 }' | sam local invoke "MessageFunction" 

Is there any way possible to do this?


Solution

  • So I tried piping and it seems to be complaining about extra parameters. The -e flag expects a file path as an input. So to answer your question, it seems to be not feasible as for my capabilities. However, you might want to consider piping the event on a temp file using the commands below:

    sam_tmp=$(mktemp)
    echo '{"type": "Message", "user_id": 5 }' >> "$sam_tmp"
    sam local invoke "MessageFunction" -e $sam_tmp
    rm $sam_tmp
    

    Looks really dirty but it gets the job done :D The logic is: