Skip to content

Instantly share code, notes, and snippets.

@LyleScott
Last active April 8, 2018 02:35
Show Gist options
  • Save LyleScott/fc4d5caef39b59b07f0ee519858d74e9 to your computer and use it in GitHub Desktop.
Save LyleScott/fc4d5caef39b59b07f0ee519858d74e9 to your computer and use it in GitHub Desktop.
Lambda Go code to ship Log Group subscription events to Logentries
/*
* 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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment