DEV Community

Cover image for Automating AWS Resource Management with Shell Scripting
Adesokan Israel
Adesokan Israel

Posted on

Automating AWS Resource Management with Shell Scripting

In the dynamic world of cloud computing, managing resources across an AWS environment can be a daunting task. With numerous services spread across different regions, keeping track of everything manually is not only time-consuming but also prone to error. To address this challenge, I’ve developed a simple yet powerful shell script that automates the process of listing all resources in an AWS account.

This script provides a quick and efficient way to gather detailed information about your AWS infrastructure, covering a wide range of services like EC2, RDS, S3, and more. Whether you’re a cloud architect responsible for multiple environments or a developer managing a single account, this tool can significantly simplify your workflow, ensuring that you always have an up-to-date inventory of your cloud resources.

In this article, I’ll walk you through the development and functionality of this script, showcasing how automation can enhance your cloud management strategy.
The focus of this project is to provide a solution for listing resources of a user
on AWS in an automated way, and basically could be used to perform cron job as the case may suit the user and the need in the line of automation.

Supported Services

The script currently supports listing resources for the following AWS services:

  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. EBS

Features

  • Multi-Region Support: The script can query resources across multiple AWS regions.
  • Service Coverage: Lists resources from major AWS services such as EC2, S3, RDS, Lambda, and more.
  • Output Formats: Results can be outputted in plain text or saved to a file for easy integration with other tools.
  • Error Handling: Includes basic error handling to manage API rate limits and service-specific issues.

Prerequisites

Before running the script, ensure you have the following:

AWS CLI: Installed and configured with the necessary permissions to query AWS resources.

Bash Shell: The script is written for Unix-like environments (Linux, macOS, WSL on Windows).

IAM Permissions: The IAM user or role running the script should have read-only permissions to all AWS services being queried.

Setup

Image description

STEPS

  1. clone the Repository:
https://github.com/Aymogul/Aws-resource-script.git
Enter fullscreen mode Exit fullscreen mode
  1. Install AWS CLI on the instance
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
Enter fullscreen mode Exit fullscreen mode

Image description

  1. Configure AWS CLI: Ensure your AWS CLI is configured with the necessary credentials and default region
aws configure
Enter fullscreen mode Exit fullscreen mode

Input your:
Access key
Secret Key
Regon
Output as JSON

  1. Make script Executable: Give execution permission to the script
chmod +x aws_resource_list.sh
Enter fullscreen mode Exit fullscreen mode

or

chmod 771 aws_resource_list.sh
Enter fullscreen mode Exit fullscreen mode

Usage

  1. Run the script This can be executed directly from the command line after you have logged into an EC2 via ssh or puTTY
./aws_resource_list.sh <aws_region> <aws_service>
Enter fullscreen mode Exit fullscreen mode

example usage

./aws_resource_list.sh us-east-1 ec2
Enter fullscreen mode Exit fullscreen mode
  1. Output: This script will run and the output of the AWS resource list will be outputed to aws_resources.txt.
./_aws_resource_list.sh > aws_resources.txt
Enter fullscreen mode Exit fullscreen mode
  1. Customization of Regions and other Variables: By default, the script will query resources in all regions. You can edit the regions variable in the script to specify particular regions:
regions="us-east-1"
Enter fullscreen mode Exit fullscreen mode

This command will list all EC2 instances in the us-east-1 region.

Image description

Image description

  1. Valid AWS Services:

The script supports various services like ec2, rds, s3, cloudfront, vpc, iam, route53, cloudwatch, cloudformation, lambda, sns, sqs, dynamodb, and ebs.

Error Handling

AWS CLI Not Installed: The script checks if AWS CLI is installed and configured. If not, it will prompt the user to install and configure it.
Invalid Service: If an unsupported service is specified, the script will return an error message.

Customization

Additional Services: You can modify the script to include additional AWS services by adding new cases to the case block.
Output Format: The script can be enhanced to format output in different ways (e.g., JSON, CSV) depending on your requirements.

Contribution

Contributions are welcome! If you have improvements or additional features, feel free to fork the repository and submit a pull request.

Top comments (0)