Project-02: Automate AWS Resource Reports and Email Them Using Cron Job
Step 1: Create a Shell Script to List AWS Resources
#!/bin/bash
# Author: DevOps Team
# Date: 09-10-2024
# Version: v0.0.2
# Description: This script lists AWS resources for a given service and region and emails the report.
#
#below are the services that are supported by this scripz
# 1. EC2
# 2. RDS
# 3. S3
# 4. CloudFront
# 5. VPC
# 6. IAM
# 7. Route53
# 8. CloudWatch
# 9. CloudFormation
# 10. Lambda
# 11. SNS
# 12. SQS
# 13. DynamoDB
# 14. VPC
# 15. EBS
#
#The script will prompt the user to enter the AWS region and the service for which the resources need to be listed
#
#Usage: ./aws_resources_list.sh <aws_region> <aws_service>
#Example: ./aws_resource_list.sh us-east-1 ec2
################################################################################
#Check if the required number of arguments are passed
if [ $# -ne 3 ]; then
echo "Usage: $0 <aws_region> <aws_service> <email>"
echo "Example: $0 us-east-1 ec2 manager@example.com"
exit 1
fi
# Assign arguments to variables
aws_region=$1
aws_service=$2
manager_email=$3
# Check if AWS CLI is installed
if ! command -v aws &> /dev/null; then
echo "AWS CLI is not installed"
exit 1
fi
# Check if AWS CLI is configured
if [ ! -d ~/.aws ]; then
echo "AWS CLI is not configured. Please configure the AWS CLI and try again."
exit 1
fi
# Generate AWS resource report based on the service
case $aws_service in
ec2) aws ec2 describe-instances --region $aws_region ;;
rds) aws rds describe-db-instances --region $aws_region ;;
s3) aws s3api list-buckets --region $aws_region ;;
cloudfront) aws cloudfront list-distributions --region $aws_region ;;
vpc) aws ec2 describe-vpcs --region $aws_region ;;
iam) aws iam list-users --region $aws_region ;;
route53) aws route53 list-hosted-zones --region $aws_region ;;
cloudwatch) aws cloudwatch describe-alarms --region $aws_region ;;
cloudformation) aws cloudformation describe-stacks --region $aws_region ;;
lambda) aws lambda list-functions --region $aws_region ;;
sns) aws sns list-topics --region $aws_region ;;
sqs) aws sqs list-queues --region $aws_region ;;
dynamodb) aws dynamodb list-tables --region $aws_region ;;
ebs) aws ec2 describe-volumes --region $aws_region ;;
*) echo "Invalid service name"; exit 1 ;;
esac
Step 2: Open the cron configuration:
crontab -e
Add the following line to schedule the script to run every day at 7 AM:
0 7 * * * /path/to/aws_resources_list.sh us-east-1 ec2 > /tmp/aws_resources_list.log 2>&1 && mail -s "AWS Resource Report - EC2 Instances" manager@example.com < /tmp/aws_resources_list.log