Automating AWS Resource Tracking with Shell Scripting and Cron Job

WHAT IS CRON, CRONTAB and CRON JOB?

Cron is a standard Unix utility that is used to schedule commands to execute automatically at specified interval of time in Unix like operating systems.
Crontab refers to “Cron table” is a command that allows users to create, edit, update the entries of tasks to run in a file. Crontab file consists of one or more entries of commands to execute and when. It uses the Cron daemon (a background process) to manage and execute these scheduled tasks. Crontab entries consist of minute, hour, day of the month, month, day of the week, and the command to be executed.
The individual entries in the crontab file are nothing but Cron jobs.

PREREQUISITES TO DO THE HANDS-ON

Before you begin, ensure you have the AWS Command Line Interface (CLI) installed and configured with the necessary credentials. The CLI provides a command-line interface to interact with AWS services, allowing you to manage your resources programmatically. Head to the AWS documentation to install and configure the AWS CLI for your environment.

aws --version → to check version of aws cli installed
aws configure → After you run this command, you’re prompted to provide Access key and Secret key credentials. These credentials you need to create from AWS console → Click on your profile on top right → Security Credentials → Create and save somewhere safe. These credentials will help AWS CLI to authenticate and sync with AWS Console and whatever the changes you do through CLI will be reflected on your console.

STEPS INVOLVED

  1. Craft the Bash script: Create a shell script that employs AWS CLI commands to list your desired AWS resources (EC2 instances, S3 buckets, Lambda functions, IAM roles).

  2. Set up the Cron job: Utilize the power of Cron to schedule the execution of your script at designated time every day.

Write a Bash Script

Using vim editor, run vi script.sh and add the below lines.

#!/bin/bash

#Date and time at the running of the script
echo “Cron job ran at $(date)”

#List EC2 instance Ids
echo “EC2 instances:”
aws ec2 describe-instances | jq ‘.Reservations[].Instances[].InstanceId’

#List S3 Buckets
echo “S3 Buckets:”
aws s3 ls

#List Lambda Functions
echo “Lambda Functions:”
aws lambda list-functions

#List IAM Users
echo “IAM users:”
aws iam list-users

This is a simple shell script utilizing AWS CLI to talk to the AWS resources. This will simply list the resources.

Give the executable permissions to the file using command below
chmod +x script.sh

Schedule a cron Job

To schedule a cron job, run the below command. This will open a crontab file where you can list the entries of tasks to run.

Syntax: \ command_to_be_executed*

0 18 \ path_to_the_script/[script.sh](script.sh) >> output.log 2>&1*

This is in the format of minute(0), hour(18 nothing but 6PM IST), day of the month(*), month(*), day of the week(*), and the command to be executed. * refers to ‘any’. This will execute the bash script everyday daily at 6PM.

\>> is to redirect the output and save in the file output.log and will append the output to the same output file instead of over writing it each time the cron job runs, where as using > instead of >> will replace the existing output.

2>&1 is to log any errors occurred at the running of cron job.

CONCLUSION

In the world of cloud computing and fast-paced development, automating the day to day repetitive tasks is the crucial aspect of efficient work management. Remember, this project is just the beginning. This is just a simple script which gives the idea of what can be done. The script can be extended and customized further to meet the specific needs.