简体   繁体   中英

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?

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:

  • We create a temporary file using mktemp
  • We then push the event onto the temporary file
  • Then pass it to the -e flag of the event
  • Clean it after session completes

According to the doc ,

To input JSON from stdin, you must pass in the value '-'.

You can do something like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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