-
Notifications
You must be signed in to change notification settings - Fork 3
/
template.yaml
150 lines (142 loc) · 4.13 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
AWSTemplateFormatVersion: 2010-09-09
Description: "earthquake-notifier"
Transform:
- AWS::Serverless-2016-10-31
Parameters:
PrefixParam:
Type: String
Description: A stack prefix used to name resources
Default: earthquake-notifier
EventBusNameParam:
Type: String
Description: The name of the EventBridge bus to use
Default: default
CenterLatLonParam:
Type: String
Description: Point to monitor for close earthquakes. LatLon format (e.g. "37.5079,15.0830")
Default: "37.5079,15.0830" # CATANIA
MinMagnitudeParam:
Type: String
Description: A float representing the minimum magnitude to alert on
Default: "3.5" # 3.5ML
MaxDistanceMetersParam:
Type: String
Description: The max distance from the center in meters to monitor for
Default: "2000000" # 200 Km
EmailAddressParam:
Type: String
Description: The email address to subscribe to for notifications
Resources:
monitorEarthquakes:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: rust-cargolambda
BuildProperties:
Binary: earthquake-notifier
Properties:
Description: Monitors for recent heartquakes matching certain conditions and send events to EventBridge
CodeUri: ./
Handler: bootstrap
Runtime: provided.al2
Architectures:
- arm64
MemorySize: 512
Timeout: 5
Environment:
Variables:
CENTER_LAT_LON: !Ref CenterLatLonParam
MIN_MAGNITUDE: !Ref MinMagnitudeParam
MAX_DISTANCE_METERS: !Ref MaxDistanceMetersParam
EVENT_BUS: !Ref EventBusNameParam
Policies:
- EventBridgePutEventsPolicy:
EventBusName: !Ref EventBusNameParam
Events:
Scheduled:
Type: Schedule
Properties:
Schedule: "rate(1 hour)"
Name: EveryHour
Description: run every hour
Enabled: true
EventsLogGroup:
Type: AWS::Logs::LogGroup
Properties:
RetentionInDays: 7
LogGroupName: !Sub "${PrefixParam}-events"
EventsLogGroupPolicy:
Type: AWS::Logs::ResourcePolicy
Properties:
PolicyName: !Sub "${PrefixParam}-events-log-policy"
PolicyDocument: !Sub >
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EventBridgetoCWLogsCreateLogStreamPolicy",
"Effect": "Allow",
"Principal": {
"Service": [
"events.amazonaws.com"
]
},
"Action": [
"logs:CreateLogStream"
],
"Resource": [
"${EventsLogGroup.Arn}"
]
},
{
"Sid": "EventBridgetoCWLogsPutLogEventsPolicy",
"Effect": "Allow",
"Principal": {
"Service": [
"events.amazonaws.com"
]
},
"Action": [
"logs:PutLogEvents"
],
"Resource": [
"${EventsLogGroup.Arn}"
],
"Condition": {
"ArnEquals": {"AWS:SourceArn": "${EventsRule.Arn}"}
}
}
]
}
NotificationTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: !Ref EmailAddressParam
Protocol: email
TopicName: !Sub "${PrefixParam}-topic"
TopicPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
PolicyDocument:
Statement:
- Principal:
Service: events.amazonaws.com
Action: sns:Publish
Effect: Allow
Resource: !Ref NotificationTopic
Topics:
- !Ref NotificationTopic
EventsRule:
Type: AWS::Events::Rule
Properties:
EventBusName: !Ref EventBusNameParam
EventPattern:
source:
- "earthquake-notifier"
Name: !Sub "${PrefixParam}-EventsRule"
State: ENABLED
Targets:
- Arn: !Ref NotificationTopic
Id: !Sub ${PrefixParam}-topic
- Arn: !Sub ${EventsLogGroup.Arn}
Id: !Sub ${PrefixParam}-logs