Skip to content

Instantly share code, notes, and snippets.

@morcmarc
Last active September 30, 2020 11:43
Show Gist options
  • Save morcmarc/35194b39184977c2c44eb9fe063889a3 to your computer and use it in GitHub Desktop.
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
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)
}
@RaeesBhatti
Copy link

NewImage should be changed to:

NewImage map[string]*dynamodb.AttributeValue `json:"NewImage"`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment