github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/aws/cloudwatch.go (about)

     1  package aws
     2  
     3  import (
     4  	"github.com/aws/aws-sdk-go/aws"
     5  	"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
     6  	"github.com/gruntwork-io/terratest/modules/testing"
     7  )
     8  
     9  // GetCloudWatchLogEntries returns the CloudWatch log messages in the given region for the given log stream and log group.
    10  func GetCloudWatchLogEntries(t testing.TestingT, awsRegion string, logStreamName string, logGroupName string) []string {
    11  	out, err := GetCloudWatchLogEntriesE(t, awsRegion, logStreamName, logGroupName)
    12  	if err != nil {
    13  		t.Fatal(err)
    14  	}
    15  	return out
    16  }
    17  
    18  // GetCloudWatchLogEntriesE returns the CloudWatch log messages in the given region for the given log stream and log group.
    19  func GetCloudWatchLogEntriesE(t testing.TestingT, awsRegion string, logStreamName string, logGroupName string) ([]string, error) {
    20  	client, err := NewCloudWatchLogsClientE(t, awsRegion)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	output, err := client.GetLogEvents(&cloudwatchlogs.GetLogEventsInput{
    26  		LogGroupName:  aws.String(logGroupName),
    27  		LogStreamName: aws.String(logStreamName),
    28  	})
    29  
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	entries := []string{}
    35  	for _, event := range output.Events {
    36  		entries = append(entries, *event.Message)
    37  	}
    38  
    39  	return entries, nil
    40  }
    41  
    42  // NewCloudWatchLogsClient creates a new CloudWatch Logs client.
    43  func NewCloudWatchLogsClient(t testing.TestingT, region string) *cloudwatchlogs.CloudWatchLogs {
    44  	client, err := NewCloudWatchLogsClientE(t, region)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	return client
    49  }
    50  
    51  // NewCloudWatchLogsClientE creates a new CloudWatch Logs client.
    52  func NewCloudWatchLogsClientE(t testing.TestingT, region string) (*cloudwatchlogs.CloudWatchLogs, error) {
    53  	sess, err := NewAuthenticatedSession(region)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	return cloudwatchlogs.New(sess), nil
    58  }