Setup Email Notification for Ec2 disk alert and Private IP using SNS topic via bash script - vetechno
Rate this post

Last Updated on January 29, 2023 by Vikash Ekka

Hey reader, if you want to

To send EC2 instance disk space alerts and Private IP using an SNS topic to an email address via a Bash script, you can use the AWS Command Line Interface (CLI) to automate the process.

So before we start

Here are a couple more things to keep in mind when using this script:

  1. You will need to have the AWS CLI installed on the system where you are running the script. You can install it by running pip install awscli
  2. Before running the script, you will need to configure your AWS credentials on the system where you are running the script. You can do this by running the aws configure command and providing your access key and secret key.
  3. Make sure that the IAM role associated with your EC2 instance has the necessary permissions to publish to an SNS topic and to access the instance metadata.
  4. This script uses the df command to check the disk usage and curl command to get the private IP. If you are running this script outside of an EC2 instance, you will need to modify the commands accordingly to get the disk usage and IP data.

You may also want to consider adding additional error handling to the script such as checking if the aws sns publish command has been executed successfully or not.

It would also be a good idea to test the script by setting the threshold very low, and then watching the email for the alert. Once you are done testing, you can set the threshold to a desired level.

You can also add a function to send message to multiple recipients, or also send message to phone number and other protocols as well.

Also Read :

How to download files and folder from s3 bucket using command line

Here is an example of a Bash script that you need to put this file in any directory / location of Amazon Linux :


# Set the threshold for disk space usage (in percentage)

# Get the current disk space usage of the EC2 instance
usage=$(df -h / | awk '{print $5}' | tail -1 | cut -d'%' -f1)

# Get the private IP of the EC2 instance

# Compare the usage to the threshold
if [ $usage -ge $threshold ]; then
  # Send an SNS notification
  aws sns publish us-east-1 --topic-arn arn:aws:sns:us-east-1:1234567890:MyTopic --message "EC2 instance disk space usage is above $threshold% and the private IP is $private_ip" --subject "EC2 Disk Space and IP Alert"
  • Replace threshold=90 as per your convenience
  • Replace the topic ARN with the SNS topic ARN you created.
  • Replace the region us-east-1 with the region where your topic (MyTopic) was created.
  • Replace the account number 1234567890 with your AWS account number.

You can also schedule this script to run at regular intervals using a tool like cron.

Step 1. Copy the above Bash script into .sh file and name as

Step 2. Give the executable permission to the file

sudo chmod +x

Step 3. Now open Terminal and type crontab -e

Step 4. Now schedule cron job at every minute for checking the disk usage.

* * * * *  /bin/bash /root/ 

This script will check the disk usage of the EC2 instance and compare it to the threshold specified. If the usage is above the threshold, it will also get the private IP of the EC2 instance and send an SNS notification to the topic you created in the above, including the private IP information.

You can also include this script in your CloudWatch event as well and schedule it accordingly.

It is important to note that this script uses the df command, which reports the file system disk space usage. If you want to check the disk space of other partition, you need to modify the command accordingly.

Also, make sure that your system has the AWS CLI installed and you have configured your credentials. If not you can configure your credentials by running aws configure command. Please note that this script uses the curl command to fetch the IP data, it may not work if you are running this script outside of an EC2 instance.

By Vikash Ekka

Hi All, My name is Vikash Ekka from India. I’m the founder and tech editor of I have completed my Graduation in BCA. I love to write technical articles like Windows, Linux & MAC Tutorials, Tips, Tricks, How To fix, Tutorials About Ethical Hacking & Cyber Security Guide, and Software Review. Currently, I have been working as an IT professional since 2018.

One thought on “Setup Email Notification for Ec2 disk alert and Private IP using SNS topic via bash script”

Leave a Reply

Your email address will not be published. Required fields are marked *