AnandShiva
AnandShiva

Reputation: 1339

sam local generate-event sqs - I want to generate a dummy request to lambda from SQS

I was following the official documentation but it gives no information on the parameters that need to be passed for specific event sources. SQS in my case.

I tried running sam local generate-event sqs in the CLI. But that fails with the wrong syntax scenario.

How can I test lambda from an SQS in my local using SAM project? I saw some sample code for apigateway, but no idea how they got those properties to be used in the CLI, Any reference to documentation or code samples would be helpful.

Upvotes: 2

Views: 1310

Answers (1)

user11666461
user11666461

Reputation: 1135

On running the command: sam local generate-event sqs receive-message --body '{"hello": "world"}', you will receive a response of the form:

{
  "Records": [
    {
      "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
      "receiptHandle": "MessageReceiptHandle",
      "body": "{"hello": "world"}",
      "attributes": {
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "1523232000000",
        "SenderId": "123456789012",
        "ApproximateFirstReceiveTimestamp": "1523232000001"
      },
      "messageAttributes": {},
      "md5OfBody": "49dfdd54b01cbcd2d2ab5e9e5ee6b9b9",
      "eventSource": "aws:sqs",
      "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
      "awsRegion": "us-east-1"
    }
  ]
}

For figuring out the same for other services, follow the below steps:

  1. sam local generate-event --help - This will list the possible set of services for which event can be generated.

  2. sam local generate-event sqs --help - This will list the possible set of commands under the service (here I have selected sqs)

  3. sam local generate-event sqs receive-message --help - This will list the possible set of options under that command(only receive-message command is available under sqs service)

Upvotes: 3

Related Questions