Skip to content

Commit 02a038f

Browse files
committed
add sqs event
1 parent 66db136 commit 02a038f

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

events/README_SQS.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
# Sample Function
3+
4+
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.)
5+
6+
```go
7+
package main
8+
9+
import (
10+
"context"
11+
"fmt"
12+
13+
"github.com/aws/aws-lambda-go/events"
14+
"github.com/aws/aws-lambda-go/lambda"
15+
)
16+
17+
func handler(ctx context.Context, sqsEvent events.SQSEvent) error {
18+
for _, message := range sqsEvent.Records {
19+
fmt.Printf("The message %s for event source %s = %s \n", message.MessageId, message.EventSource, message.Body)
20+
}
21+
22+
return nil
23+
}
24+
25+
func main() {
26+
lambda.Start(handler)
27+
}
28+
29+
```

events/sqs.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
package events
4+
5+
type SQSEvent struct {
6+
Records []SQSMessage `json:"Records"`
7+
}
8+
9+
type SQSMessage struct {
10+
MessageId string `json:"messageId"`
11+
ReceiptHandle string `json:"receiptHandle"`
12+
Body string `json:"body"`
13+
Md5OfBody string `json:"md5OfBody"`
14+
Md5OfMessageAttributes string `json:"md5OfMessageAttributes"`
15+
Attributes map[string]string `json:"attributes"`
16+
MessageAttributes map[string]SQSMessageAttribute `json:"messageAttributes"`
17+
EventSourceARN string `json:"eventSourceARN"`
18+
EventSource string `json:"eventSource"`
19+
AWSRegion string `json:"awsRegion"`
20+
}
21+
22+
type SQSMessageAttribute struct {
23+
StringValue *string `json:"stringValue,omitempty"`
24+
BinaryValue []byte `json:"binaryValue,omitempty"`
25+
StringListValues []string `json:"stringListValues"`
26+
BinaryListValues [][]byte `json:"binaryListValues"`
27+
DataType string `json:"dataType"`
28+
}

events/sqs_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
package events
3+
4+
import (
5+
"encoding/json"
6+
"testing"
7+
8+
"github.com/aws/aws-lambda-go/events/test"
9+
)
10+
11+
func TestSqsEventMarshaling(t *testing.T) {
12+
13+
// 1. read JSON from file
14+
inputJson := readJsonFromFile(t, "./testdata/sqs-event.json")
15+
16+
// 2. de-serialize into Go object
17+
var inputEvent SQSEvent
18+
if err := json.Unmarshal(inputJson, &inputEvent); err != nil {
19+
t.Errorf("could not unmarshal event. details: %v", err)
20+
}
21+
22+
// 3. serialize to JSON
23+
outputJson, err := json.Marshal(inputEvent)
24+
if err != nil {
25+
t.Errorf("could not marshal event. details: %v", err)
26+
}
27+
28+
// 4. check result
29+
test.AssertJsonsEqual(t, inputJson, outputJson)
30+
}
31+
32+
func TestSqsMarshalingMalformedJson(t *testing.T) {
33+
test.TestMalformedJson(t, SQSEvent{})
34+
}

events/testdata/sqs-event.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"Records": [
3+
{
4+
"messageId" : "MessageID_1",
5+
"receiptHandle" : "MessageReceiptHandle",
6+
"body" : "Message Body",
7+
"md5OfBody" : "fce0ea8dd236ccb3ed9b37dae260836f",
8+
"md5OfMessageAttributes" : "582c92c5c5b6ac403040a4f3ab3115c9",
9+
"eventSourceARN": "arn:aws:sqs:us-west-2:123456789012:SQSQueue",
10+
"eventSource": "aws:sqs",
11+
"awsRegion": "us-west-2",
12+
"attributes" : {
13+
"ApproximateReceiveCount" : "2",
14+
"SentTimestamp" : "1520621625029",
15+
"SenderId" : "AROAIWPX5BD2BHG722MW4:sender",
16+
"ApproximateFirstReceiveTimestamp" : "1520621634884"
17+
},
18+
"messageAttributes" : {
19+
"Attribute3" : {
20+
"binaryValue" : "MTEwMA==",
21+
"stringListValues" : ["abc", "123"],
22+
"binaryListValues" : ["MA==", "MQ==", "MA=="],
23+
"dataType" : "Binary"
24+
},
25+
"Attribute2" : {
26+
"stringValue" : "123",
27+
"stringListValues" : [ ],
28+
"binaryListValues" : ["MQ==", "MA=="],
29+
"dataType" : "Number"
30+
},
31+
"Attribute1" : {
32+
"stringValue" : "AttributeValue1",
33+
"stringListValues" : [ ],
34+
"binaryListValues" : [ ],
35+
"dataType" : "String"
36+
}
37+
}
38+
}
39+
]
40+
}

0 commit comments

Comments
 (0)