Menu
AWS SDK for Go
Developer Guide

Listing the CloudTrail Trails

This example uses the DescribeTrails operation to list the names of the CloudTrail trails and the bucket in which CloudTrail stores information in the us-west-2 region.

Choose Copy to save the code locally.

Create the file describe_trails.go. Add the following statements to import the Go and AWS SDK for Go packages used in the example.

Copy
import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudtrail" "fmt" "os" )

Initialize the session that the SDK uses to load credentials from the shared credentials file .aws/credentials in your home folder, and create a new service client.

Copy
sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create CloudTrail client svc := cloudtrail.New(sess)

Call DescribeTrails. If an error occurs, print the error and exit. If no error occurs, loop through the trails, printing the name of each trail and the bucket.

Copy
resp, err := svc.DescribeTrails(&cloudtrail.DescribeTrailsInput{TrailNameList: nil}) if err != nil { fmt.Println("Got error calling CreateTrail:") fmt.Println(err.Error()) os.Exit(1) } fmt.Println("Found",len(resp.TrailList),"trail(s) in", regionName) fmt.Println("") for _, trail := range resp.TrailList { fmt.Println("Trail name: " + *trail.Name) fmt.Println("Bucket name: " + *trail.S3BucketName) fmt.Println("") }

See the complete example on GitHub.