DEV Community

Louretta
Louretta

Posted on

Automating Aws Cost Optimization Using Iac(Terraform)

This project focuses on cost optimization while also improving resource management, monitoring, and cost analysis. It’s a comprehensive approach to managing cloud resources efficiently and cost-effectively.
Incorporating these strategies can make a significant difference for small businesses, helping them thrive in a competitive landscape while maintaining financial health and operational efficiency.
Resources used for this project includes: AWS Resource Groups,
Amazon CloudWatch Dashboards, Amazon CloudWatch Alarms, AWS Budgets with email/SNS alerts, Cost and Usage Report (CUR),Aws Lambda ,S3 Bucket for CUR storage and Amazon QuickSight for visualization
*Architectural View *
Image description
***PEREQUISITES *
Basic Knowledge of AWS Services
AWS Management Console
Scripting and Automation
Understanding of Cost Management Tools

Terraform setup
Terraform was used to ensure consistency and repeatability in resource provisioning and also automating infrastructure deployment reduces errors , manual effort and Tracks infrastructure changes using version control.

Image description

Add resource groups for cost monitoring, budgets and cost and usage report: This Provides detailed billing and usage data for all AWS services

Image description

Add S3 bucket resource group: This stores Cost and Usage Reports (CUR) data in a specified folder structure. , s3 is cost effective and acts as a data source for QuickSight

Image description

Add resource groups for cloud watch dashboard alarms and Lambda archive_file: This Monitors cost-related metrics like Estimated Charges and triggers alerts for anomalies to SNS when threshold set is exceeded
Terraform archive_file was used to automate the ZIP process eliminating the need for external dependencies.

Image description
Add a python script for your Lambda function. This Automate Resource Scheduling using Lambda-based start/stop automation

Image description

Configure your Lambda function to trigger start/stop resources based on a schedules via Event Bridge (make reference to the script above)
and for best security practices, add Iam role for Lambda. This Securely grants permissions to CUR, QuickSight, Lambda, and other services.
Image description

Add Lambda Iam policy-This ensure resources only have the permissions they need and provides fine-grained control for secure infrastructure operations

Image description
run terraform init, plan and apply to deploy
Image description

Some Tips :you can use commands to output the result on console should look like this.

Image description

some of the outs via console

Image description

Always remember to clean after yourself -Run terraform destroy to clean resources
PS: S3 bucket has to be emptied before it can be deleted, best to do the console

Image description
Feel free to pull codes from my GitHub. Happy Coding
https://github.com/Louretta/Aws-cost-optimization-Automation.git

Top comments (0)