☁️ DevOps Day 7 — Build a Simple AWS Resource Tracker using Shell Script

 

🚀 Introduction

Welcome to Day 7 of the DevOps series!
In this project, you’ll build a simple AWS Resource Tracker using a few lines of shell scripting and the AWS CLI.

This script helps you monitor your AWS account — checking how many EC2 instances, S3 buckets, Lambda functions, and IAM users/roles you currently have.
You’ll also learn how to automate it with a cron job so it runs daily and stores a simple report.
 


🧰 Prerequisites

Before starting, make sure you have:

  • AWS CLI installed and configured → aws configure

  • Basic Linux knowledge (creating files, running scripts)

  • IAM permissions with read-only access for EC2, S3, Lambda, and IAM


💻 Step 1: Create the Script

Create a new file named aws_resource_tracker.sh

#!/usr/bin/env bash
set -euo pipefail
# Default region (update as needed)
REGION="${AWS_DEFAULT_REGION:-ap-southeast-2}"
DATE=$(date +%F)
echo "=== AWS Resource Tracker ==="
echo "Date: ${DATE}"
echo "Region: ${REGION}"
echo
# S3 Buckets (global)
S3_BUCKETS=$(aws s3api list-buckets --query 'length(Buckets)' --output text)
echo "S3 Buckets: ${S3_BUCKETS}"
# IAM Users and Roles (global)
IAM_USERS=$(aws iam list-users --query 'length(Users)' --output text)
IAM_ROLES=$(aws iam list-roles --query 'length(Roles)' --output text)
echo "IAM Users: ${IAM_USERS}"
echo "IAM Roles: ${IAM_ROLES}"
# EC2 Instances (regional)
EC2_RUNNING=$(aws ec2 describe-instances --region "$REGION" \
--filters Name=instance-state-name,Values=running \
--query 'length(Reservations[].Instances[])' --output text 2>/dev/null || echo 0)
EC2_STOPPED=$(aws ec2 describe-instances --region "$REGION" \
--filters Name=instance-state-name,Values=stopped \
--query 'length(Reservations[].Instances[])' --output text 2>/dev/null || echo 0)
echo "EC2 Running: ${EC2_RUNNING}"
echo "EC2 Stopped: ${EC2_STOPPED}"
# Lambda Functions (regional)
LAMBDA_FUNCS=$(aws lambda list-functions --region "$REGION" \
--query 'length(Functions)' --output text 2>/dev/null || echo 0)
echo "Lambda Functions: ${LAMBDA_FUNCS}"
# Save output to a report file
mkdir -p reports
REPORT_FILE="reports/aws_report_${DATE}.txt"
{
echo "AWS Resource Report - ${DATE}"
echo "Region: ${REGION}"
echo "S3 Buckets: ${S3_BUCKETS}"
echo "IAM Users: ${IAM_USERS}"
echo "IAM Roles: ${IAM_ROLES}"
echo "EC2 Running: ${EC2_RUNNING}"
echo "EC2 Stopped: ${EC2_STOPPED}"
echo "Lambda Functions: ${LAMBDA_FUNCS}"
} > "$REPORT_FILE"
echo
echo "✅ Report saved at: ${REPORT_FILE}"

⚙️ Step 2: Make It Executable

chmod +x aws_resource_tracker.sh

Then run it:

./aws_resource_tracker.sh

You’ll get an output like this:

=== AWS Resource Tracker ===
Date: 2025-10-14
Region: ap-southeast-2
S3 Buckets: 5
IAM Users: 2
IAM Roles: 7
EC2 Running: 3
EC2 Stopped: 1
Lambda Functions: 4
Report saved at: reports/aws_report_2025-10-14.txt

⏰ Step 3: Automate It with Cron

To run it automatically every morning at 7:00 AM:

crontab -e

Add this line (update your path):

0 7 * * * /bin/bash /home/ubuntu/aws_resource_tracker.sh >> /home/ubuntu/reports/cron.log 2>&1

Now it will:

  • Run daily at 7 AM

  • Save a new report in reports/

  • Log results in cron.log


🔍 Step 4: Verify and Extend

You can later expand this script to:

  • Track EBS volumes, RDS instances, or CloudWatch alarms

  • Email the daily report using AWS SES

  • Upload results to S3 for central logging

  • Integrate with Slack alerts


🌟 Summary

By the end of this project, you learned how to:

  • Use the AWS CLI to query real-time resource data

  • Automate daily reporting using cron

  • Write a simple shell script that makes API calls to AWS

“DevOps isn’t just about deploying code — it’s about automating visibility.”

Comments

Popular posts from this blog

🧩 DevOps Day 1 — Fundamentals of DevOps

DevOps Day 23 — Multi-Stage Docker Builds & Distroless Images: Build Smaller, Safer Containers

🚀 DevOps Day 2 — Understanding the SDLC and the Role of DevOps Engineers