-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplate.yml
162 lines (154 loc) · 4.71 KB
/
template.yml
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
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: Provision the AWS Lambda function of s3-monitor - https://zoph.io
############################################################
Parameters:
############################################################
ENV:
Type: String
Default: dev
Description: Environment name
PROJECT:
Type: String
Default: no_name_project
Description: Project name
AWSREGION:
Type: String
Default: eu-west-1
Description: AWS Region
MONITORINGBUCKET:
Type: String
Default: no_bucket_vars_definied
Description: Monitored bucket name
S3PREFIX:
Type: String
Default: no_prefix_defined
Description: S3 prefix to monitor
RECIPIENTS:
Type: String
Default: [email protected]
Description: Recipients emails addresses
SENDER:
Type: String
Default: [email protected]
Description: Sender email addresses
CRONENABLED:
Type: String
Default: ENABLED
Description: Enable Schedule or not
###########################################################
Conditions:
###########################################################
IsProdEnvironment: !Equals [!Ref ENV, prod]
###########################################################
Resources:
###########################################################
s3monitor:
Type: "AWS::Serverless::Function"
Properties:
FunctionName: !Sub ${PROJECT}-${ENV}
Description: !Sub "[${ENV}] S3 Backup Monitor (${PROJECT}) - https://zoph.io"
Handler: handlers.main
Runtime: python3.10
CodeUri: ./python
MemorySize: 128
Timeout: 30
Architectures:
- arm64
Environment:
Variables:
MONITORINGBUCKET: !Ref MONITORINGBUCKET
S3PREFIX: !Ref S3PREFIX
RECIPIENTS: !Ref RECIPIENTS
SENDER: !Ref SENDER
AWSREGION: !Ref AWSREGION
Policies:
- AWSLambdaExecute # Managed Policy
- Version: "2012-10-17" # Policy Document
Statement:
- Effect: Allow
Action:
- s3:List*
Resource: !Sub "arn:aws:s3:::${MONITORINGBUCKET}"
- Effect: Allow
Action:
- ses:SendEmail
Resource: "*"
Tags:
Project: !Ref PROJECT
Environment: !Ref ENV
MonitoredBucketName: !Ref MONITORINGBUCKET
Region: !Ref AWSREGION
Reference: "https://zoph.io"
S3MonitorLogGroup:
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${PROJECT}-${ENV}"
RetentionInDays: 14 # You can adjust the retention period as needed
LambdaSchedule:
Type: AWS::Events::Rule
Properties:
State: !Ref CRONENABLED
ScheduleExpression: cron(0 10 * * ? *)
Description: !Sub Lambda Schedule for ${PROJECT}
Targets:
- Id: s3-monitor-event-schedule
Arn:
Fn::GetAtt:
- s3monitor
- Arn
LambdaInvokePermission:
# https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/CWE_Troubleshooting.html#LAMfunctionNotInvoked
Type: "AWS::Lambda::Permission"
Properties:
FunctionName: !GetAtt s3monitor.Arn
Action: "lambda:InvokeFunction"
Principal: events.amazonaws.com
SourceArn: !GetAtt LambdaSchedule.Arn
InvokConfig:
Type: AWS::Lambda::EventInvokeConfig
Properties:
FunctionName: !Ref s3monitor
Qualifier: $LATEST
MaximumRetryAttempts: 0
CloudWatchLogsMetricsFilter:
DependsOn: S3MonitorLogGroup
Type: AWS::Logs::MetricFilter
Properties:
FilterPattern: "ERROR"
LogGroupName: !Sub "/aws/lambda/${PROJECT}-${ENV}"
MetricTransformations:
- MetricValue: "1"
MetricNamespace: !Sub "${PROJECT}-${ENV}"
MetricName: "ERROR"
DefaultValue: 0
ErrorAlarm:
Type: "AWS::CloudWatch::Alarm"
Properties:
ActionsEnabled: true
AlarmActions:
- !Ref AlertingTopic
AlarmDescription: !Sub >
Alarm for ERROR on ${PROJECT} Lambda function
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
MetricName: ERROR
Namespace: !Sub "${PROJECT}-${ENV}"
OKActions:
- !Ref AlertingTopic
Period: 10
Statistic: Sum
Threshold: 1
TreatMissingData: ignore
AlertingTopic:
Type: "AWS::SNS::Topic"
Properties:
DisplayName: !Sub "[${ENV}] Alarm Topic for ${PROJECT}"
Subscription:
- Protocol: email
Endpoint: !Select [0, !Split [" ", !Ref RECIPIENTS]]
Outputs:
IsProdEnv:
Value: !If [IsProdEnvironment, true, false]