-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
502 lines (500 loc) · 18.4 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
Transform: AWS::Serverless-2016-10-31
Parameters:
TelegramBotToken:
Description: Bot Token for Telegram
Type: String
TelegramUserChatId:
Description: User Chat ID for Telegram
Type: String
TelegramGroupChatId:
Description: Group Chat ID for Telegram
Type: String
FrequencyFilterPattern:
Description: Frequency pattern to filter Alerts
Type: String
Resources:
HamAlertApi:
Type: AWS::Serverless::Api
Properties:
Name: !Sub
- ${ResourceName} From Stack ${AWS::StackName}
- ResourceName: HamAlertApi
StageName: Prod
DefinitionBody:
openapi: '3.0'
info: {}
paths:
/notify:
post:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HamAlertProcessFunction.Arn}/invocations
responses: {}
EndpointConfiguration: REGIONAL
TracingEnabled: true
Cors:
MaxAge: 5
HamAlertProcessFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: HamAlertProcessFunction
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: HamAlertProcessFunction
CodeUri: src/HamAlertProcessFunction
Handler: handler.handler
Runtime: python3.12
MemorySize: 3008
Timeout: 30
Tracing: Active
Events:
HamAlertApiPOSTnotify:
Type: Api
Properties:
Path: /notify
Method: POST
RestApiId: !Ref HamAlertApi
Environment:
Variables:
TELEGRAM_GROUP_ID: !Ref TelegramGroupChatId
TELEGRAM_USER_ID: !Ref TelegramUserChatId
TELEGRAMNOTIFYFUNCTION_FUNCTION_NAME: !Ref TelegramNotifyFunction
TELEGRAMNOTIFYFUNCTION_FUNCTION_ARN: !GetAtt TelegramNotifyFunction.Arn
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref TelegramNotifyFunction
LoggingConfig:
LogFormat: Text
LogGroup: !Ref SotaWatchtowerMasterLogGroup
RequestsLayer:
Type: AWS::Serverless::LayerVersion
Properties:
Description: !Sub
- Stack ${AWS::StackName} Layer ${ResourceName}
- ResourceName: RequestsLayer
ContentUri: src/RequestsLayer
RetentionPolicy: Delete
Metadata:
BuildMethod: python3.12
GetSotaAlertsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: GetSotaAlertsFunction
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: GetSotaAlertsFunction
CodeUri: src/GetSotaAlertsFunction
Handler: handler.handler
Runtime: python3.12
MemorySize: 3008
Timeout: 30
Tracing: Active
Layers:
- !Ref RequestsLayer
Environment:
Variables:
FREQUENCY_FILTER_PATTERN: !Ref FrequencyFilterPattern
SOTAALERTSTABLE_TABLE_NAME: !Ref SotaAlertsTable
SOTAALERTSTABLE_TABLE_ARN: !GetAtt SotaAlertsTable.Arn
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref SotaAlertsTable
LoggingConfig:
LogFormat: Text
LogGroup: !Ref SotaWatchtowerMasterLogGroup
DailyBriefingFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: DailyBriefingFunction
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: DailyBriefingFunction
CodeUri: src/DailyBriefingFunction
Handler: handler.handler
Runtime: python3.12
MemorySize: 3008
Timeout: 30
Tracing: Active
Environment:
Variables:
TELEGRAM_GROUP_ID: !Ref TelegramGroupChatId
GETSOTAALERTSFUNCTION_FUNCTION_NAME: !Ref GetSotaAlertsFunction
GETSOTAALERTSFUNCTION_FUNCTION_ARN: !GetAtt GetSotaAlertsFunction.Arn
TELEGRAMNOTIFYFUNCTION_FUNCTION_NAME: !Ref TelegramNotifyFunction
TELEGRAMNOTIFYFUNCTION_FUNCTION_ARN: !GetAtt TelegramNotifyFunction.Arn
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref GetSotaAlertsFunction
- LambdaInvokePolicy:
FunctionName: !Ref TelegramNotifyFunction
LoggingConfig:
LogFormat: Text
LogGroup: !Ref SotaWatchtowerMasterLogGroup
TelegramNotifyFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: TelegramNotifyFunction
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: TelegramNotifyFunction
CodeUri: src/TelegramNotifyFunction
Handler: handler.handler
Runtime: python3.12
MemorySize: 3008
Timeout: 30
Tracing: Active
Layers:
- !Ref RequestsLayer
Environment:
Variables:
TELEGRAM_BOT_TOKEN: !Ref TelegramBotToken
LoggingConfig:
LogFormat: Text
LogGroup: !Ref SotaWatchtowerMasterLogGroup
GetSotaSpotsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: GetSotaSpotsFunction
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: GetSotaSpotsFunction
CodeUri: src/GetSotaSpotsFunction
Handler: handler.handler
Runtime: python3.12
MemorySize: 3008
Timeout: 30
Tracing: Active
Layers:
- !Ref RequestsLayer
LoggingConfig:
LogFormat: Text
LogGroup: !Ref SotaWatchtowerMasterLogGroup
ActivationZoneMonitorFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: ActivationZoneMonitorFunction
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: ActivationZoneMonitorFunction
CodeUri: src/ActivationZoneMonitorFunction
Handler: handler.handler
Runtime: python3.12
MemorySize: 3008
Timeout: 30
Tracing: Active
Environment:
Variables:
TELEGRAM_GROUP_ID: !Ref TelegramGroupChatId
GETSOTAALERTSFUNCTION_FUNCTION_NAME: !Ref GetSotaAlertsFunction
GETSOTAALERTSFUNCTION_FUNCTION_ARN: !GetAtt GetSotaAlertsFunction.Arn
GETSOTASPOTSFUNCTION_FUNCTION_NAME: !Ref GetSotaSpotsFunction
GETSOTASPOTSFUNCTION_FUNCTION_ARN: !GetAtt GetSotaSpotsFunction.Arn
SOTAALERTSTABLE_TABLE_NAME: !Ref SotaAlertsTable
SOTAALERTSTABLE_TABLE_ARN: !GetAtt SotaAlertsTable.Arn
TELEGRAMNOTIFYFUNCTION_FUNCTION_NAME: !Ref TelegramNotifyFunction
TELEGRAMNOTIFYFUNCTION_FUNCTION_ARN: !GetAtt TelegramNotifyFunction.Arn
Layers:
- !Ref RequestsLayer
- !Ref GeopyLayer
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref GetSotaAlertsFunction
- LambdaInvokePolicy:
FunctionName: !Ref GetSotaSpotsFunction
- DynamoDBCrudPolicy:
TableName: !Ref SotaAlertsTable
- LambdaInvokePolicy:
FunctionName: !Ref TelegramNotifyFunction
LoggingConfig:
LogFormat: Text
LogGroup: !Ref SotaWatchtowerMasterLogGroup
GeopyLayer:
Type: AWS::Serverless::LayerVersion
Properties:
Description: !Sub
- Stack ${AWS::StackName} Layer ${ResourceName}
- ResourceName: GeopyLayer
ContentUri: src/GeopyLayer
RetentionPolicy: Delete
Metadata:
BuildMethod: python3.12
SotaAlertsTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: callsign
AttributeType: S
- AttributeName: summit
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: callsign
KeyType: HASH
- AttributeName: summit
KeyType: RANGE
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
TimeToLiveSpecification:
AttributeName: expiration
Enabled: true
AprsMonitorInstance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t3.micro
ImageId: ami-0e872aee57663ae2d
IamInstanceProfile: !Ref AprsMonitorInstanceProfile
SecurityGroupIds:
- !Ref AprsMonitorSecurityGroup
UserData: !Base64
Fn::Sub: |
#!/bin/bash
echo "ACTIVATIONZONEMONITORFUNCTION_FUNCTION_ARN=${ActivationZoneMonitorFunction.Arn}" >> /etc/profile
echo "ACTIVATIONZONEMONITORFUNCTION_FUNCTION_ARN=${ActivationZoneMonitorFunction.Arn}" >> /etc/environment
echo "AWS_REGION=${AWS::Region}" >> /etc/profile
echo "AWS_REGION=${AWS::Region}" >> /etc/environment
# Install necessary packages
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y git htop
# Clone the GitHub repository
cd /home/ubuntu/
git clone https://github.com/DO4AF/sota-watchtower.git
# Change ownership of the cloned repository to the 'ubuntu' user
sudo chown -R ubuntu:ubuntu /home/ubuntu/sota-watchtower
# Set permissions to 777 (note: this is very permissive, consider using 755 or 775 instead)
sudo chmod -R 777 /home/ubuntu/sota-watchtower
# Run the UserData script from the repository
cd /home/ubuntu/sota-watchtower/ec2/aprs-listener/
sudo chmod +x install.sh
./install.sh
Tags:
- Key: Name
Value: SotaWatchtower-AprsMonitor
AprsMonitorSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow SSH and HTTP access
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
AprsMonitorInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref AprsMonitorInstanceRole
AprsMonitorInstanceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: EC2InvokeLambdaPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource: !GetAtt ActivationZoneMonitorFunction.Arn
SotaWatchtowerMasterLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: /sotawatchtower/MasterLogGroup
RetentionInDays: 90
SotaWatchtowerDashboard:
Type: AWS::CloudWatch::Dashboard
Properties:
DashboardBody:
Fn::Sub: |
{
"start": "-PT72H",
"widgets": [
{
"height": 10,
"width": 10,
"y": 0,
"x": 13,
"type": "metric",
"properties": {
"metrics": [
[ "AWS/Lambda", "Invocations", "FunctionName", "ActivationZoneMonitorFunction" ]
],
"view": "timeSeries",
"stacked": false,
"region": "eu-central-1",
"period": 3600,
"stat": "Sum",
"title": "Invocations",
"annotations": {
"horizontal": [
[
{
"color": "#f89256",
"label": "Medium load",
"value": 200
},
{
"value": 400,
"label": "Medium load"
}
],
{
"color": "#fe6e73",
"label": "High load",
"value": 400,
"fill": "above"
}
]
},
"yAxis": {
"left": {
"min": 0,
"label": ""
}
},
"liveData": false
}
},
{
"height": 11,
"width": 23,
"y": 10,
"x": 0,
"type": "log",
"properties": {
"query": "SOURCE '/sotawatchtower/MasterLogGroup' | fields @timestamp, @message\n| sort @timestamp desc\n| limit 10000\n| filter @message like /\\QAPRS Callsign: \\E.*\\Q, APRS Latitude: \\E.*\\Q, APRS Longitude: \\E.*\\Q, APRS Altitude: \\E.*\\Q\n\\E/",
"region": "eu-central-1",
"stacked": false,
"title": "APRS data stream",
"view": "table"
}
},
{
"height": 7,
"width": 6,
"y": 0,
"x": 0,
"type": "metric",
"properties": {
"metrics": [
[ "AWS/Lambda", "Invocations", "FunctionName", "ActivationZoneMonitorFunction" ]
],
"view": "gauge",
"region": "eu-central-1",
"yAxis": {
"left": {
"min": 0,
"max": 500
}
},
"stat": "Sum",
"period": 3600,
"title": "Callsigns per hour",
"annotations": {
"horizontal": [
{
"color": "#fe6e73",
"label": "High load",
"value": 400,
"fill": "above"
},
[
{
"color": "#f89256",
"label": "Medium load",
"value": 200
},
{
"value": 400,
"label": "Medium load"
}
]
]
},
"liveData": false,
"singleValueFullPrecision": false
}
},
{
"height": 7,
"width": 7,
"y": 0,
"x": 6,
"type": "log",
"properties": {
"query": "SOURCE '/sotawatchtower/MasterLogGroup' | fields @timestamp, @message, @logStream, @log\n| sort @timestamp desc\n| limit 10000\n| filter @message like /\\QAPRS Callsign: \\E.*\\Q, APRS Latitude: \\E.*\\Q, APRS Longitude: \\E.*\\Q, APRS Altitude: \\E/\n| parse @message /\\QAPRS Callsign: \\E(?<callsign>[^,]+), APRS Latitude/\n| stats \n count(if(callsign like /^D.*/, 1, null)) as D_Callsigns,\n count(if(callsign like /^OE.*/, 1, null)) as OE_Callsigns,\n count(if(!(callsign like /^D.*/ or callsign like /^OE.*/), 1, null)) as Other_Callsigns\n",
"region": "eu-central-1",
"stacked": false,
"title": "Country",
"view": "bar"
}
},
{
"height": 3,
"width": 6,
"y": 7,
"x": 0,
"type": "log",
"properties": {
"query": "SOURCE '/sotawatchtower/MasterLogGroup' | fields @timestamp, @message\n| parse @message /APRS Callsign: (?<callsign>[^\\s,]+), APRS Latitude: [^,]+, APRS Longitude: [^,]+, APRS Altitude: [^,]+/ \n| display @timestamp, callsign\n| sort @timestamp desc\n| limit 1\n| filter @message like /\\QAPRS Callsign: \\E.*\\Q, APRS Latitude: \\E.*\\Q, APRS Longitude: \\E.*\\Q, APRS Altitude: \\E.*\\Q\n\\E/",
"region": "eu-central-1",
"stacked": false,
"title": "Last callsign",
"view": "table"
}
},
{
"height": 3,
"width": 7,
"y": 7,
"x": 6,
"type": "alarm",
"properties": {
"title": "APRS Signal Health",
"alarms": [
"${AprsMonitorAlarm.Arn}"
]
}
}
]
}
DashboardName: SOTA-Watchtower-Dashboard
AprsMonitorAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: APRS Signal alive
ActionsEnabled: true
OKActions: []
AlarmActions: []
InsufficientDataActions: []
MetricName: Invocations
Namespace: AWS/Lambda
Statistic: Sum
Dimensions:
- Name: FunctionName
Value: ActivationZoneMonitorFunction
Period: 300
EvaluationPeriods: 1
DatapointsToAlarm: 1
Threshold: 1
ComparisonOperator: LessThanOrEqualToThreshold
TreatMissingData: breaching
Outputs:
ApiGatewayInvokeUrl:
Description: URL to enter into HamAlert Destination
Value: !Sub https://${HamAlertApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/notify
Export:
Name: !Sub ${AWS::StackName}-ApiGatewayInvokeUrl
DashboardUrl:
Description: URL to the CloudWatch Dashboard
Value: !Sub https://${AWS::Region}.console.aws.amazon.com/cloudwatch/home?region=${AWS::Region}#dashboards/dashboard/${SotaWatchtowerDashboard}
Export:
Name: !Sub ${AWS::StackName}-DashboardUrl