Amazon S3에 Log 저장을 위해서는 각 Bucket에 Bucket Policy 설정을 해야된다.

 Bucket Policy는 IAM이 아닌, S3 Bucket에서 설정하는 것이다.

 Policy 설정 시, Principal에 각 Region에 맞게 설정해야 되며, 관련 링크는 다음과 같다.

 콘솔에서 설정: https://docs.aws.amazon.com/ko_kr/AmazonCloudWatch/latest/logs/S3ExportTasksConsole.html

 CLI를 통해 설정: https://docs.aws.amazon.com/ko_kr/AmazonCloudWatch/latest/logs/S3ExportTasks.html

 

Bucket Policy 예시.(Bucket 명: my-exported-logs, Region: us-west-2)

{
    "Version": "2012-10-17",
    "Statement": [
      {
          "Action": "s3:GetBucketAcl",
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::my-exported-logs",
          "Principal": { "Service": "logs.us-west-2.amazonaws.com" }
      },
      {
          "Action": "s3:PutObject" ,
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::my-exported-logs/random-string/*",
          "Condition": { "StringEquals": { "s3:x-amz-acl": "bucket-owner-full-control" } },
          "Principal": { "Service": "logs.us-west-2.amazonaws.com" }
      }
    ]
}

 

 주의할점은 GetBucketAcl 을 Lambda나 기타 다른 곳에서 수행 시, Bucket 명만 입력해야 되며 하위의 object 나 디렉토리까지 입력하면 GetBucketAcl 권한이 없다는 오류가 난다.

 

 오류 예시.

An error occurred (InvalidParameterException) when calling the CreateExportTask operation: GetBucketAcl call on the given bucket failed.

Please check if CloudWatch Logs has been granted permission to perform this operation.: InvalidParameterException