-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrds_slow_query_parameter_toggle.py
More file actions
89 lines (79 loc) · 3.19 KB
/
rds_slow_query_parameter_toggle.py
File metadata and controls
89 lines (79 loc) · 3.19 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
# _____ __ _ ________
# \__ \\ \/ \/ / ___/
# / __ \\ /\___ \
# (____ /\/\_//____ >
# \/ \/
################################################################################################
# This script:
# uses the boto3 library to interact with Amazon RDS, and modify the parameters from the attached
# parameter group.
################################################################################################
# Args:
# -event (dict): Dictionary containing information about the event that triggered the function.
# -context (LambdaContext): Context information about the invocation, including the function name,
# version, and ID.
################################################################################################
import boto3
def describe_and_modify_parameter(param_group_name, param_name):
client = boto3.client("rds")
# Initialize marker for pagination
marker = "String"
parameters = []
# Retrieve parameters in multiple steps
while True:
# Fetch parameters with marker
response = client.describe_db_parameters(
DBParameterGroupName=param_group_name, Marker=marker
)
parameters.extend(response["Parameters"])
# Check if there are more parameters to retrieve
if "Marker" in response:
marker = response["Marker"]
else:
break
# Search for the specified parameter
parameter_value = None
for parameter in parameters:
if parameter["ParameterName"] == param_name:
parameter_value = parameter.get("ParameterValue", "Not set")
break
# Print the value of the specified parameter
if parameter_value is not None:
print(f"The value of parameter '{param_name}' is '{parameter_value}'.")
else:
print(
f"Parameter '{param_name}' not found in parameter group '{param_group_name}' "
)
# Modify the parameter value if it's not already set to '1'
if parameter_value == "1":
response = client.modify_db_parameter_group(
DBParameterGroupName=param_group_name,
Parameters=[
{
"ParameterName": param_name,
"ParameterValue": "0",
"ApplyMethod": "immediate",
},
],
)
print(
f"Parameter {param_name} in parameter group {param_group_name} is changed to '0' successfully"
)
elif parameter_value == "0":
response = client.modify_db_parameter_group(
DBParameterGroupName=param_group_name,
Parameters=[
{
"ParameterName": param_name,
"ParameterValue": "1",
"ApplyMethod": "immediate",
},
],
)
print(
f"Parameter {param_name} in parameter group {param_group_name} is changed to '1' successfully"
)
if __name__ == "__main__":
param_group_name = "your-parameter-group-name"
param_name = "slow_query_log"
describe_and_modify_parameter(param_group_name, param_name)