-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
] | ||
} |