Juuso Nykänen
Juuso Nykänen

Reputation: 448

Test AWS lambda function via cli

I want to test a lambda function via CLI instead of the AWS Management console. (Looking to automate function testing by making a bash script)

I've read the documentation: http://docs.aws.amazon.com/cli/latest/reference/lambda/invoke.html

I am trying to invoke the lambda function with a json event payload. My bashcode looks like this:

#!/bin/bash

name="arn:aws:lambda:euwest1:100000000000:function:foo"
invoketype="Event"
payload="{'account':'100261334439', 'region':'eu-west-1', 'detail-type':'Scheduled Event', 'source':'aws.events', 'time':'2017-07-16T03:00:00Z', 'id':'178710aa-6871-11e7-b6ef-e9b95183cfc9', 'resources':['arn:aws:events:eu-west-1:100000000000:rule/run_everyday']}"

aws lambda invoke --function-name "$name" --invocation-type "$invoketype" --payload "$payload" testresult.log

I am getting the error:

An error occurred (InvalidRequestContentException) when calling the Invoke operation: Could not parse request body into json: Unexpected character ('a'
(code 97)): was expecting double-quote to start field name at [Source: [B@7ac4c2fa; line: 1, column: 3]

I believe I am providing the payload in wrong format, but the documentation has no example, it just says that the datatype is 'blob'. I did some searching but only found info on BLOB (binary large object), but I'm pretty sure this is not what the documentation is referring to.

I have tried without the outer double quotes and replacing all the single quotes with double quotes, but all of these give the same error.

Upvotes: 5

Views: 6501

Answers (2)

Geek Stocks
Geek Stocks

Reputation: 2030

tl;dr

Correct (normally): $ aws lambda invoke --function-name myFunction --payload '{"key1":"value1"}' outputfile.txt

Correct (Windows): aws lambda invoke --function-name myFunction --payload "{""key1"": ""value1""}" outputfile.txt


OP's specific question is already answered correctly; this answer is for the benefit of those ending up here with slightly different circumstances from OP. What OS are you using? If its Windows, then you are likely pulling your hair out over single and double quote issues.

Windows command line does not understand single quotes, and double quotes within a quoted string must be escaped as two double quotes ("").

Credit (normal): AWS documentation

Credit (Windows): Hail Reddit!

Upvotes: 4

Frederic Henri
Frederic Henri

Reputation: 53793

valid JSon should have key and value between double quotes

so you should have the payload attribute written as

payload='{"account":"100261334439", "region":"eu-west-1", "detail-type":"Scheduled Event", "source":"aws.events", "time":"2017-07-16T03:00:00Z", "id":"178710aa-6871-11e7-b6ef-e9b95183cfc9", "resources":["arn:aws:events:eu-west-1:100000000000:rule/run_everyday"]}'

Upvotes: 2

Related Questions