A comprehensive Model Context Protocol (MCP) server for AWS cost analysis and optimization recommendations, designed to work seamlessly with Amazon Q CLI and other MCP-compatible clients.
# Clone the repository
git clone https://github.com/aws-samples/sample-cfm-tips-mcp.git
cd sample-cfm-tips-mcp
# Install dependencies
pip install -r requirements.txt
# Configure AWS credentials
aws configure
# Add the MCP server config to Amazon Q using the mcp_runbooks.json as a template
vi ~/.aws/amazonq/mcp.json
# Example usage in Q chat:
"Run comprehensive cost analysis for us-east-1"
"Find unused EBS volumes costing money"
"Generate EC2 right-sizing report in markdown"
Add-on AWS Pricing MCP Server MCP server for accessing real-time AWS pricing information and providing cost analysis capabilities https://github.com/awslabs/mcp/tree/main/src/aws-pricing-mcp-server
# Example usage with Add-on AWS Pricing MCP Server:
"Review the CDK by comparing it to the actual spend from my AWS account's stackset. Suggest cost optimization opportunities for the app accordingly"
- Cost Explorer - Retrieve cost data and usage metrics
- Cost Optimization Hub - Get AWS cost optimization recommendations
- Compute Optimizer - Right-sizing recommendations for compute resources
- Trusted Advisor - Cost optimization checks and recommendations
- Performance Insights - RDS performance metrics and analysis
- CUR Reports - Cost and Usage Report analysis from S3
- π§ EC2 Right Sizing - Identify underutilized EC2 instances
- πΎ EBS Optimization - Find unused and underutilized volumes
- ποΈ RDS Optimization - Identify idle and underutilized databases
- β‘ Lambda Optimization - Find overprovisioned and unused functions
- π Comprehensive Analysis - Multi-service cost analysis
- Real CloudWatch Metrics - Uses actual AWS metrics for analysis
- Multiple Output Formats - JSON and Markdown report generation
- Cost Calculations - Estimated savings and cost breakdowns
- Actionable Recommendations - Priority-based optimization suggestions
sample-cfm-tips-mcp/
βββ playbooks/ # CFM Tips optimization playbooks engine
βββ services/ # AWS Services as datasources for the cost optimization
βββ mcp_server_with_runbooks.py # Main MCP server
βββ runbook_functions.py # Cost optimization runbook implementations
βββ mcp_runbooks.json # Template file for MCP configuration file
βββ requirements.txt # Python dependencies
βββ test_runbooks.py # Integration tests
βββ diagnose_cost_optimization_hub_v2.py # Diagnostic utilities
βββ RUNBOOKS_GUIDE.md # Detailed usage guide
βββ README.md # Project ReadMe
Your AWS credentials need these permissions. The MCP server uses your role to perform actions, hence proceed with least privilege access only. The below creates an IAM policy with for list, read and describe actions only:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cost-optimization-hub:ListEnrollmentStatuses",
"cost-optimization-hub:ListRecommendations",
"cost-optimization-hub:GetRecommendation",
"cost-optimization-hub:ListRecommendationSummaries",
"ce:GetCostAndUsage",
"ce:GetCostForecast",
"compute-optimizer:GetEC2InstanceRecommendations",
"compute-optimizer:GetEBSVolumeRecommendations",
"compute-optimizer:GetLambdaFunctionRecommendations",
"ec2:DescribeInstances",
"ec2:DescribeVolumes",
"rds:DescribeDBInstances",
"lambda:ListFunctions",
"cloudwatch:GetMetricStatistics",
"s3:ListBucket",
"s3:ListObjectsV2",
"support:DescribeTrustedAdvisorChecks",
"support:DescribeTrustedAdvisorCheckResult",
"pi:GetResourceMetrics"
],
"Resource": "*"
}
]
}
- Python 3.11 or higher
- AWS CLI configured with appropriate credentials
- Amazon Q CLI (for MCP integration) - https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-installing.html
-
Clone the Repository
git clone https://github.com/aws-samples/sample-cfm-tips-mcp.git cd sample-cfm-tips-mcp
-
Install Dependencies
pip install -r requirements.txt
-
Configure AWS Credentials
aws configure # Or set environment variables: # export AWS_ACCESS_KEY_ID=your_access_key # export AWS_SECRET_ACCESS_KEY=your_secret_key # export AWS_DEFAULT_REGION=us-east-1
-
Apply IAM Permissions
- Create an IAM policy with the permissions listed above
- Attach the policy to your IAM user or role
-
Test the Installation
python3 test_runbooks.py
-
Start the MCP Server
python3 mcp_server_with_runbooks.py &
-
Start the Q Chat
q chat
get_cost_explorer_data
- Retrieve AWS cost and usage datalist_coh_enrollment
- Check Cost Optimization Hub enrollmentget_coh_recommendations
- Get cost optimization recommendationsget_coh_summaries
- Get recommendation summariesget_compute_optimizer_recommendations
- Get compute optimization recommendations
ec2_rightsizing
- Analyze EC2 instances for right-sizing opportunitiesec2_report
- Generate detailed EC2 optimization reports
ebs_optimization
- Analyze EBS volumes for optimizationebs_unused
- Identify unused EBS volumesebs_report
- Generate EBS optimization reports
rds_optimization
- Analyze RDS instances for optimizationrds_idle
- Identify idle RDS instancesrds_report
- Generate RDS optimization reports
lambda_optimization
- Analyze Lambda functions for optimizationlambda_unused
- Identify unused Lambda functionslambda_report
- Generate Lambda optimization reports
comprehensive_analysis
- Multi-service cost analysis
list_cur_reports
- List Cost and Usage Reports in S3get_trusted_advisor_checks
- Get Trusted Advisor recommendationsget_performance_insights_metrics
- Get RDS Performance Insights data
"Get my AWS costs for the last month"
"Show me cost optimization recommendations"
"What are my biggest cost drivers?"
"Find underutilized EC2 instances in us-east-1"
"Show me unused EBS volumes that I can delete"
"Identify idle RDS databases"
"Find unused Lambda functions"
"Generate a comprehensive cost optimization report"
"Create an EC2 right-sizing report in markdown format"
"Generate an EBS optimization report with cost savings"
"Run comprehensive cost analysis for all services in us-east-1"
"Analyze my AWS infrastructure for cost optimization opportunities"
"Show me immediate cost savings opportunities"
-
Cost Optimization Hub Not Working
python3 diagnose_cost_optimization_hub_v2.py
-
No Metrics Found
- Ensure resources have been running for at least 14 days
- Verify CloudWatch metrics are enabled
- Check that you're analyzing the correct region
-
Permission Errors
- Verify IAM permissions are correctly applied
- Check AWS credentials configuration
- Ensure Cost Optimization Hub is enabled in AWS Console
-
Import Errors
# Check Python path and dependencies python3 -c "import boto3, mcp; print('Dependencies OK')"
- Check the RUNBOOKS_GUIDE.md for detailed usage instructions
- Run the diagnostic script:
python3 diagnose_cost_optimization_hub_v2.py
- Run integration tests:
python3 test_runbooks.py
- Immediate Cost Savings - Identify unused resources for deletion
- Right-Sizing Opportunities - Optimize overprovisioned resources
- Real Metrics Analysis - Uses actual CloudWatch data
- Actionable Reports - Clear recommendations with cost estimates
- Comprehensive Coverage - Analyze EC2, EBS, RDS, Lambda, and more
- Easy Integration - Works seamlessly with Amazon Q CLI
The CFM Tips cost optimization server can help you:
- Identify cost savings on average
- Find unused resources costing hundreds of dollars monthly
- Right-size overprovisioned instances for optimal performance/cost ratio
- Optimize storage costs through volume type recommendations
- Eliminate idle resources that provide no business value
We welcome contributions! Please see our contributing guidelines:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.