๐Ÿš€ AWS Resource Tracker Script ๐Ÿš€

๐Ÿš€ AWS Resource Tracker Script ๐Ÿš€

ยท

2 min read

Pre-Requisites:

  • AWS CLI

  • Access key

Installation of prerequisites:

๐Ÿ”ง AWS CLI:

  • On Ubuntu, use the following command:

      sudo apt install awscli
    

Install AWS CLI for your OS

Create and configure access key:

  1. Step 1:

  2. Step 2:

Script:

#!/bin/bash
###########################################################################
# ๐Ÿ› ๏ธ This script tracks resources in our AWS account ๐Ÿ› ๏ธ #
###########################################################################

##### List IAM Users #####
echo "IAM users are:" > resources.txt

echo "Before JSON Parsing" >> resources.txt
aws iam list-users >> resources.txt

echo "After JSON Parsing by filtering with username alone" >> resources.txt
aws iam list-users | jq '.Users[].UserName' >> resources.txt

##### List EC2 Instances #####
echo "EC2 Instances that we have:" >> resources.txt
aws ec2 describe-instances | jq '.Reservations[].Instances[].Tags[].Value' >> resources.txt

##### List IAM Group #####
echo "IAM Groups are:" >> resources.txt
aws iam list-groups >> resources.txt

##### List S3 Bucket #####
echo "S3 buckets are:" >> resources.txt
aws s3 ls >> resources.txt

Usage:

  • Make a .sh filetouch aws_resource_tracker.sh chmod +x aws_resource_tracker.sh

  • Open the script file: vim aws_resource_tracker.sh

  • Paste the script and save it.

  • Execute the script: ./aws_resource_tracker.sh

Output: ๐Ÿ“ resources.txt:

IAM users are:
Before JSON Parsing
... [IAM user details]
After JSON Parsing by filtering with username alone
... [Filtered usernames]

EC2 Instances that we have:
... [EC2 instance details]

IAM Groups are:
... [IAM group details]

S3 buckets are:
... [S3 bucket details]

Automate Using CRON JOB:

  • Open crontab file:

      sudo nano /etc/crontab
    
  • Add the job to run the script daily at 16:40 (4:40 pm):

      40 16 * * * ubuntu bash /home/ubuntu/aws_resource_tracker.sh
    

Did you find this article valuable?

Support vaibhav upare by becoming a sponsor. Any amount is appreciated!

ย