/* * Code goes along with the post made at: * https://ls3.io/post/ship_cloudwatch_logs_to_logentries/ */ package main import ( "encoding/json" "fmt" "strings" "github.com/aws/aws-lambda-go/events" "github.com/aws/aws-lambda-go/lambda" "github.com/bsphere/le_go" ) // CHANGE ME const LE_LOG_KEY = "INSERT_YOUR_LOG_KEY" func Handler(request events.CloudwatchLogsEvent) error { le, err := le_go.Connect(LE_LOG_KEY) if err != nil { panic(err) } defer le.Close() cloudwatchLogsData, err := request.AWSLogs.Parse() if err != nil { fmt.Println(err) return nil } // Remove the prefix to get to the name of the Lambda. logGroup := strings.Replace(cloudwatchLogsData.LogGroup, "/aws/lambda/", "", 1) // What you want to capture is up to you. BUT, for example: type LogEntry struct { LogGroup string `json:"log_group"` Timestamp int64 `json:"timestamp"` Message string `json:"message"` } // Stuff the incoming log lines into the datastructure to serialize to Log Entries. for _, event := range cloudwatchLogsData.LogEvents { logEntry := LogEntry{LogGroup: logGroup, Timestamp: event.Timestamp, Message: event.Message} j, err := json.Marshal(logEntry) if err != nil { fmt.Println(err) return nil } // Send to Logentries, basically. le.Print(string(j)) } return nil } func main() { // Entrypoint that the Lambda will execute. lambda.Start(Handler) }