forked from dannysteenman/aws-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_cloudwatch_logs_retention.py
executable file
·77 lines (64 loc) · 2.04 KB
/
set_cloudwatch_logs_retention.py
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
# https://github.com/dannysteenman/aws-toolbox
#
# License: MIT
#
# This script will set a CloudWatch Logs Retention Policy to x number of days for all log groups in the region that you exported in your cli.
import argparse
import boto3
cloudwatch = boto3.client("logs")
def get_cloudwatch_log_groups():
kwargs = {"limit": 50}
cloudwatch_log_groups = []
while True: # Paginate
response = cloudwatch.describe_log_groups(**kwargs)
cloudwatch_log_groups += [log_group for log_group in response["logGroups"]]
if "nextToken" in response:
kwargs["nextToken"] = response["nextToken"]
else:
break
return cloudwatch_log_groups
def cloudwatch_set_retention(args):
retention = vars(args)["retention"]
cloudwatch_log_groups = get_cloudwatch_log_groups()
for group in cloudwatch_log_groups:
print(group)
if "retentionInDays" not in group or group["retentionInDays"] != retention:
print(f"Retention needs to be updated for: {group['logGroupName']}")
cloudwatch.put_retention_policy(
logGroupName=group["logGroupName"], retentionInDays=retention
)
else:
print(
f"CloudWatch Loggroup: {group['logGroupName']} already has the specified retention of {group['retentionInDays']} days."
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Set a retention in days for all your CloudWatch Logs in a single region."
)
parser.add_argument(
"retention",
metavar="RETENTION",
type=int,
choices=[
1,
3,
5,
7,
14,
30,
60,
90,
120,
150,
180,
365,
400,
545,
731,
1827,
3653,
],
help="Enter the retention in days for the CloudWatch Logs.",
)
args = parser.parse_args()
cloudwatch_set_retention(args)