Claude-Optimizing AWS Cost Management
What is the best approach to evaluate and save cost of AWS expenses?
To effectively evaluate and save on AWS expenses, I recommend a multi-pronged approach:
Continuous monitoring and proactive management are key to controlling AWS expenses effectively.
Give me pointers to best resources like github repository and open source tools that will help to generate reports of AWS costs resource consumption and triggering any kind of actions to save costs.
I'll share some valuable open-source tools and repositories for AWS cost management and automation:
For implementing these tools effectively, I recommend:
# Example using AWS SDK to generate cost reports
import boto3
from datetime import datetime, timedelta
def generate_cost_report():
client = boto3.client('ce')
response = client.get_cost_and_usage(
TimePeriod={
'Start': (datetime.now() - timedelta(days=30)).strftime('%Y-%m-%d'),
'End': datetime.now().strftime('%Y-%m-%d')
},
Granularity='DAILY',
Metrics=['UnblendedCost']
)
return response
# Example for stopping idle EC2 instances
import boto3
def stop_idle_instances():
ec2 = boto3.client('ec2')
cloudwatch = boto3.client('cloudwatch')
instances = ec2.describe_instances()
for reservation in instances['Reservations']:
for instance in reservation['Instances']:
# Check CPU utilization
metrics = cloudwatch.get_metric_statistics(
Namespace='AWS/EC2',
MetricName='CPUUtilization',
Dimensions=[{'Name': 'InstanceId', 'Value': instance['InstanceId']}],
StartTime=datetime.now() - timedelta(hours=1),
EndTime=datetime.now(),
Period=3600,
Statistics=['Average']
)
# If CPU usage is below threshold, stop instance
if metrics['Datapoints'][0]['Average'] < 5:
ec2.stop_instances(InstanceIds=[instance['InstanceId']])
Additional Tips:
Remember to:
These tools can significantly reduce manual effort in cost management while ensuring consistent policy enforcement across your AWS infrastructure.