Build an AWS resource Tracker using shell scripting

Build an AWS resource Tracker using shell scripting

Hello #techbhairavi followers, I am sharing a beginner project step-by-step procedure to build an AWS resource tracker using shell scripting.

Objective:

  • To monitor the AWS Resources using automation.

  • We can save output locally or send it to an external dashboard.

  • The script should run itself at a particular time so, we set up a Cronjob on a Linux server.

Prerequisite:

  • AWS Access Key

  • AWS CLI bash console.

Steps:

  1. Connect to EC2 instance on Linux terminal.

ssh -i /Users/testing/Downloads/aws_testing_key.pem ubuntu@52.168.176.45

(enter your key-pair path and public IP address of the EC2 instance)

  1. Configure your credentials to communicate with AWS.

Run "aws configure"

AWS Access Key : #################

AWS secret access key: #############################

(Generate the key from AWS console Go to security credentials > Access Keys)

  1. Create a script file named aws_resource_tracker.sh

vim aws_resource_tracker.sh

#!/bin/bash

#############

# Author: Bhairavi

# Date: 12th-sept

# version: v1

# This script will report the AWS resource usage

###############

# we will be listing the following:

# AWS S3

# AWS EC2

# AWS Lambda

# AWS IAM Users

#################

# To list S3 bucket,

echo "Print list of S3 buckets"

aws s3 ls

# List EC2 Instances

echo "Print list of EC2 Instances"

aws ec2 describe-instances

# List Lambda

echo "Print list of lambda function"

aws lambda list-functions

# List IAM users

echo "Print list of IAM users"

aws iam list-users

:wq! (save and exit)

(for aws cli commands check out https://docs.aws.amazon.com/cli/latest/#)

  1. Update the permission of file

chmod 777 aws_resource_tracker.sh

  1. Run the script

./aws_resource_tracker.sh

  1. Use set -x ,

set -x is used to enable debugging for the current shell session within a script.

(Ex: #!/bin/bash

set -x

  1. To list only the instance id, We can run the following command in the shell script under EC2 details

aws ec2 describe-instances | jq '.Reservations[].Instances[].InstanceId'

  1. We can redirect the output of a command into a file

ex: aws s3 ls > resourcetracker

  1. Automating execution with crontab

To further streamline the resource tracking process we can automate script execution with crontab. Ex: you can schedule the script to run daily at 7 PM Here's how you can do it:

Run " crontab -e"

0 19 *** /path/to/aws_resource_tracker.sh

Leverage the AWS Resource Tracker script to stay organized, maintain your AWS resources, and enhance your resource management practices.

If you like my blog don't forget to follow me on LinkedIn linkedin.com/in/bhairaviw I would sincerely appreciate your support. Your engagement means a lot to me, and I'm grateful for the time spent reading this post. Thank you!:w