func getLog(cw *cloudwatchlogs.CloudWatchLogs, name string) (string, error) { groupPrefix := aws.String("/aws/lambda/" + name) groups, err := cw.DescribeLogGroups(&cloudwatchlogs.DescribeLogGroupsInput{LogGroupNamePrefix: groupPrefix}) if err != nil { return "", err } if len(groups.LogGroups) < 1 { return "", errors.New(fmt.Sprintf("No log group found for %s", name)) } group := groups.LogGroups[0] // We don't handle the case where lambda functions may share prefixes but we get the list of groups back in non-lexicographic order. Reminder in case that ever happens. if *group.LogGroupName != *groupPrefix { log.Fatal("Got group matching prefix but not actual", groupPrefix, group.LogGroupName) } streams, err := cw.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{ LogGroupName: group.LogGroupName, Descending: aws.Bool(true), OrderBy: aws.String("LastEventTime"), }) if err != nil { return "", err } if len(streams.LogStreams) < 1 { return "", errors.New(fmt.Sprintf("No log streams found for %s", name)) } stream := streams.LogStreams[0] get_log_input := &cloudwatchlogs.GetLogEventsInput{ LogStreamName: stream.LogStreamName, LogGroupName: group.LogGroupName, StartFromHead: aws.Bool(true), // allow 3 minute local vs server time out of sync StartTime: aws.Int64(time.Now().Add(-3*time.Minute).Unix() * 1000), } events, err := cw.GetLogEvents(get_log_input) if err != nil { return "", err } var output bytes.Buffer for _, event := range events.Events { output.WriteString(*event.Message) } return output.String(), nil }
// To be used when no Group or Stream is provided. Will point the user to a group // they can use for future runs. func cmdList(svc *cloudwatchlogs.CloudWatchLogs) { params := &cloudwatchlogs.DescribeLogGroupsInput{} resp, err := svc.DescribeLogGroups(params) if err != nil { panic(err) } for _, l := range resp.LogGroups { fmt.Println(*l.LogGroupName) } }
func lookupCloudWatchLogGroup(conn *cloudwatchlogs.CloudWatchLogs, name string, nextToken *string) (*cloudwatchlogs.LogGroup, error) { input := &cloudwatchlogs.DescribeLogGroupsInput{ LogGroupNamePrefix: aws.String(name), NextToken: nextToken, } resp, err := conn.DescribeLogGroups(input) if err != nil { return nil, err } for _, lg := range resp.LogGroups { if *lg.LogGroupName == name { return lg, nil } } if resp.NextToken != nil { return lookupCloudWatchLogGroup(conn, name, resp.NextToken) } return nil, fmt.Errorf("CloudWatch Log Group %q not found", name) }