-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
134 lines (127 loc) · 3.41 KB
/
template.yaml
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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Parameters:
LogGroupRetention:
Type: String
Default: 30
AllowedValues:
- 1
- 3
- 5
- 7
- 14
- 30
- 60
- 90
- 120
- 150
- 180
- 365
- 400
- 545
- 731
- 1827
- 3653
Metadata:
AWS::ServerlessRepo::Application:
Name: cloudwatch-logs-janitor
Description: Applications to optimize the CloudWatch log groups
Author: y0ssi10
SpdxLicenseId: MIT
LicenseUrl: LICENSE
ReadmeUrl: README.md
Labels: ['cloudwatch', 'logs', 'optimization']
HomePageUrl: https://github.com/y0ssi10/cloudwatch-logs-janitor
SemanticVersion: 1.0.0
SourceCodeUrl: https://github.com/y0ssi10/cloudwatch-logs-janitor
Globals:
Function:
Runtime: python3.7
MemorySize: 128
AutoPublishAlias: live
DeploymentPreference:
Type: AllAtOnce
Environment:
Variables:
RETENTION_IN_DAYS: !Ref LogGroupRetention
Resources:
SetExistingLogGroupsRetention:
Type: AWS::Serverless::Function
Properties:
CodeUri: cloudwatch_logs_janitor/
Handler: janitor.set_existing_log_groups_retention
Timeout: 900
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:DescribeLogGroups
- logs:PutRetentionPolicy
Resource: 'arn:aws:logs:*:*:log-group:*'
Events:
ScheduleEvent:
Type: Schedule
Properties:
Schedule: rate(1 day)
SetNewLogGroupRetention:
Type: AWS::Serverless::Function
Properties:
CodeUri: cloudwatch_logs_janitor/
Handler: janitor.set_new_log_group_retention
Timeout: 10
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:PutRetentionPolicy
Resource: 'arn:aws:logs:*:*:log-group:*'
Events:
SubscribeEvent:
Type: CloudWatchEvent
Properties:
Pattern:
source:
- aws.logs
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- logs.amazonaws.com
eventName:
- CreateLogGroup
DeleteLogGroups:
Type: AWS::Serverless::Function
Properties:
CodeUri: cloudwatch_logs_janitor/
Handler: janitor.delete_log_groups
Timeout: 900
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:DeleteLogGroup
- logs:DescribeLogGroups
Resource: 'arn:aws:logs:*:*:log-group:*'
- Effect: Allow
Action:
- apigateway:GET
Resource: 'arn:aws:apigateway:*::/restapis/*'
- Effect: Allow
Action:
- lambda:GetFunction
Resource: 'arn:aws:lambda:*:*:function:*'
Events:
ScheduleEvent:
Type: Schedule
Properties:
Schedule: rate(1 day)
Outputs:
SetExistingLogGroupsRetentionArn:
Value: !GetAtt SetExistingLogGroupsRetention.Arn
SetNewLogGroupRetentionArn:
Value: !GetAtt SetNewLogGroupRetention.Arn
DeleteLogGroupsArn:
Value: !GetAtt DeleteLogGroups.Arn