Skip to content

Instantly share code, notes, and snippets.

@Greyeye
Created August 29, 2022 22:52
Show Gist options
  • Save Greyeye/c993da8169a89dba76204131b8d2ce8c to your computer and use it in GitHub Desktop.
Save Greyeye/c993da8169a89dba76204131b8d2ce8c to your computer and use it in GitHub Desktop.
dynamoDB stream event trigger handling code
func handler(ctx context.Context, ev events.S3Event) {
// need to handle PUT, DELETE, UPDATE, separately
for _, record := range ev.Records {
switch record.EventName {
case "INSERT":
fmt.Printf("%+v\n", record)
var rr localTypes.ActivityEventsTable
UnmarshalStreamImage(record.Change.NewImage, rr)
fmt.Printf("%+v\n", rr)
case "MODIFY":
case "DELETE":
fmt.Printf("delete event")
}
}
}
// UnmarshalStreamImage converts events.DynamoDBAttributeValue to struct
func UnmarshalStreamImage(attribute map[string]events.DynamoDBAttributeValue, out interface{}) error {
dbAttrMap := make(map[string]types.AttributeValue)
for k, v := range attribute {
var dbAttr types.AttributeValue
bytes, marshalErr := v.MarshalJSON()
if marshalErr != nil {
return marshalErr
}
json.Unmarshal(bytes, &dbAttr)
dbAttrMap[k] = dbAttr
}
return attributevalue.UnmarshalMap(dbAttrMap, out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment