using-s3-access-logs-to-identify-requests
Using Amazon S3 Access Logs to Identify Requests
You can identify Amazon S3 requests using Amazon S3 access logs.
Note
We recommend that you use AWS CloudTrail data events instead of Amazon S3 access logs. CloudTrail data events are easier to set up and contain more information. For more information, see Using AWS CloudTrail to Identify Amazon S3 Requests.
Depending on how many access requests you get, it may require more resources and/or more time to analyze your logs.
Topics
- Enabling Amazon S3 Access Logs for Requests
- Querying Amazon S3 Access Logs for Requests
- Using Amazon S3 Access Logs to Identify Signature Version 2 Requests
- Using Amazon S3 Access Logs to Identify Object Access Requests
- Related Resources
Enabling Amazon S3 Access Logs for Requests
We recommend that you create a dedicated logging bucket in each AWS Region that you have S3 buckets in. Then have the Amazon S3 access log delivered to that S3 bucket.
Example โ Enable access logs with five buckets across two Regions
In this example, you have the following five buckets:
1-awsexamplebucket-us-east-1
2-awsexamplebucket-us-east-1
3-awsexamplebucket-us-east-1
1-awsexamplebucket-us-west-2
2-awsexamplebucket-us-west-2
Create two logging buckets in the following Regions:
awsexamplebucket-logs-us-east-1
awsexamplebucket-logs-us-west-2
Then enable the Amazon S3 access logs as follows:
1-awsexamplebucket-us-east-1
logs to the S3 bucketawsexamplebucket-logs-us-east-1
with prefix1-awsexamplebucket-us-east-1
2-awsexamplebucket-us-east-1
logs to the S3 bucketawsexamplebucket-logs-us-east-1
with prefix2-awsexamplebucket-us-east-1
1-awsexamplebucket-us-east-1
logs to the S3 bucketawsexamplebucket-logs-us-east-1
with prefix3-awsexamplebucket-us-east-1
1-awsexamplebucket-us-west-2
logs to the S3 bucketawsexamplebucket-logs-us-west-2
with prefix1-awsexamplebucket-us-west-2
2-awsexamplebucket-us-west-2
logs to the S3 bucketawsexamplebucket-logs-us-west-2
with prefix2-awsexamplebucket-us-west-2
You can then enable the Amazon S3 access logs using the following methods:
Using the AWS Management Console or,
Using the AWS CLI put-bucket-logging command to programmatically enable access logs on a bucket using the following commands:
1. First, grant Amazon S3 permission using `put-bucket-acl`:```1. aws s3api put-bucket-acl --bucket awsexamplebucket-logs --grant-write URI=http://acs.amazonaws.com/groups/s3/LogDelivery --grant-read-acp URI=http://acs.amazonaws.com/groups/s3/LogDelivery```1. Then, apply the logging policy:```1. aws s3api put-bucket-logging --bucket awsexamplebucket --bucket-logging-status file://logging.json````Logging.json` is a JSON document in the current folder that contains the logging policy:```{"LoggingEnabled": {"TargetBucket": "awsexamplebucket-logs","TargetPrefix": "awsexamplebucket/","TargetGrants": [{"Grantee": {"Type": "AmazonCustomerByEmail","EmailAddress": "user@example.com"},"Permission": "FULL_CONTROL"}]}}```Note
Theput-bucket-acl
command is required to grant the Amazon S3 log delivery system the necessary permissions (write and read-acp permissions).1. Use a bash script to add access logging for all the buckets in your account:```loggingBucket='awsexamplebucket-logs'region='us-west-2'# Create Logging bucketaws s3 mb s3://$loggingBucket --region $regionaws s3api put-bucket-acl --bucket $loggingBucket --grant-write URI=http://acs.amazonaws.com/groups/s3/LogDelivery --grant-read-acp URI=http://acs.amazonaws.com/groups/s3/LogDelivery# List buckets in this accountbuckets="$(aws s3 ls | awk '{print $3}')"# Put bucket logging on each bucketfor bucket in $bucketsdo printf '{"LoggingEnabled": {"TargetBucket": "%s","TargetPrefix": "%s/"}}' "$loggingBucket" "$bucket" > logging.jsonaws s3api put-bucket-logging --bucket $bucket --bucket-logging-status file://logging.jsonecho "$bucket done"donerm logging.jsonecho "Complete"```Note
This only works if all your buckets are in the same Region. If you have buckets in multiple Regions, you must adjust the script.
Querying Amazon S3 Access Logs for Requests
Amazon S3 stores server access logs as objects in an S3 bucket. It is often easier to use a tool that can analyze the logs in Amazon S3. Athena supports analysis of S3 objects and can be used to query Amazon S3 access logs.
Example
The following example shows how you can query Amazon S3 server access logs in Amazon Athena.
To specify the Amazon S3 location in an Athena query, you need the target bucket name and the target prefix, as follows: s3://awsexamplebucket-logs/prefix/
Open the Athena console at https://console.aws.amazon.com/athena/.
In the Query Editor, run a command similar to the following:
create database s3_access_logs_dbNote
It's a best practice to create the database in the same AWS Region as your S3 bucket.In the Query Editor, run a command similar to the following to create a table schema in the database that you created in step 2. The
STRING
andBIGINT
data type values are the access log properties. You can query these properties in Athena. ForLOCATION
, enter the S3 bucket and prefix path as noted earlier.CREATE EXTERNAL TABLE IF NOT EXISTS s3_access_logs_db.mybucket_logs(BucketOwner STRING,Bucket STRING,RequestDateTime STRING,RemoteIP STRING,Requester STRING,RequestID STRING,Operation STRING,Key STRING,RequestURI_operation STRING,RequestURI_key STRING,RequestURI_httpProtoversion STRING,HTTPstatus STRING,ErrorCode STRING,BytesSent BIGINT,ObjectSize BIGINT,TotalTime STRING,TurnAroundTime STRING,Referrer STRING,UserAgent STRING,VersionId STRING,HostId STRING,SigV STRING,CipherSuite STRING,AuthType STRING,EndPoint STRING,TLSVersion STRING)ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe'WITH SERDEPROPERTIES ('serialization.format' = '1', 'input.regex' = '([^ ]*) ([^ ]*)\\[(.*?)\\] ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) \\\"([^ ]*) ([^ ]*) (- |[^ ]*)\\\" (-|[0-9]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) (\"[^\"]*\") ([^ ]*)(?: ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*))?.*$' )LOCATION 's3://awsexamplebucket-logs/prefix'In the navigation pane, under Database, choose your database.
Under Tables, choose Preview table next to your table name.
In the Results pane, you should see data from the server access logs, such as
bucketowner
,bucket
,requestdatetime
, and so on. This means that you successfully created the Athena table. You can now query the Amazon S3 server access logs.
Example โ Show who deleted an object and when (timestamp, IP address, and IAM user)
Example โ Show all operations executed by an IAM user
Example โ Show all operations that were performed on an object in a specific time period
Example โ Show how much data was transferred by a specific IP address in a specific time period
Note
To reduce the time that you retain your log, you can create an Amazon S3 lifecycle policy for your server access logs bucket. Configure the lifecycle policy to remove log files periodically. Doing so reduces the amount of data that Athena analyzes for each query.
Using Amazon S3 Access Logs to Identify Signature Version 2 Requests
Amazon S3 support for Signature Version 2 will be turned off (deprecated). After that, Amazon S3 will no longer accept requests that use Signature Version 2, and all requests must use Signature Version 4 signing. You can identify Signature Version 2 access requests using Amazon S3 access logs.
Note
We recommend that you use AWS CloudTrail data events instead of Amazon S3 access logs. CloudTrail data events are easier to set up and contain more information. For more information, see Using AWS CloudTrail to Identify Amazon S3 Signature Version 2 Requests .
Example โ Show all requesters that are sending Signature Version 2 traffic
Using Amazon S3 Access Logs to Identify Object Access Requests
You can use queries on Amazon S3 server access logs to identify Amazon S3 object access requests, for operations such as GET, PUT, and DELETE, and discover further information about those requests.
The following Amazon Athena query example shows how to get all PUT object requests for Amazon S3 from the server access log.
Example โ Show all requesters that are sending PUT object requests in a certain period
The following Amazon Athena query example shows how to get all GET object requests for Amazon S3 from the server access log.
Example โ Show all requesters that are sending GET object requests in a certain period
The following Amazon Athena query example shows how to get all annonymous requests to your S3 buckets from the server access log.
Example โ Show all anonymous requesters that are making requests to a bucket in a certain period
Note
You can modify the date range as needed to suit your needs.
These query examples may also be useful for security monitoring. You can review the results for PutObject
or GetObject
calls from unexpected or unauthorized IP addresses/requesters and for identifying any anonymous requests to your buckets.
This query only retrieves information from the time at which logging was enabled.
If you are using Amazon S3 AWS CloudTrail logs, see Using AWS CloudTrail to Identify Access to Amazon S3 Objects.