AWS Infrastructure Inventory Discovery Blog

Streamline Your AWS Inventory with Automated Discovery and Reporting

Managing resources across multiple AWS accounts and regions can be a daunting task, especially as your cloud infrastructure grows. To simplify this process, I have developed an AWS Inventory Discovery tool that scans all your AWS accounts across all regions and compiles a comprehensive, searchable HTML report. In this blog, I'll walk you through the features, benefits, and the technical details of this solution.

Introduction

As organizations scale their use of AWS, keeping track of resources scattered across various accounts and regions becomes increasingly challenging. Manual inventory management is not only time-consuming but also prone to errors. This is where the AWS Inventory Discovery tool comes in.

Features

  • Comprehensive Scanning: The tool scans all your AWS accounts across all regions, ensuring no resource is left unaccounted for.
  • Detailed Reporting: Generates a detailed HTML report that lists all discovered resources, including their ARNs and regions.
  • Searchable Interface: The HTML report includes a search functionality, making it easy to find specific resources quickly.
  • Visual Representation: Includes pie charts to provide a visual summary of your resources, helping you quickly understand the distribution of resources across various dimensions.
  • User-Friendly: The generated report is easy to navigate, allowing users to drill down into resource details with just a few clicks.

Benefits

  • Improved Visibility: Gain a clear overview of all your AWS resources across accounts and regions.
  • Time Savings: Automate the discovery process, freeing up time for your team to focus on more critical tasks.
  • Error Reduction: Minimize the risk of overlooking resources or making mistakes in manual inventories.
  • Enhanced Security: Quickly identify and manage resources to ensure compliance with security policies and best practices.

How It Works

Prerequisites

Before you begin, ensure you have the following set up:

  • AWS CLI: Install the AWS Command Line Interface (CLI).
  • pip install awscli
  • AWS Credentials: Export your AWS profile or access keys and secrets, and specify the region.
  • export AWS_PROFILE=your-aws-profile
    # Or
    export AWS_ACCESS_KEY_ID=your-access-key-id
    export AWS_SECRET_ACCESS_KEY=your-secret-access-key
    export AWS_DEFAULT_REGION=your-default-region

Bash Script

    #!/bin/bash
    
    # Ensure AWS CLI is installed
    if ! command -v aws &> /dev/null
    then
        echo "AWS CLI could not be found. Please install it before proceeding."
        exit
    fi
    
    # Set AWS credentials and region
    export AWS_PROFILE=your-aws-profile
    # Or
    export AWS_ACCESS_KEY_ID=your-access-key-id
    export AWS_SECRET_ACCESS_KEY=your-secret-access-key
    export AWS_DEFAULT_REGION=your-default-region
    
    #!/bin/bash
    
    # Script Name: aws_inventory_discovery.sh
    # Author: Praveen HA
    # Description: This script automates the discovery of AWS resources across multiple accounts and regions and generates a detailed HTML report.
    
    # Ensure AWS CLI is installed
    AccountID=""
    regions=$(aws ec2 describe-regions --query "Regions[].RegionName" --output text)
    #regions=(ap-south-1 eu-north-1  eu-west-3  eu-west-1  ap-northeast-3  ap-northeast-2  ap-northeast-1  ca-central-1  sa-east-1   ap-southeast-1  ap-southeast-2  eu-central-1  us-east-1  us-east-2  us-west-1  us-west-2 )
    
    # Initialize arrays to store data
    declare -a summary_data=()
    declare -a detailed_data=()
    
    # Fetch resources from all regions
    for region in $regions; do
        # Fetch resource ARNs and process them
        data=$(aws --region $region resourcegroupstaggingapi get-resources | jq -r '.ResourceTagMappingList[].ResourceARN' | \
        awk -v region=$region -F '[:/]' '
            {
                resourceType = "unknown"
                if ($3 == "s3") {
                    resourceType = "bucket"
                } else if ($3 == "sns") {
                    resourceType = "topic"
                } else {
                    resourceType = $6  
                }
    
                # Construct a unique key for each service-resource pair
                pair = $3 ":" resourceType
                count[pair]++
            }
            END {
                for (pair in count) {
                    split(pair, s, ":")  # Split the pair back into service and resource
                    service = s[1]
                    resource = s[2]
                    print "{\"service\":\"" service "\", \"resource\":\"" resource "\", \"count\":" count[pair] "}"
                }
            }
        ')
    
        # Append to summary_data
        summary_data+=($data)
    
        # Fetch detailed resource information
        detailed_info=$(aws resourcegroupstaggingapi get-resources --region $region | jq -r --arg region $region '.ResourceTagMappingList[] | {Resource_Arn: .ResourceARN, Region: ($region // "Global"), Detailed_Service: (.ResourceARN | split(":")[2])}')
    
        # Append to detailed_data
        detailed_data+=($detailed_info)
    done
    
    # Process summary data
    summary_json=$(printf "%s\n" "${summary_data[@]}" | jq -s 'group_by(.service, .resource) | map({service: .[0].service, resource: .[0].resource, count: map(.count) | add})')
    total_resources=$(echo "$summary_json" | jq 'map(.count) | add')
    
    # Process detailed data
    detailed_json=$(printf "%s\n" "${detailed_data[@]}" | jq -s .)
    
    # Generate HTML
    cat < index.html
    
    
    
        
        
        AWS Resources
        
    
    
        

    AWS Resources for Account ID: $AccountID

    Total Resources: $total_resources
    Service Resource Count

    Detailed AWS Resources

    Resource ARN Region Detailed Service
    EOF

Generating the Report

Once the tool completes the scan, it generates an HTML report in the output directory. The report includes a search bar for quickly locating resources by their ARNs or regions and pie charts to visualize the distribution of resources.

Example Report

Here’s a glimpse of what the generated report looks like:

Example Report

Use Cases

  • Audit and Compliance: Ensure that all resources are accounted for during audits.
  • Cost Management: Identify unused or underutilized resources to optimize costs.
  • Security Review: Regularly review your resources to ensure they comply with your security standards.

Conclusion

The AWS Inventory Discovery tool is a powerful solution for managing AWS resources across multiple accounts and regions. By automating the discovery process, providing a detailed, searchable report, and including visual summaries with pie charts, this tool helps organizations save time, reduce errors, and maintain better control over their cloud infrastructure.

Clone the AWS Inventory Discovery tool and get started today!

Comments

Popular posts from this blog

AWS CLI Get Security Group ID with Name.[ wild card support] Bash Script

AWS CLI Get Security Group ID with Name. Python Boto3 [ wild card support] Python Boto3

AMI Age Calculator of Running AWS EC2 Instances and Generate CSV Report