1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| import boto3
from datetime import datetime, timedelta
def setup_s3_cost_monitoring(bucket_name):
"""
Configura monitoreo de costos para S3
"""
cloudwatch = boto3.client('cloudwatch')
# Crear alarma para costos altos
cloudwatch.put_metric_alarm(
AlarmName=f'S3-HighCosts-{bucket_name}',
ComparisonOperator='GreaterThanThreshold',
EvaluationPeriods=1,
MetricName='EstimatedCharges',
Namespace='AWS/Billing',
Period=86400, # 24 horas
Statistic='Maximum',
Threshold=100.0, # $100
ActionsEnabled=True,
AlarmActions=[
'arn:aws:sns:us-east-1:123456789012:s3-cost-alerts'
],
AlarmDescription='Alerta cuando los costos de S3 superan $100',
Dimensions=[
{
'Name': 'Currency',
'Value': 'USD'
},
{
'Name': 'ServiceName',
'Value': 'AmazonS3'
}
]
)
# Crear dashboard personalizado
dashboard_body = {
"widgets": [
{
"type": "metric",
"properties": {
"metrics": [
["AWS/S3", "BucketSizeBytes", "BucketName", bucket_name, "StorageType", "StandardStorage"],
[".", "NumberOfObjects", ".", ".", ".", "AllStorageTypes"]
],
"period": 86400,
"stat": "Average",
"region": "us-east-1",
"title": f"S3 Storage Metrics - {bucket_name}"
}
}
]
}
cloudwatch.put_dashboard(
DashboardName=f'S3-Costs-{bucket_name}',
DashboardBody=json.dumps(dashboard_body)
)
|