In this post I will use AWS managed services to enhance my S3 user experience with custom EventBridge notifications that are low cost, quick to set up and perform well at scale.
Table of Contents
- Introduction
- SNS: The Notifier
- S3: The Storage
- EventBridge: The Go-Between
- Testing
- Cost Analysis
- Summary
Introduction
I’ve been restoring some S3 Glacier Flexible Retrieval objects lately. I use bulk retrievals to reduce costs – these finish within 5–12 hours. However, on a couple of occasions I’ve totally forgotten about them and almost missed the download deadline!
Having recently set up some alerting, I decided to make a similar setup that will trigger emails at key points in the retrieval process, using the following AWS services:
- S3 for holding the objects and managing the retrieval process
- EventBridge for receiving events from S3 and looking for patterns
- SNS for sending notifications to me
The end result will look like this:
Let’s start with SNS.
SNS: The Notifier
I went into detail about Amazon Simple Notification Service (SNS) in my last post about making some security alerts so feel free to read that if some SNS terms are unfamiliar.
Here I want SNS to send me emails, so I start by making a new standard topic called s3-object-restore
. I then create a new subscription with an email endpoint and link it to my new topic.
This completes my SNS setup. Next I need to make some changes to one of my S3 buckets.
S3: The Storage
Amazon S3 stores objects in buckets. The properties of a bucket can be customised to complement its intended purpose. For example, the Default Encryption
property forces encryption on buckets containing sensitive objects. The Bucket Versioning
property protects objects from accidental changes and deletes.
Here I’m interested in the Event Notifications
property. This property sends notifications when certain events occur in the bucket. Examples of S3 events include uploads, deletes and, importantly for this use case, restore requests.
S3 can send events to a number of AWS services including, helpfully, EventBridge! This isn’t on by default but is easily enabled in the bucket’s properties:
My bucket will now send events to EventBridge. But what is EventBridge?
EventBridge: The Go-Between
Full disclosure. At first I wasn’t entirely sure what EventBridge was. The AWS description did little to change that:
I tend to uncomplicate topics by abstracting them. Here I found it helpful to think of EventBridge as a bus:
- Busses provide high-capacity transport between bus stops. The bus is EventBridge.
- Passengers use the bus to get to where they need to go. The passengers are events.
- Bus stops are where passengers join or depart the bus. The bus stops are event sources and targets.
In the same way that a bus picks up passengers at one bus stop and drops them off at another, EventBridge receives events from a source and directs them to a target.
Much has been written about EventBridge’s benefits. Rather than spending the next few paragraphs copy/pasting, I will instead suggest the following for further reading:
- This Dashbird post about some of EventBridge’s main characteristics.
- This Lunigo EventBridge guide.
- This UK Government Digital Marketplace EventBridge assessment.
In this use case, EventBridge’s main advantage is that it is decoupled from S3. This allows one EventBridge Rule to serve many S3 buckets. S3 can send notifications to SNS without EventBridge, but each bucket needs configuring separately so this quickly causes headaches with multiple buckets.
Currently my S3 bucket is already sending events to EventBridge, so let’s create an EventBridge rule for them.
EventBridge Rule: Setting A Pattern & Choosing A Source
Rules allow EventBridge to route events from a source to a target. After naming my new rule s3-object-restore
, I need to choose what kind of rule I want:
Event Pattern
: the rule will be triggered by an event.Schedule
: the rule will be triggered by a schedule.
I select Event Pattern
. EventBridge then poses further questions to establish what events to look for:
Event Matching Pattern
: Do I want to use EventBridge presets or write my own pattern?Service Provider
: Are the events coming from an AWS service or a third party?Service Name
: What service will be the source of events?
EventBridge will only present options relevant to the previous choices. For example, choosing AWS as Service Provider
means that no third party services are available in Service Name
.
My choices so far tell EventBrdige that S3 is the event source:
Next up is Event Type
. As EventBridge knows the events are coming from S3, the options here are very specific:
I choose Amazon S3 Event Notification
.
EventBridge now knows enough to create a rule, and offers the following JSON as an Event Pattern
:
{
"source": ["aws.s3"],
"detail-type": ["Object Access Tier Changed", "Object ACL Updated", "Object Created", "Object Deleted", "Object Restore Completed", "Object Restore Expired", "Object Restore Initiated", "Object Storage Class Changed", "Object Tags Added", "Object Tags Deleted"]
}
I’m only interested in restores, so I open the Specific Event(s)
list and choose the three Object Restore
events:
EventBridge then amends the event pattern to:
{
"source": ["aws.s3"],
"detail-type": ["Object Restore Completed", "Object Restore Initiated", "Object Restore Expired"]
}
That’s it for the source. Now EventBridge needs to know what to do when it finds something!
EventBridge Rule: Choosing A Target & Configuring Inputs
One of EventBridge’s big selling points is how it interacts with targets. There are already numerous targets, and EventBridge rules can have more than one.
I select SNS Topic
as a target then choose my s3-object-restore
SNS topic from the list:
This alone is enough for EventBridge to interact with SNS. When I save this EventBridge rule and trigger it by running an S3 object restore, I receive this email:
Although this is technically a success, some factors aren’t ideal:
- The formatting of the email is hard to read.
- There’s a lot of information here, most of which is irrelevant.
- It’s not immediately clear what this email is telling me.
To address this I can use EventBridge’s Configure Input
feature to change what is sent to the target. This feature offers four options:
Matched Events
: EventBridge passes all of the event text to the target. This is the default.Part Of The Matched Event:
EventBridge only sends part of the event text to the target.Constant (JSON text)
: None of the event text is sent to the target. EventBridge sends user-defined JSON instead.Input Transformer
: EventBridge assigns lines of event text as variables, then uses those variables in a template.
Let’s look at the input transformer.
The AWS EventBridge user guide goes into detail about the input transformer and includes a good tutorial. Having consulted these resources, I start by getting the desired JSON from the initial email:
{
"detail-type":"Object Restore Initiated",
"source":"aws.s3",
"time":"2022-02-21T12:51:21Z",
"detail":
{
"bucket":{"name":"redacted"},
"object":{"key":"redacted"}
}
}
Then I convert the JSON into an Input Path
:
{
"bucket":"$.detail.bucket.name",
"detail-type":"$.detail-type",
"object":"$.detail.object.key",
"source":"$.source",
"time":"$.time"
}
And finally specify an Input Template
:
"<source> <detail-type> at <time>. Bucket: <bucket>. Object: <object>"
EventBridge checks input templates before accepting them, and will throw an error if the input template is invalid:
I update my EventBridge rule with the new Input Transformer configuration. Time to test it out!
Testing
When I trigger an S3 object restore I receive this email moments later:
I then receive a second email when the object is ready for download:
"aws.s3 Object Restore Completed at 2022-03-04T00:15:33Z. Bucket: REDACTED. Object: REDACTED"
And a final one when the object expires:
"aws.s3 Object Restore Expired at 2022-03-05T10:12:04Z. Bucket: REDACTED. Object: REDACTED"
Success!
Before moving on, let me share the results of an earlier test. My very first input path (not included here) contained some mistakes. The input template was valid but it couldn’t read the S3 event properly, so I ended up with this:
Something to bear in mind for future rules!
Cost Analysis
Before I wrap up, let’s run through the expected costs with this setup:
- SNS: the first thousand email notifications SNS every month are included in the AWS Always Free tier, and I’m nowhere near that!
- S3: There is no change for S3 passing events to EventBridge. Charges for object storage and retrieval are out of scope for this post.
- EventBridge: All events published by AWS services are free.
There is no expected cost rise for this setup based on my current use.
Summary
In this post I’ve used EventBridge and SNS to produce free bespoke notifications at key points in the S3 object retrieval process. This offers me the following benefits:
- Reassurance: I can choose the longer S3 retrieval offerings knowing that AWS will keep me updated on progress.
- Convenience: I will know the status of retrievals without accessing the AWS console or using the CLI.
- Cost: I am less likely to forget to download retrieved objects before expiry, and therefore less likely to need to retrieve those objects again.
If this post has been useful, please feel free to follow me on the following platforms for future updates:
Thanks for reading ~~^~~