github.com/Foodji/aws-lambda-go@v1.20.2/events/README_DynamoDB.md (about) 1 # Sample Function 2 3 The following is a sample Lambda function that receives DynamoDB event data as input and writes some of the record data to CloudWatch Logs. (Note that by default anything written to Console will be logged as CloudWatch Logs.) 4 5 ```go 6 import ( 7 "context" 8 "fmt" 9 10 "github.com/aws/aws-lambda-go/events" 11 ) 12 13 func handleRequest(ctx context.Context, e events.DynamoDBEvent) { 14 15 for _, record := range e.Records { 16 fmt.Printf("Processing request data for event ID %s, type %s.\n", record.EventID, record.EventName) 17 18 // Print new values for attributes of type String 19 for name, value := range record.Change.NewImage { 20 if value.DataType() == events.DataTypeString { 21 fmt.Printf("Attribute name: %s, value: %s\n", name, value.String()) 22 } 23 } 24 } 25 } 26 ``` 27 28 # Reading attribute values 29 30 Stream notifications are delivered to the Lambda handler whenever data in the DynamoDB table is modified. 31 Depending on the Stream settings, a StreamRecord may contain the following data: 32 33 * Keys: key attributes of the modified item. 34 * NewImage: the entire item, as it appears after it was modified. 35 * OldImage: the entire item, as it appeared before it was modified. 36 37 The values for the attributes can be accessed using the AttributeValue type. For each type 38 supported natively by DynamoDB, there is a corresponding accessor method: 39 40 DynamoDB type | AttributeValue accessor method | Return type | DataType constant 41 ---------------|--------------------------------|---------------------------|------------------ 42 B (Binary) | Binary() | []byte | DataTypeBinary 43 BOOL (Boolean) | Boolean() | bool | DataTypeBoolean 44 BS (Binary Set)| BinarySet() | [][]byte | DataTypeBinarySet 45 L (List) | List() | []AttributeValue | DataTypeList 46 M (Map) | Map() | map[string]AttributeValue | DataTypeMap 47 N (Number) | Number() / Integer() / Float() | string / int64 / float64 | DataTypeNumber 48 NS (Number Set)| NumberSet() | []string | DataTypeNumberSet 49 NULL (Null) | IsNull() | bool | DataTypeNull 50 S (String) | String() | string | DataTypeString 51 SS (String Set)| StringSet() | []string | DataTypeStringSet 52 53 Calling the accessor method for the incorrect type will result in a panic. If the type needs to 54 be discovered in runtime, the method DataType() can be used in order to determine the correct accessor. 55 56 More information about DynamoDB data types can be seen [in this documentation](http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html). 57 58 The following example reads values of attributes name and age, for which types are known to be String and Number: 59 60 ```go 61 import ( 62 "context" 63 "fmt" 64 65 "github.com/aws/aws-lambda-go/events" 66 ) 67 68 func handleRequest(ctx context.Context, e events.DynamoDBEvent) { 69 70 for _, record := range e.Records { 71 fmt.Printf("Processing request data for event ID %s, type %s.\n", record.EventID, record.EventName) 72 73 // Print new values for attributes name and age 74 name := record.Change.NewImage["name"].String() 75 age, _ := record.Change.NewImage["age"].Integer() 76 77 fmt.Printf("Name: %s, age: %d\n", name, age) 78 } 79 } 80 ```