How to create Cloudwatch alarms for an S3 Bucket on AWS
Cloudwatch provides Storage Metrics and Request Metrics for S3 Buckets. Storage Metrics come up with no extra cost and provided once a day whereas Request Metrics are available at 1-minute intervals and incur charges. Also, Cloudwatch provides Replication metrics. In this article, we will see how to create Cloudwatch alarms for S3 Bucket on AWS of type Storage Metric for NumberOfObjects.
To know more about the types of Metrics available, visit the official documentation of AWS here.
Pre-requisites
- AWS Account(Create if you don’t have one).
- S3 Bucket(Click here to learn to create an S3 Bucket on AWS)
- SNS Topic(Click here to learn to create an SNS topic on AWS using Cloudformation).
What will we do?
- Login to AWS.
- Create an alarm for the S3 Bucket
- Delete the alarm
Login to AWS
Click here to go to AWS Login Page.
When you hit the above link you will see a login page, enter your credentials to go to the AWS account.
Once you successfully login into your AWS account you will see its Management Console as follows.
Create an alarm for the S3 Bucket
In the search box at the top of the screen search for Cloudwatch. Click on the Cloudwatch result to go to the main dashboard of the Cloudwatch service.
You will see the main dashboard of the Cloudwatch as follows. Click on "Alarms" in the left panel.
Since there are no alarms in my account under the selected region we don't see anything listed down. To create an alarm click on the "Create alarm" button.
Click on the "Select metric" button to select a metric from the available list for S3 Buckets.
Here, search for "S3" to filter out the results and then click on Storage Metrics.
From the available list, you can choose the S3 bucket and the Metric Name. You can select only one at a time to create an alarm. Click on the "Select metric" button to proceed.
Since we have selected a metric of type "NumberOfObjects", we get an option under Conditions to specify a threshold wherein we can specify a value that will be compared against the number of objects in the bucket. If this specified condition becomes true the alarm is triggered. Click on the "Next" button to proceed.
On this screen, you can select an existing SNS topic or create a new one to send alert notifications on it. Here, I have selected an SNS topic from the existing topics. Proceed further to finish the alert creation.
Here, give a name to the alert, add a description to it and click on the "Next" button.
This is the last step where you can review configurations and create an alarm if everything looks good. Clicking on the "Create alarm" button will create an alarm for you.
Once the alarm is successfully created you can see it on the console. If you observe, you can see that the state of the alarm is "Insufficient data", this will take a few minutes to analyze the state and update accordingly.
After a few minutes depending on the condition the state will be changed. Here, the condition matches as the number of objects in the S3 bucket is greater than the number we specified in the alarm. As the condition matches the alarm goes into an "In alarm" state.
As the alarm triggers it sends a notification to SNS. The SNS topic I selected contains a subscription endpoint as my email ID, due to this the alert notification is sent to my email ID.
Now, to see what happens if the condition does not match, we can change the threshold value to something greater than the number of objects we have in the S3 bucket. I have changed the threshold value to 100 and updated the alarm.
Upon changing the value the condition does not match and the alarm is not triggered hence the state is changed to "Ok".
Delete the Alarm
When you no longer need the alarm you can easily delete it. To delete the alarm, select the alarm to be deleted and click on the Actions --> Delete button.
You will see a pop-up screen, click on the "Delete" button to confirm your deletion.
Once you delete the alarm it cannot be restored, so be careful while deleting the alarms.
Conclusion
In this article, we went through the steps to create an alarm for the S3 bucket for the Number of objects. We saw how the alarm transits from one state to another based on the condition specified and sends notifications to the selected SNS Topic.