Introduction :
As cloud systems get enhanced, it can be difficult to manage and track resources across services such as Amazon Web Services (AWS). Automation is critical to effective cloud administration, and in this blog, I'll demonstrate how to use a simple Bash script to report on AWS resource utilization. This script will retrieve information on AWS S3 buckets, EC2 instances, Lambda functions, and IAM users.
Script Overview :
The Bash script was developed to be a simple tool for developers to quickly collect essential details about their AWS services. It uses AWS CLI commands to retrieve and show data, giving it an effective way to monitor AWS usage from the command line.
Script's Breakdown :
Here's the breakdown of what the script does.
Initial Setup
The script starts with a basic header that includes metadata such as the author, date, and version. This is good practice for maintaining and documenting your scripts, especially as they evolve over time.
#!/bin/bash ########################## #Author : Reshma #Date : 02 September 2024 # #Version : v1 #This script will report the aws usage # ########################## set -x
The
set -x
command is used for debugging purposes. It prints each command to the terminal as it is executed, which can be helpful for troubleshooting.Listing AWS S3 Buckets
# list s3 Buckets echo "Print list of s3 buckets" aws s3 ls
The first resource the script checks is AWS S3. It uses the
aws s3 ls
command to list all S3 buckets in your AWS account.Listing EC2 Instances
#list ec2 instances
echo "Print list of ec2 instances"
aws ec2 describe-instances | jq '.Reservations[].Instances[].InstanceId'
Next, the script lists all running EC2 instances. The aws ec2 describe-instances
command fetches detailed information about the EC2 instances, but we filter the output to only show the instance IDs using jq
.
Listing Lambda Functions
#list aws lambda echo "Print list of lambda functions" aws lambda list-functions
Lambda functions are becoming increasingly popular for serverless architectures. This part of the script lists all Lambda functions in your account using
aws lambda list-functions
.Listing IAM Users
Finally, the script lists all IAM users in your AWS account with the
aws iam list-users
command.#list IAM users echo "Print list of iam users" aws iam list-users
Final Script :
#!/bin/bash ########################## #Author : Reshma #Date : 02 October 2024 # #Version : v1 #This script will report the aws usage # ########################## set -x # list s3 Buckets echo "Print list of s3 buckets" aws s3 ls #list ec2 instances echo "Print list of ec2 instances" aws ec2 describe-instances | jq '.Reservations[].Instances[].InstanceId' #list aws lambda echo "Print list of lambda functions" aws lambda list-functions #list IAM users echo "Print list of iam users" aws iam list-users
Conclusion
With this simple Bash script, you can quickly obtain a snapshot of your AWS resource usage. Whether you are an AWS administrator or a developer, this script can save you time and effort in managing your cloud infrastructure.
Feel free to customize this script to fit your specific needs, and happy cloud computing!