March 1, 2023 . 17 MIN READ
Server access logging provides detailed records for the requests that are made to an Amazon S3 bucket. Server access logs are useful for many applications. For example, access log information can be useful in security and access audits. This information can also help you learn about your customer base and understand your Amazon S3 bill.
By default, Amazon S3 doesn’t collect server access logs. When you enable logging, Amazon S3 delivers access logs for a source bucket to a destination bucket (also known as a target bucket) that you choose. The destination bucket must be in the same AWS Region and AWS account as the source bucket.
An access log record contains details about the requests that are made to a bucket. This information can include the request type, the resources that are specified in the request, and the time and date that the request was processed. For more information about logging basics, see Logging requests with server access logging.
Amazon S3 uses a special log delivery account to write server access logs. These writes are subject to the usual access control restrictions. For access log delivery, you must grant the logging service principal (logging.s3.amazonaws.com) access to your destination bucket.
To grant permissions to Amazon S3 for log delivery, you can use either a bucket policy or bucket access control lists (ACLs), depending on your destination bucket’s S3 Object Ownership settings. However, we recommend that you use a bucket policy instead of ACLs.
If the destination bucket uses the Bucket owner enforced setting for Object Ownership, ACLs are disabled and no longer affect permissions. In this case, you must update the bucket policy for the destination bucket to grant access to the logging service principal. You can’t update your bucket ACL to grant access to the S3 log delivery group. You also can’t include destination grants (also known as target grants) in your PutBucketLogging configuration.
For information about migrating existing bucket ACLs for access log delivery to a bucket policy, see Grant access to the S3 log delivery group for server access logging. For more information about Object Ownership, see Controlling ownership of objects and disabling ACLs for your bucket. When you create new buckets, ACLs are disabled by default.
To grant access by using the bucket policy on the destination bucket, update the bucket policy to grant the s3:PutObject permission to the logging service principal. If you use the Amazon S3 console to enable server access logging, the console automatically updates the bucket policy on the destination bucket to grant this permission to the logging service principal. If you enable server access logging programmatically, you must manually update the bucket policy for the destination bucket to grant access to the logging service principal.
For an example bucket policy that grants access to the logging service principal, see Grant permissions to the logging service principal by using a bucket policy.
You can alternately use bucket ACLs to grant access for access log delivery. You add a grant entry to the bucket ACL that grants WRITE and READ_ACP permissions to the S3 log delivery group. However, granting access to the S3 log delivery group by using bucket ACLs is not recommended. For more information, see Controlling ownership of objects and disabling ACLs for your bucket. For information about migrating existing bucket ACLs for access log delivery to a bucket policy, see Grant access to the S3 log delivery group for server access logging. For an example ACL that grants access to the logging service principal, see Grant permissions to the log delivery group by using a bucket ACL.
This example bucket policy grants the s3:PutObject permission to the logging service principal (logging.s3.amazonaws.com). To use this bucket policy, replace the with your own information. In the following policy, user input placeholders is the destination bucket where server access logs will be delivered, and amzn-s3-demo-destination-bucket is the source bucket. amzn-s3-demo-source-bucket is the optional destination prefix (also known as a target prefix) that you want to use for your log objects. EXAMPLE-LOGGING-PREFIX is the AWS account that owns the source bucket.SOURCE-ACCOUNT-ID
If there are Deny statements in your bucket policy, make sure that they don’t prevent Amazon S3 from delivering access logs.
{
“Version”:”2012-10-17″,
“Statement”: [
{
“Sid”: “S3ServerAccessLogsPolicy”,
“Effect”: “Allow”,
“Principal”: {
“Service”: “logging.s3.amazonaws.com”
},
“Action”: [
“s3:PutObject”
],
“Resource”: “arn:aws:s3:::amzn-s3-demo-destination-bucket/EXAMPLE-LOGGING-PREFIX*”,
“Condition”: {
“ArnLike”: {
“aws:SourceArn”: “arn:aws:s3:::amzn-s3-demo-source-bucket”
},
“StringEquals”: {
“aws:SourceAccount”: “SOURCE-ACCOUNT-ID”
}
}
}
]
}
As a security best practice, Amazon S3 disables access control lists (ACLs) by default in all new buckets. For more information about ACL permissions in the Amazon S3 console, see Configuring ACLs.
Although we do not recommend this approach, you can grant permissions to the log delivery group by using a bucket ACL. However, if the destination bucket uses the Bucket owner enforced setting for Object Ownership, you can’t set bucket or object ACLs. You also can’t include destination grants (also known as target grants) in your PutBucketLogging configuration. Instead, you must use a bucket policy to grant access to the logging service principal (logging.s3.amazonaws.com). For more information, see Permissions for log delivery.
In the bucket ACL, the log delivery group is represented by the following URL:
http://acs.amazonaws.com/groups/s3/LogDelivery
To grant WRITE and READ_ACP (ACL read) permissions, add the following grants to the destination bucket ACL:
<Grant>
<Grantee xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:type=”Group”>
<URI>http://acs.amazonaws.com/groups/s3/LogDelivery</URI>
</Grantee>
<Permission>WRITE</Permission>
</Grant>
<Grant>
<Grantee xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:type=”Group”>
<URI>http://acs.amazonaws.com/groups/s3/LogDelivery</URI>
</Grantee>
<Permission>READ_ACP</Permission>
</Grant>
For examples of adding ACL grants programmatically, see Configuring ACLs.
When you enable Amazon S3 server access logging by using AWS CloudFormation on a bucket and you’re using ACLs to grant access to the S3 log delivery group, you must also add “AccessControl": "LogDeliveryWrite" to your CloudFormation template. Doing so is important because you can grant those permissions only by creating an ACL for the bucket, but you can’t create custom ACLs for buckets in CloudFormation. You can use only canned ACLs with CloudFormation.
To enable server access logging by using the Amazon S3 console, Amazon S3 REST API, AWS SDKs, and AWS CLI, use the following procedures.
Reference:
https://docs.aws.amazon.com/AmazonS3/latest/userguide/enable-server-access-logging.html