I am trying to create a cloudtrail using Go SDK. Successfully able to connect AWS without any issue by following AWS doc.
I Followed below steps for creating a trail
Step1 - Created S3 Bucket, so that all trail log files can be placed in this bucket.
CreateS3Bucket: Code
func CreateS3Bucket(bucketName string) error {
bucketName := "s3-bucket-123"
svc := s3.New(session.New(&aws.Config{Region: aws.String("us-east-1")}))
params := &s3.CreateBucketInput{
Bucket: aws.String(bucketName), // Required
}
resp, err1 := svc.CreateBucket(params)
if err1 != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Errorf("S3 Bucket Creation Fails: %s", err1.Error())
errs := errors.New("500")
return errs
}
// Pretty-print the response data.
log.Infof("Bucket Successfully created: %s", resp)
return nil
}
Success Response:
{\n Location: \"/s3-bucket-123\"\n}"
Step2 - Create CloudTrail
CreateCloudTrail: Code
func (ref *AwsCloudTrail) CreateCloudTrail(bucketName, trailName string) error {
svc := cloudtrail.New(session.New(&aws.Config{Region: aws.String("us-east-1")}))
//bucketName is "s3-bucket-123" and trailName is cloudtrail123
params := &cloudtrail.CreateTrailInput{
Name: aws.String(trailName), // Required
S3BucketName: aws.String(bucketName), // Required
}
resp, errs := svc.CreateTrail(params)
if errs != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Errorf("Error while creating trail %v",errs.Error())
err := errors.New("500")
return err
}
// Pretty-print the response data.
log.Infof("create trail response: %s",resp)
return nil
}
Response
"Error while creating trail InsufficientS3BucketPolicyException: Incorrect S3 bucket policy is detected for bucket: s3-bucket-123\n\tstatus code: 400, request id: 203d63d6-51ea-11e6-bb2c-b5d25b86e418"
Can anyone help me to resolve this, where am I doing it wrong? Is there any S3 Policy that I need to create in this ?
I took reference from AWS Website: https://docs.aws.amazon.com/sdk-for-go/api/service/cloudtrail/#CloudTrail.CreateTrail
https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#S3.CreateBucket