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
-
AWS SDK (Boto3) resource Audits
Construct dynamic queries to sweep through regional instances, volume allocations, and Elastic IPs.
-
FinOps resource pruning logic
Define resource lifecycle checkpoints to remove orphaned assets, minimizing ongoing cloud expenses.
-
Safe Tag-Based Exemption Gates
Implement parser rules checking resource tag metadata (e.g.
keep,production) to skip deleting critical elements. -
Scheduled Serverless execution
Package dependencies and schedule cron triggers using EventBridge policies to invoke functions daily.
π Step-by-Step Installation Guide
Fetch the repository to your local workspace:
git clone https://github.com/Pradeeptalari14/aws-cost-optimizer.git
cd aws-cost-optimizer
Install package dependencies in the current directory so they can be zipped together with the script:
pip install -r requirements.txt -t .
Compress scripts and libraries into a deployment zip file:
zip -r cost-optimizer.zip .
Setup a Lambda role containing permissions to access and prune resource targets:
{
"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": "*"
}
]
}
Upload the zip archive to AWS Lambda, assign the IAM role, and configure EventBridge cron scheduling:
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
π οΈ 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