6-1. Immediate Collection on S3 Event Notifications
By default an S3 based logger lists the S3 bucket every cycle to find new objects. Collection is delayed by up to one cycle after an object is uploaded, and on a bucket with many objects the listing itself takes a long time.
Setting the SQS queue URL option makes the logger download and collect an object as soon as it is notified. This is supported from AWS 2.0.2612.0.
1. Create the SQS queue
Create a standard queue in the SQS console. Use one queue per logger.
Set the visibility timeout of the queue longer than the time the logger needs to download and parse one object. Raise it when you collect large objects for which the default 30 seconds is not enough.
2. Allow S3 to send messages
Add the following policy to the access policy of the queue so that the target bucket can send messages. Replace {queue ARN}, {bucket name}, and {account ID} with the actual values.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "s3.amazonaws.com" },
"Action": "sqs:SendMessage",
"Resource": "{queue ARN}",
"Condition": {
"ArnLike": { "aws:SourceArn": "arn:aws:s3:::{bucket name}" },
"StringEquals": { "aws:SourceAccount": "{account ID}" }
}
}
]
}
3. Register the event notification on the S3 bucket
In the S3 console, go to Properties > Event notifications of the bucket that stores the logs and click Create event notification.
- Event types: Select All object create events (
s3:ObjectCreated:*) - Prefix: Enter the same value as the S3 Bucket Prefix of the logger. Always set it when other logs are stored in the same bucket.
- Destination: Select SQS queue and choose the queue created in step 1
4. Add the IAM permissions
Add the following permissions to the IAM user or role of the connection profile. They are the same as the logger permissions in 2. IAM Policy Configuration.
sqs:ReceiveMessagesqs:DeleteMessagesqs:ChangeMessageVisibility
5. Configure the logger
Go to Loggers > Loggers, edit the target logger, and enter the queue URL in SQS queue URL.
The region of the queue URL may differ from the S3 Bucket Region of the logger. The app reads the region from the queue URL and connects to SQS in that region.
How it works
- The logger long polls the queue every cycle. On a notification it downloads and collects that object immediately and deletes the message.
- When an object cannot be downloaded, the message is kept and its visibility timeout is extended so it is redelivered: after 30 seconds, 1, 2, 4, 8 minutes, and then every 15 minutes. An object that fails three times is no longer retried.
- Duplicate and redelivered notifications are filtered by the object keys recorded in the collection state.
- Setting a queue URL on a logger that was collecting by listing keeps its existing collection history, so objects already collected are not collected again.
To verify the queue setup, start the logger and check the number of messages available in the SQS console. If messages only pile up, re-check the IAM permissions and the queue URL. When the logger reports an error, read the error message on the Loggers > Loggers page.