0
votes

I'm having some trouble invoking locally using serverless.

Using Python3.6 runtime

The function in serverless.yml is:

functions:
  myFunction:
  events:
    - sns: arn:aws:sns:us-east-1:123456789:myTopic

My command is

sls invoke local -f myFunction -s dev -r us-east-1 -p events/myMockedSnsMessage.json

Serverless doesn't like the syntax on the myMockedSnsMessage.json

When I log the event in my lambda function I get something like:

{
  'Records': [
    {
      'Sns': {
        'Message': '{"version":"0","id":"a965ce94-fcb2-ad15-319d-04adab1072d0","detail-type":"AWS API Call via CloudTrail",...}'
      }
    }
  ]
}

That is, the SNS message is a string with valid JSON inside

How should I store a mock event for an SNS message and still have valid JSON so serverless doesn't yell at me for bad JSON syntax?

1
What's the code in myMockedSnsMessage.json? I bet that it is invalid. Please include it in your question. - Noel Llevares
The json comes directly from the sns event source received by Lambda. The Message attribute is a string of JSON - maafk
Include it in your question please. Does it look like the JSON in Trent's answer below? - Noel Llevares

1 Answers

1
votes

Perhaps use double-quotes, and stringify the json. e.g.

{
  "Records": [
    {
      "Sns": {
        "Message": "{\"version\":\"0\",\"id\":\"a965ce94-fcb2-ad15-319d-04adab1072d0\",\"detail-type\":\"AWS API Call via CloudTrail\",...}"
      }
    }
  ]
}

If you need more examples, the AWS documentation has sample events that you can modify, and when you use AWS console to test your lambda, you can select many different event templates from a dropdown.