Skip to content

Commit

Permalink
add sqs event
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoffatt committed Jun 28, 2018
1 parent 66db136 commit 02a038f
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
29 changes: 29 additions & 0 deletions events/README_SQS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

# Sample Function

The following is a sample class and Lambda function that receives Amazon SQS event message data as input, writes some of the message data to CloudWatch Logs, and responds with a 200 status and the same body as the request. (Note that by default anything written to Console will be logged as CloudWatch Logs events.)

```go
package main

import (
"context"
"fmt"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

func handler(ctx context.Context, sqsEvent events.SQSEvent) error {
for _, message := range sqsEvent.Records {
fmt.Printf("The message %s for event source %s = %s \n", message.MessageId, message.EventSource, message.Body)
}

return nil
}

func main() {
lambda.Start(handler)
}

```
28 changes: 28 additions & 0 deletions events/sqs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.

package events

type SQSEvent struct {
Records []SQSMessage `json:"Records"`
}

type SQSMessage struct {
MessageId string `json:"messageId"`
ReceiptHandle string `json:"receiptHandle"`
Body string `json:"body"`
Md5OfBody string `json:"md5OfBody"`
Md5OfMessageAttributes string `json:"md5OfMessageAttributes"`
Attributes map[string]string `json:"attributes"`
MessageAttributes map[string]SQSMessageAttribute `json:"messageAttributes"`
EventSourceARN string `json:"eventSourceARN"`
EventSource string `json:"eventSource"`
AWSRegion string `json:"awsRegion"`
}

type SQSMessageAttribute struct {
StringValue *string `json:"stringValue,omitempty"`
BinaryValue []byte `json:"binaryValue,omitempty"`
StringListValues []string `json:"stringListValues"`
BinaryListValues [][]byte `json:"binaryListValues"`
DataType string `json:"dataType"`
}
34 changes: 34 additions & 0 deletions events/sqs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
package events

import (
"encoding/json"
"testing"

"github.com/aws/aws-lambda-go/events/test"
)

func TestSqsEventMarshaling(t *testing.T) {

// 1. read JSON from file
inputJson := readJsonFromFile(t, "./testdata/sqs-event.json")

// 2. de-serialize into Go object
var inputEvent SQSEvent
if err := json.Unmarshal(inputJson, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}

// 3. serialize to JSON
outputJson, err := json.Marshal(inputEvent)
if err != nil {
t.Errorf("could not marshal event. details: %v", err)
}

// 4. check result
test.AssertJsonsEqual(t, inputJson, outputJson)
}

func TestSqsMarshalingMalformedJson(t *testing.T) {
test.TestMalformedJson(t, SQSEvent{})
}
40 changes: 40 additions & 0 deletions events/testdata/sqs-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"Records": [
{
"messageId" : "MessageID_1",
"receiptHandle" : "MessageReceiptHandle",
"body" : "Message Body",
"md5OfBody" : "fce0ea8dd236ccb3ed9b37dae260836f",
"md5OfMessageAttributes" : "582c92c5c5b6ac403040a4f3ab3115c9",
"eventSourceARN": "arn:aws:sqs:us-west-2:123456789012:SQSQueue",
"eventSource": "aws:sqs",
"awsRegion": "us-west-2",
"attributes" : {
"ApproximateReceiveCount" : "2",
"SentTimestamp" : "1520621625029",
"SenderId" : "AROAIWPX5BD2BHG722MW4:sender",
"ApproximateFirstReceiveTimestamp" : "1520621634884"
},
"messageAttributes" : {
"Attribute3" : {
"binaryValue" : "MTEwMA==",
"stringListValues" : ["abc", "123"],
"binaryListValues" : ["MA==", "MQ==", "MA=="],
"dataType" : "Binary"
},
"Attribute2" : {
"stringValue" : "123",
"stringListValues" : [ ],
"binaryListValues" : ["MQ==", "MA=="],
"dataType" : "Number"
},
"Attribute1" : {
"stringValue" : "AttributeValue1",
"stringListValues" : [ ],
"binaryListValues" : [ ],
"dataType" : "String"
}
}
}
]
}

0 comments on commit 02a038f

Please sign in to comment.