Last active
September 30, 2020 11:43
-
-
Save morcmarc/35194b39184977c2c44eb9fe063889a3 to your computer and use it in GitHub Desktop.
Unmarshaling a DynamoDB Stream event into a custom struct inside a Lambda Go function
This file contains hidden or 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
package main | |
import ( | |
"context" | |
"github.com/aws/aws-lambda-go/lambda" | |
"github.com/aws/aws-sdk-go/service/dynamodb" | |
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | |
) | |
type DynamoEventChange struct { | |
NewImage *dynamodb.AttributeValue `json:"NewImage"` | |
// ... more fields if needed: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_GetRecords.html | |
} | |
type DynamoEventRecord struct { | |
Change DynamoEventChange `json:"dynamodb"` | |
EventName string `json:"eventName"` | |
EventID string `json:"eventID"` | |
// ... more fields if needed: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_GetRecords.html | |
} | |
type DynamoEvent struct { | |
Records []DynamoEventRecord `json:"records"` | |
} | |
func handler(ctx context.Context, event DynamoEvent) error { | |
for _, record := range event.Records { | |
var myStruct *MyStruct | |
err := dynamodbattribute.Unmarshal(record.Change.NewImage, &myStruct) | |
if err != nil { | |
return err | |
} | |
// ... so on | |
} | |
} | |
func main() { | |
lambda.Start(handler) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NewImage
should be changed to: