-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.py
More file actions
431 lines (391 loc) · 16 KB
/
lambda.py
File metadata and controls
431 lines (391 loc) · 16 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import os
import boto3
import json
import time
from datetime import datetime, timedelta
from botocore.config import Config
import checker
import cloudformation_checks
import ebs_checks
import ec2_checks
import efs_checks
import eks_checks
import es_checks
import rds_checks
import route53_checks
import workspaces_checks
import vpc_checks
import logging
logger = logging.getLogger(__name__)
logger.setLevel(os.environ.get('LOG_LEVEL', logging.INFO))
ch = logging.StreamHandler()
ch.setLevel(os.environ.get('LOG_LEVEL', logging.INFO))
logger.addHandler(ch)
sns = boto3.client('sns')
# generally skip rates - this is meant to capture service limits
quota_name_exclusions = [
'concurrently running',
'queries',
'rate quota',
'rate of'
]
# these servers don't have meaningful service specific usage
skip_services = [
'app-integrations',
'acm-pca',
'amplify',
'amplifyuibuilder',
'aoss',
'appconfig',
'appfabric',
'appflow',
'appintegrations',
'application-autoscaling',
'application-cost-profiler',
'application-signals',
'b2bi',
'bcm-data-exports',
'billingconductor',
'braket',
'bugbust',
'cases',
'chime',
'cleanrooms',
'cleanrooms-ml',
'cloud9',
'cloudsearch',
'codeartifact',
'codecommit',
'codeguru-reviewer',
'codeguruprofiler',
'codestar',
'codestar-connections',
'codestar-notifications',
'comprehend',
'comprehendmedical',
'compute-optimizer',
'connect',
'connect-campaigns',
'crowdscale-usagelimitservice',
'databrew',
'datapipeline',
'datasync',
'datazone',
'dax',
'deadline',
'deepracer',
'discovery',
'evidently',
'finspace',
'fis',
'fms',
'forecast',
'frauddetector',
'gamelift',
'geo',
'globalaccelerator',
'greengrass',
'groundstation',
'internetmonitor',
'iot',
'iot1click',
'iotanalytics',
'iotcore',
'iotdeviceadvisor',
'iotevents',
'iotfleethub',
'iotfleetwise',
'iotsitewise',
'iotthingsgraph',
'iottwinmaker',
'iotwireless',
'ivs',
'ivschat',
'kafkaconnect',
'kendra',
'kendra-ranking',
'launchwizard',
'lex',
'license-manager',
'license-manager-linux-subscriptions',
'license-manager-user-subscriptions',
'lightsail',
'lookoutequipment',
'lookoutmetrics',
'lookoutvision',
'm2',
'machinelearning',
'managedblockchain',
'managedblockchain-query',
'mediaconnect',
'mediaconvert',
'medialive',
'mediapackage',
'mediapackagev2',
'mediastore',
'mediatailor',
'medical-imaging',
'mgn',
'migrationhuborchestrator',
'migrationhubstrategy',
'networkinsights',
'nimble',
'oam',
'omics',
'opsworks',
'opsworks-cm',
'organizations',
'outposts',
'panorama',
'payment-cryptography',
'pca-connector-ad',
'pca-connector-scep',
'personalize',
'pinpoint',
'private-networks',
'profile',
'proton',
'qapps',
'qbusiness',
'qldb',
'rbin',
'refactor-spaces',
'rekognition',
'resiliencehub',
'resource-explorer-2',
'robomaker',
'rolesanywhere',
'rum',
'schemas',
'scn',
'sdb',
'serverlessrepo',
'signer',
'simspaceweaver',
'sms',
'snow-device-management',
'snowball',
'supportapp',
'swf',
'tnb',
'ts',
'vendor-insights',
'verifiedpermissions',
'vmimportexport',
'voiceid',
'wam',
'wellarchitected'
]
cloudformation_registry = checker.CheckerRegistry()
cloudformation_registry.register(r'^Stack instances per stack set$', cloudformation_checks.StackInstancesPerStackSetChecker)
cloudformation_registry.register(r'^Stack count$', cloudformation_checks.StackCountChecker)
ebs_registry = checker.CheckerRegistry()
ebs_registry.register(r'^IOPS for Provisioned IOPS SSD \(io1\) volumes$', ebs_checks.ProvisionedIOPSChecker)
ebs_registry.register(r'^IOPS for Provisioned IOPS SSD \(io2\) volumes$', ebs_checks.ProvisionedIOPSChecker)
ebs_registry.register(r'^Snapshots per Region$', ebs_checks.SnapshotsPerRegionChecker)
ebs_registry.register(r'^Archived snapshots per volume$', ebs_checks.ArchivedSnapshotsPerVolumeChecker)
for volume_type in ebs_checks.VolumeTypeStorageChecker.VOLUME_TYPE_MAP.keys():
ebs_registry.register(f'^Storage for {volume_type} volumes, in TiB$', ebs_checks.VolumeTypeStorageChecker)
ec2_registry = checker.CheckerRegistry()
ec2_registry.register(r'^Running On-Demand Standard \(A, C, D, H, I, M, R, T, Z\) instances$', ec2_checks.RunningOnDemandInstancesChecker)
ec2_registry.register(r'^Client VPN endpoints per Region$', ec2_checks.ClientVPNEndpointsChecker)
ec2_registry.register(r'^AMIs$', ec2_checks.AMIsChecker)
ec2_registry.register(r'^Multicast domain associations per VPC$', ec2_checks.MulticastDomainAssociationsChecker)
ec2_registry.register(r'^Multicast Network Interfaces per transit gateway$', ec2_checks.MulticastNetworkInterfacesChecker)
ec2_registry.register(r'^New Reserved Instances per month', ec2_checks.NewReservedInstancesChecker)
ec2_registry.register(r'^Running Dedicated ([A-Za-z0-9-]+) Hosts', ec2_checks.RunningDedicatedHostsChecker)
ec2_registry.register(r'^Attachments per transit gateway', ec2_checks.AttachmentsPerTransitGatewayChecker)
ec2_registry.register(r'^Transit gateways per account', ec2_checks.TransitGatewaysPerAccountChecker)
ec2_registry.register(r'^VPN connections per VGW$', ec2_checks.VPNConnectionsPerVGWChecker)
ec2_registry.register(r'^VPN connections per region$', ec2_checks.VPNConnectionsPerRegionChecker)
ec2_registry.register(r'^Routes per transit gateway$', ec2_checks.RoutesPerTransitGatewayChecker)
ec2_registry.register(r'^Customer gateways per region$', ec2_checks.CustomerGatewaysPerRegionChecker)
ec2_registry.register(r'^AMI sharing$', ec2_checks.AMISharingChecker)
ec2_registry.register(r'^Routes per Client VPN endpoint$', ec2_checks.RoutesPerClientVPNEndpointChecker)
ec2_registry.register(r'^EC2-VPC Elastic IPs$', ec2_checks.EC2VPCElasticIPsChecker)
efs_registry = checker.CheckerRegistry()
efs_registry.register(r'^File systems per account$', efs_checks.FileSystemsPerAccountChecker)
eks_registry = checker.CheckerRegistry()
eks_registry.register(r'^Clusters$', eks_checks.ClustersChecker)
eks_registry.register(r'^Nodes per managed node group$', eks_checks.NodesPerManagedNodeGroupChecker)
eks_registry.register(r'^Fargate profiles per cluster$', eks_checks.FargateProfilesPerClusterChecker)
eks_registry.register(r'^Managed node groups per cluster$', eks_checks.ManagedNodeGroupsPerClusterChecker)
es_registry = checker.CheckerRegistry()
es_registry.register(r'^Instances per domain$', es_checks.InstancesPerDomainChecker)
es_registry.register(r'^Domains per region$', es_checks.DomainsPerRegionChecker)
rds_registry = checker.CheckerRegistry()
rds_registry.register(r'^Total storage for all DB instances$', rds_checks.TotalStorageForAllDBInstancesChecker)
rds_registry.register(r'^Manual DB cluster snapshots$', rds_checks.ManualDBClusterSnapshotsChecker)
rds_registry.register(r'^Parameter groups$', rds_checks.ParameterGroupsChecker)
rds_registry.register(r'^Manual DB instance snapshots$', rds_checks.ManualDBInstanceSnapshotsChecker)
rds_registry.register(r'^DB clusters$', rds_checks.DBClustersChecker)
rds_registry.register(r'^DB Instances$', rds_checks.DBInstancesChecker)
vpc_registry = checker.CheckerRegistry()
vpc_registry.register(r'^VPCs per Region$', vpc_checks.VPCsPerRegionChecker)
vpc_registry.register(r'^Subnets per VPC$', vpc_checks.SubnetsPerVPCChecker)
vpc_registry.register(r'^Network interfaces per Region$', vpc_checks.NetworkInterfacesPerRegionChecker)
vpc_registry.register(r'^Route tables per VPC$', vpc_checks.RouteTablesPerVPCChecker)
workspaces_registry = checker.CheckerRegistry()
workspaces_registry.register(r'^GraphicsPro WorkSpaces$', workspaces_checks.WorkspacesChecker)
workspaces_registry.register(r'^Standby WorkSpaces$', workspaces_checks.WorkspacesChecker)
workspaces_registry.register(r'^WorkSpaces$', workspaces_checks.WorkspacesChecker)
workspaces_registry.register(r'^Images$', workspaces_checks.WorkspacesImagesChecker)
route53_registry = checker.CheckerRegistry()
route53_registry.register(r'^Hosted zones$', route53_checks.HostedZonesChecker)
route53_registry.register(r'^Health checks$', route53_checks.HealthChecksChecker)
# Configure boto3 client with adaptive retries for throttling
BOTO_CONFIG = Config(
retries={
'max_attempts': 5,
'mode': 'adaptive' # Automatically handles throttling with exponential backoff
}
)
# Rate limiting delay between API calls (in seconds)
# ~6-7 requests/second, safely under AWS rate limits
API_RATE_LIMIT_DELAY = 0.15
def handler(event, context):
regions = os.environ['SERVICE_QUOTA_REGION_LIST'].split(',')
results = {}
for region in regions:
region_results = check_quotas_in_region(region)
if region_results:
results[region] = region_results
if results:
sns_topic_arn = os.environ['SNS_TOPIC_ARN']
sns.publish(
TopicArn=sns_topic_arn,
Message=json.dumps(results),
Subject="Service quotas approaching limits"
)
return
def check_quotas_in_region(region):
quota_client = boto3.client('service-quotas', region_name=region, config=BOTO_CONFIG)
ec2_client = boto3.client('ec2', region_name=region, config=BOTO_CONFIG)
threshold = float(os.environ['SERVICE_QUOTA_THRESHOLD']) / 100
results = []
try:
for service in list_all_services(quota_client):
service_code = service['ServiceCode']
logger.debug(f"Checking service {service_code} in {region}")
for quota in list_all_service_quotas(quota_client, service_code):
if service_code == 's3':
logger.debug(f"Checking quota {quota['QuotaName']} for service {service_code} in {region}")
# Skip hard limits here, we may want to manually check key hard limits separately
# But there are too many for meaningful querying
if quota['Adjustable'] == True and not any(exclusion in quota['QuotaName'].lower() for exclusion in quota_name_exclusions):
usage = get_quota_usage(quota, service_code, region)
limit = quota['Value']
if usage and limit > 0 and usage / limit >= threshold:
logger.info(f"Service {service['ServiceName']} has {usage} of {limit} ({usage / limit * 100}%) which is approaching the limit")
results.append({
'ServiceName': service['ServiceName'],
'QuotaName': quota['QuotaName'],
'Usage': usage,
'Limit': limit,
'Percentage': (usage / limit) * 100
})
except Exception as e:
logger.error(f"Error checking {region}: {str(e)}")
results.append({
'Error': f"Error checking {region}: {str(e)}"
})
return results
def list_all_services(client):
services = []
paginator = client.get_paginator('list_services')
for page in paginator.paginate():
for service in page['Services']:
if service['ServiceCode'] not in skip_services:
services.append(service)
time.sleep(API_RATE_LIMIT_DELAY)
return services
def list_all_service_quotas(client, service_code):
quotas = []
paginator = client.get_paginator('list_service_quotas')
for page in paginator.paginate(ServiceCode=service_code):
quotas.extend(page['Quotas'])
time.sleep(API_RATE_LIMIT_DELAY)
return quotas
def get_quota_usage(quota, service_code, region):
if 'UsageMetric' in quota:
value = get_cloudwatch_metric_value(boto3.client('cloudwatch', region_name=region), quota['UsageMetric'])
return value
else:
return get_service_specific_usage(service_code, quota, region)
def get_cloudwatch_metric_value(cloudwatch_client, usage_metric):
try:
dimensions = []
if 'MetricDimensions' in usage_metric and isinstance(usage_metric['MetricDimensions'], list):
for dim in usage_metric['MetricDimensions']:
if isinstance(dim, dict) and 'Name' in dim and 'Value' in dim:
dimensions.append({'Name': dim['Name'], 'Value': dim['Value']})
response = cloudwatch_client.get_metric_statistics(
Namespace=usage_metric['MetricNamespace'],
MetricName=usage_metric['MetricName'],
Dimensions=dimensions,
StartTime=datetime.utcnow() - timedelta(minutes=10),
EndTime=datetime.utcnow(),
Period=300,
Statistics=[usage_metric.get('MetricStatisticRecommendation', 'Maximum')]
)
# this will typically be no data
if response['Datapoints']:
logger.debug(f"Getting cloudwatch metric value for {usage_metric['MetricNamespace']} - {usage_metric['MetricName']}")
return response['Datapoints'][0]['Maximum']
else:
return None
except Exception as e:
print(f"Error getting metric value: {str(e)}")
return None
def get_service_specific_usage(service_code, quota, region):
if service_code == 'cloudformation':
logger.debug(f"Getting service specific usage for {quota['QuotaName']} - {service_code}")
return get_service_usage(cloudformation_registry, quota['QuotaName'], region)
elif service_code == 'ebs':
logger.debug(f"Getting service specific usage for {quota['QuotaName']} - {service_code}")
return get_service_usage(ebs_registry, quota['QuotaName'], region)
elif service_code == 'ec2':
logger.debug(f"Getting service specific usage for {quota['QuotaName']} - {service_code}")
return get_service_usage(ec2_registry, quota['QuotaName'], region)
elif service_code == 'efs':
logger.debug(f"Getting service specific usage for {quota['QuotaName']} - {service_code}")
return get_service_usage(efs_registry, quota['QuotaName'], region)
elif service_code == 'eks':
logger.debug(f"Getting service specific usage for {quota['QuotaName']} - {service_code}")
return get_service_usage(eks_registry, quota['QuotaName'], region)
elif service_code == 'es':
logger.debug(f"Getting service specific usage for {quota['QuotaName']} - {service_code}")
return get_service_usage(es_registry, quota['QuotaName'], region)
elif service_code == 'rds':
logger.debug(f"Getting service specific usage for {quota['QuotaName']} - {service_code}")
return get_service_usage(rds_registry, quota['QuotaName'], region)
elif service_code == 'route53':
logger.debug(f"Getting service specific usage for {quota['QuotaName']} - {service_code}")
return get_service_usage(route53_registry, quota['QuotaName'], region)
elif service_code == 'vpc':
logger.debug(f"Getting service specific usage for {quota['QuotaName']} - {service_code}")
return get_service_usage(vpc_registry, quota['QuotaName'], region)
elif service_code == 'workspaces':
logger.debug(f"Getting service specific usage for {quota['QuotaName']} - {service_code}")
return get_service_usage(workspaces_registry, quota['QuotaName'], region)
else:
logger.debug(f"Unsupported service - {quota['QuotaName']} - {service_code}")
return None
def get_service_usage(registry, quota_name, region):
try:
checker = registry.get_checker(quota_name)
if checker:
return checker.get_usage(region, quota_name)
else:
logger.debug(f"No checker found for {quota_name}")
return None
except Exception as e:
logger.error(f"Error getting usage: {str(e)}")
return None
# For local testing
if __name__ == "__main__":
os.environ['SERVICE_QUOTA_REGION_LIST'] = 'us-east-1'
os.environ['SERVICE_QUOTA_THRESHOLD'] = '20'
print(handler({}, None))
print(handler({}, None))
print(handler({}, None))