Back to Projects
finops bot

AWS Cost Optimizer Bot

An automated FinOps helper bot written in Python and deployed as serverless AWS Lambdas. Scans regional configurations to delete orphaned EBS storage, release Elastic IPs, and stop non-prod workloads dynamically.

πŸ’‘ What We Will Learn in This Repo

  • 1

    AWS SDK (Boto3) resource Audits

    Construct dynamic queries to sweep through regional instances, volume allocations, and Elastic IPs.

  • 2

    FinOps resource pruning logic

    Define resource lifecycle checkpoints to remove orphaned assets, minimizing ongoing cloud expenses.

  • 3

    Safe Tag-Based Exemption Gates

    Implement parser rules checking resource tag metadata (e.g. keep, production) to skip deleting critical elements.

  • 4

    Scheduled Serverless execution

    Package dependencies and schedule cron triggers using EventBridge policies to invoke functions daily.

πŸ“– Step-by-Step Installation Guide

1 Clone the Repository

Fetch the repository to your local workspace:

bash
git clone https://github.com/Pradeeptalari14/aws-cost-optimizer.git
cd aws-cost-optimizer
2 Download Libraries to Local Folder

Install package dependencies in the current directory so they can be zipped together with the script:

bash
pip install -r requirements.txt -t .
3 Package Deployment Archive

Compress scripts and libraries into a deployment zip file:

bash
zip -r cost-optimizer.zip .
4 Create AWS IAM execution Role

Setup a Lambda role containing permissions to access and prune resource targets:

json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ec2:DescribeVolumes", "ec2:DeleteVolume",
        "ec2:DescribeAddresses", "ec2:ReleaseAddress",
        "ec2:DescribeInstances", "ec2:StopInstances",
        "rds:DescribeDBClusters", "rds:StopDBCluster",
        "logs:CreateLogGroup", "logs:PutLogEvents"
      ],
      "Resource": "*"
    }
  ]
}
5 Deploy and Attach Schedule

Upload the zip archive to AWS Lambda, assign the IAM role, and configure EventBridge cron scheduling:

cron
cron(0 19 * * ? *)
// Schedule triggers daily at 7:00 PM UTC to shut down non-prod instances

πŸ”„ Things You Need to Replace (Customization Checklist)

Configure these parameters inside your Python handler to adjust resource targeting filters:

Target Element File Location Placeholder / Code target
Exempted Tags list lambda_function.py ['keep', 'protection'] (add custom safety tags)
Prune work hours lambda_function.py cron(0 19 * * ? *) (adjust shutdown triggers)
Target Tags list lambda_function.py ['dev', 'stage', 'test'] (instances to stop)
Execution Regions list lambda_function.py regions = ['us-east-1'] (array of region strings to sweep)

πŸ“Š Architectural Workflow

graph TD
    EB[EventBridge Cron: 7 PM Daily] -->|Invoke Event| Lambda[Cost Optimizer Lambda]
    
    subgraph AWS Cloud Account
        Lambda -->|Query Available| EBS[EBS Volumes]
        Lambda -->|Query Unassociated| EIP[Elastic IPs]
        Lambda -->|Query Running Dev| EC2[EC2/RDS Nodes]
    end
    
    subgraph Safe Prune Logic
        EBS -->|Check Exemption Tag| EBS_Check{Keep?}
        EBS_Check -->|No| EBS_Prune[Delete volume]
        EBS_Check -->|Yes| EBS_Skip[Skip volume]
        
        EIP -->|Release| EIP_Prune[Release Address]
        EC2 -->|Stop| EC2_Prune[Stop Instances]
    end
    
    EBS_Prune -->|Log metric| CW[CloudWatch Logs]
    EIP_Prune -->|Log metric| CW
    EC2_Prune -->|Log metric| CW
            

πŸ› οΈ Useful Commands (Project Reference)

Common CLI tasks for packing and updating functions via AWS CLI:

# Package dependencies locally on Windows: pip install -r requirements.txt -t . Compress-Archive -Path * -DestinationPath cost-optimizer.zip -Force # Deploy code update directly: aws lambda update-function-code \ --function-name aws-cost-optimizer \ --zip-file fileb://cost-optimizer.zip # Invoke function manually via CLI: aws lambda invoke \ --function-name aws-cost-optimizer \ response.json
πŸ“‹ Code copied to clipboard!