@@ -31,7 +31,7 @@ def send_sns_alert(message):
31
31
32
32
def get_instance_details (client , instance_identifier ):
33
33
"""
34
- get instance details
34
+ get the instance details
35
35
"""
36
36
try :
37
37
response = client .describe_db_instances (DBInstanceIdentifier = instance_identifier )
@@ -40,21 +40,21 @@ def get_instance_details(client, instance_identifier):
40
40
cluster_identifier = instance_info .get ('DBClusterIdentifier' , None )
41
41
return instance_class , cluster_identifier
42
42
except ClientError as e :
43
- error_message = f"Error getting DB instance details: { e } "
43
+ error_message = f"Error getting the DB instance details: { e } "
44
44
print (error_message )
45
45
send_sns_alert (error_message )
46
46
return None , None
47
47
48
48
def get_cluster_version (client , cluster_identifier ):
49
49
"""
50
- get cluster version
50
+ get the cluster version
51
51
"""
52
52
try :
53
53
response = client .describe_db_clusters (DBClusterIdentifier = cluster_identifier )
54
54
cluster_info = response ['DBClusters' ][0 ]
55
55
return cluster_info ['EngineVersion' ]
56
56
except ClientError as e :
57
- error_message = f"Error getting DB cluster version: { e } "
57
+ error_message = f"Error getting the DB cluster version: { e } "
58
58
print (error_message )
59
59
send_sns_alert (error_message )
60
60
return None
@@ -70,7 +70,7 @@ def instance_type_sorter(instance_type):
70
70
71
71
def change_instance_type (client , instance_identifier , new_instance_type ):
72
72
"""
73
- change instance type
73
+ change the instance type
74
74
"""
75
75
try :
76
76
response = client .modify_db_instance (
@@ -80,32 +80,32 @@ def change_instance_type(client, instance_identifier, new_instance_type):
80
80
)
81
81
return response , None
82
82
except ClientError as e :
83
- error_message = f"An error during the attempt to vertically scale the RDS instance { e } "
83
+ error_message = f"Error during an attempt to vertically scale the RDS instance { e } "
84
84
print (error_message )
85
85
send_sns_alert (error_message )
86
86
return None , str (e )
87
87
88
88
def get_instance_arn (client , instance_identifier ):
89
89
"""
90
- get instance arn
90
+ get the instance arn
91
91
"""
92
92
try :
93
93
instance_info = client .describe_db_instances (DBInstanceIdentifier = instance_identifier )
94
94
return instance_info ['DBInstances' ][0 ]['DBInstanceArn' ]
95
95
except ClientError as e :
96
- error_message = f"Error getting instance ARN for { instance_identifier } : { e } "
96
+ error_message = f"Error getting the instance ARN for { instance_identifier } : { e } "
97
97
print (error_message )
98
98
send_sns_alert (error_message )
99
99
return None
100
100
101
101
102
102
def add_modifying_tag (client , instance_identifier ):
103
103
"""
104
- add modifying tag and timestamp to prevent a few actions in one period
104
+ add the modifying tag and timestamp to prevent simultaneous actions at the same time
105
105
"""
106
106
instance_arn = get_instance_arn (client , instance_identifier )
107
107
if not instance_arn :
108
- print (f"ARN not found for instance { instance_identifier } " )
108
+ print (f"ARN not found for the instance { instance_identifier } " )
109
109
return
110
110
timestamp = datetime .now (timezone .utc ).isoformat ()
111
111
try :
@@ -115,15 +115,15 @@ def add_modifying_tag(client, instance_identifier):
115
115
{'Key' : 'modificationTimestamp' , 'Value' : timestamp }
116
116
]
117
117
)
118
- print (f"Added 'modifying' tag to instance { instance_identifier } " )
118
+ print (f"Added the 'modifying' tag to instance { instance_identifier } " )
119
119
except ClientError as e :
120
- error_message = f"Error adding 'modifying' tag to { instance_identifier } : { e } "
120
+ error_message = f"Error adding the 'modifying' tag to { instance_identifier } : { e } "
121
121
print (error_message )
122
122
send_sns_alert (error_message )
123
123
124
124
def any_instance_has_modifying_tag (client , cluster_instances ):
125
125
"""
126
- search if modifying tag exists
126
+ check if the modifying tag exists
127
127
"""
128
128
for member in cluster_instances :
129
129
instance_arn = get_instance_arn (client , member ['DBInstanceIdentifier' ])
@@ -169,7 +169,7 @@ def modification_timestamps(client, cluster_instances, cooldown_period):
169
169
170
170
def any_member_modifying (client , cluster_instances ):
171
171
"""
172
- checking if any cluster instance already modifing
172
+ checking if any cluster instance is being modified already
173
173
"""
174
174
for member in cluster_instances :
175
175
instance_info = client .describe_db_instances (DBInstanceIdentifier = member ['DBInstanceIdentifier' ])
@@ -193,17 +193,17 @@ def lambda_handler(event, _):
193
193
break
194
194
195
195
if db_instance_identifier is None :
196
- raise ValueError ("DBInstanceIdentifier not found in CloudWatch Alarm event" )
196
+ raise ValueError ("DBInstanceIdentifier not found in the CloudWatch Alarm event" )
197
197
198
198
_ , cluster_identifier = get_instance_details (rds_client , db_instance_identifier )
199
199
if not cluster_identifier :
200
- raise ValueError ("Instance is not part of any RDS cluster" )
200
+ raise ValueError ("Instance is not a part of any RDS cluster" )
201
201
202
202
cluster_response = rds_client .describe_db_clusters (DBClusterIdentifier = cluster_identifier )
203
203
cluster_instances = cluster_response ['DBClusters' ][0 ]['DBClusterMembers' ]
204
204
205
205
if any_member_modifying (rds_client , cluster_instances ):
206
- print ("At least one instance in the cluster is currently modifying ." )
206
+ print ("At least one instance in the cluster is currently being modified ." )
207
207
return
208
208
209
209
writer_instance_identifier , writer_instance_type = None , None
@@ -228,7 +228,7 @@ def lambda_handler(event, _):
228
228
229
229
# Ensure writer_instance_type is defined before comparing
230
230
if writer_instance_type is None :
231
- raise ValueError ("Writer instance type not found in the cluster" )
231
+ raise ValueError ("The writer instance type not found in the cluster" )
232
232
233
233
234
234
for member in cluster_instances :
@@ -237,41 +237,41 @@ def lambda_handler(event, _):
237
237
if instance_type_sorter (member_instance_type ) <= instance_type_sorter (writer_instance_type ):
238
238
is_writer_smallest = False
239
239
240
- # Check if any instance is modifying or has modifying tag
240
+ # Check if any instance is being modified or has the modifying tag
241
241
if any_instance_has_modifying_tag (rds_client , cluster_instances ):
242
- print ("An instance in the cluster has 'modifying' tag." )
242
+ print ("An instance in the cluster has the 'modifying' tag." )
243
243
return
244
244
245
245
cooldown_not_expired = modification_timestamps (rds_client , cluster_instances , MODIFY_COOLDOWN_PERIOD )
246
246
if cooldown_not_expired :
247
- message = "An attempt was made to vertically scale the RDS instance in cluster, but Cooldown period has not expired for at least one instance in the cluster."
247
+ message = "We tried to vertically scale the RDS instance in the cluster. However, the Cooldown period has not expired for at least one instance in the cluster."
248
248
print (message )
249
249
print (send_sns_alert )
250
250
return
251
251
252
252
if is_writer_smallest :
253
- # Scaling up writer
253
+ # Scaling up the writer
254
254
new_writer_instance_type = SIZE_ORDER [writer_size_index + 1 ]
255
- print (f"Selected new instance type for writer: { new_writer_instance_type } " )
255
+ print (f"Selected new instance type for the writer: { new_writer_instance_type } " )
256
256
if new_writer_instance_type != writer_instance_type :
257
- print (f"Attempting to change instance type for { writer_instance_identifier } to { new_writer_instance_type } " )
257
+ print (f"Attempting to change the instance type for { writer_instance_identifier } to { new_writer_instance_type } " )
258
258
_ , error = change_instance_type (rds_client , writer_instance_identifier , new_writer_instance_type )
259
259
if not error :
260
- message = f"Changed writer instance type to { new_writer_instance_type } "
260
+ message = f"Changed the writer instance type to { new_writer_instance_type } "
261
261
print (message )
262
262
send_sns_alert (message )
263
263
add_modifying_tag (rds_client , writer_instance_identifier )
264
264
else :
265
- error_message = f"Failed to change writer instance type. Error: { error } "
265
+ error_message = f"Failed to change the writer instance type. Error: { error } "
266
266
print (error_message )
267
267
send_sns_alert (error_message )
268
268
else :
269
- error_message = "Writer instance is already at the maximum size, scaling is not possible"
269
+ error_message = "The writer instance is at the maximum size already; scaling is not possible"
270
270
print (error_message )
271
271
send_sns_alert (error_message )
272
272
continue
273
273
274
- # Process reader instances
274
+ # Process the reader instances
275
275
smallest_size = None
276
276
min_size_index = float ('inf' )
277
277
eligible_readers = []
@@ -290,28 +290,28 @@ def lambda_handler(event, _):
290
290
reader_to_scale = random .choice (eligible_readers )
291
291
new_reader_instance_type = SIZE_ORDER [min_size_index + 1 ]
292
292
if new_reader_instance_type != smallest_size :
293
- print (f"Attempting to change instance type for { reader_to_scale } to { new_reader_instance_type } " )
293
+ print (f"Attempting to change the instance type for { reader_to_scale } to { new_reader_instance_type } " )
294
294
_ , error = change_instance_type (rds_client , reader_to_scale , new_reader_instance_type )
295
295
if not error :
296
- message = f"Changed reader instance type to { new_reader_instance_type } "
296
+ message = f"Changed the reader instance type to { new_reader_instance_type } "
297
297
print (message )
298
298
send_sns_alert (message )
299
299
add_modifying_tag (rds_client , reader_to_scale )
300
300
else :
301
- error_message = f"Failed to change reader instance type. Error: { error } "
301
+ error_message = f"Failed to change the reader instance type. Error: { error } "
302
302
print (error_message )
303
303
send_sns_alert (error_message )
304
304
else :
305
- error_message = "Reader instance is already at the maximum size, scaling is not possible"
305
+ error_message = "The reader instance is at the maximum size already; scaling is not possible"
306
306
print (error_message )
307
307
send_sns_alert (error_message )
308
308
else :
309
309
print ("No eligible readers to scale up." )
310
- send_sns_alert ("An attempt was made to vertically scale the RDS instance, but the conditions were not suitable ." )
310
+ send_sns_alert ("We tried to vertically scale the RDS instance. However, the required conditions were not met ." )
311
311
312
312
return {
313
313
'statusCode' : 200 ,
314
- 'body' : json .dumps ("Processed instances in cluster." )
314
+ 'body' : json .dumps ("Processed instances in the cluster." )
315
315
}
316
316
except ClientError as e :
317
317
error_message = f"Failed to execute the function. Error: { str (e )} "
0 commit comments