As a buzzing Cloud/DevOps guy, I recently worked on creating a Bash script to monitor AWS resources. This project allowed me to understand how to interact with AWS services programmatically and automate routine tasks. In this article, I’ll walk you through how I approached this task and built the script.
Why Create an AWS Resource Monitoring Script?
AWS offers a wide range of services, and while their Management Console is powerful, having a lightweight script to quickly monitor resources like EC2 instances, S3 buckets, and CloudWatch metrics can be very handy.
Prerequisite and Setup
Before writing the script, I ensured the following:
AWS CLI: That i had the AWS CLI installed and configured with the necessary permissions. If you don’t have it yet, you can install it from AWS CLI installation guide.
Bash Shell: That the Bash shell was available on my system. It is available on most Linux and macOS systems by default.
Script Structure
The script is divided into three main monitoring functions:
EC2 Instances: Fetches instance details like ID, state, type, and availability zone.
S3 Buckets: Lists all S3 buckets with their creation dates.
CloudWatch Metrics: Lists metrics for a specified namespace (default is AWS/EC2).
Let's dive deep into it!
Step 1: Checking AWS CLI Installation
The first thing the script does is check if the AWS CLI is installed. If not, it prompts the user to install it.
With this, we are sure that the script doesn’t proceed without the necessary tools.
Step 2: Writing the Monitoring Functions
Each function queries AWS resources using the AWS CLI and formats the output into a readable table.
Monitor EC2 Instances: The script uses aws ec2 describe-instances
to fetch details about all EC2 instances.
Monitor S3 Buckets: This function lists all S3 buckets using aws s3api list-buckets
.
Monitor CloudWatch Metrics: This function fetches CloudWatch metrics for a specific namespace. By default, it’s set to AWS/EC2, but you can modify it.
Step 3: Creating a Menu Interface
To make the script user-friendly, I added a menu interface. The user can choose which resource to monitor by typing a number.
These were the processes i followed to create the script to monitor my resources. I had some real hands-on experience with the AWS CLI and Bash scripting.
Here is the link to the full script: full script
Top comments (0)