github.com/Foodji/aws-lambda-go@v1.20.2/events/cloudwatch_logs.go (about) 1 package events 2 3 import ( 4 "bytes" 5 "compress/gzip" 6 "encoding/base64" 7 "encoding/json" 8 ) 9 10 // CloudwatchLogsEvent represents raw data from a cloudwatch logs event 11 type CloudwatchLogsEvent struct { 12 AWSLogs CloudwatchLogsRawData `json:"awslogs"` 13 } 14 15 // CloudwatchLogsRawData contains gzipped base64 json representing the bulk 16 // of a cloudwatch logs event 17 type CloudwatchLogsRawData struct { 18 Data string `json:"data"` 19 } 20 21 // Parse returns a struct representing a usable CloudwatchLogs event 22 func (c CloudwatchLogsRawData) Parse() (d CloudwatchLogsData, err error) { 23 data, err := base64.StdEncoding.DecodeString(c.Data) 24 if err != nil { 25 return 26 } 27 28 zr, err := gzip.NewReader(bytes.NewBuffer(data)) 29 if err != nil { 30 return 31 } 32 defer zr.Close() 33 34 dec := json.NewDecoder(zr) 35 err = dec.Decode(&d) 36 37 return 38 } 39 40 // CloudwatchLogsData is an unmarshal'd, ungzip'd, cloudwatch logs event 41 type CloudwatchLogsData struct { 42 Owner string `json:"owner"` 43 LogGroup string `json:"logGroup"` 44 LogStream string `json:"logStream"` 45 SubscriptionFilters []string `json:"subscriptionFilters"` 46 MessageType string `json:"messageType"` 47 LogEvents []CloudwatchLogsLogEvent `json:"logEvents"` 48 } 49 50 // CloudwatchLogsLogEvent represents a log entry from cloudwatch logs 51 type CloudwatchLogsLogEvent struct { 52 ID string `json:"id"` 53 Timestamp int64 `json:"timestamp"` 54 Message string `json:"message"` 55 }