forked from riusricardo/validator-failover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicServerlessValidator.yml
247 lines (231 loc) · 7.47 KB
/
BasicServerlessValidator.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
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
##################################################
## A serverless failover solution for Web3 validator nodes
##################################################
## Author: Ricardo Rius
## Copyright: Copyright 2019, Ricardo Rius
## License: Apache-2.0
## Version: 0.1.3
##################################################
AWSTemplateFormatVersion: "2010-09-09"
Description: Basic serverless architecture to handle validator nodes outages & service interruptions.
Parameters:
InstanceId:
Type: String
Default: i-<REPLACE>
Description: Validator instance ID (EC2)
NodeName:
Type: String
Default: ValidatorNode
Description: Validator reference name
CertificateId:
Type: String
Default: <REPLACE>
Description: Validator X.509 certificate ID (IOT)
Email:
Type: String
Default: <REPLACE>
Description: Receive email alerts
LambdaSize:
Type: Number
Default: 256
AllowedValues:
- 256
- 512
- 1024
- 2048
Description: Lambda memory size [MB]
Resources:
LambdaStartEC2:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile:
!Sub
- |-
import boto3
ec2 = boto3.client('ec2', region_name='${HostRegion}')
instances = ['${InstanceId}']
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('Started instances ' + str(instances))
- {HostRegion: !Ref "AWS::Region"}
Runtime: python3.7
Timeout: 5
MemorySize: !Ref LambdaSize
Handler: index.lambda_handler
TracingConfig:
Mode: Active
Role: !GetAtt LambdaEC2StartStopRole.Arn
LambdaStopEC2:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile:
!Sub
- |-
import boto3
ec2 = boto3.client('ec2', region_name='${HostRegion}')
instances = ['${InstanceId}']
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('Stopped instances ' + str(instances))
- {HostRegion: !Ref "AWS::Region"}
Runtime: python3.7
Timeout: 5
MemorySize: !Ref LambdaSize
Handler: index.lambda_handler
TracingConfig:
Mode: Active
Role: !GetAtt LambdaEC2StartStopRole.Arn
LambdaEC2StartStopRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess
- arn:aws:iam::aws:policy/CloudWatchLogsFullAccess
Policies:
-
PolicyName: StartStopEC2
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: Allow
Action:
- "ec2:Start*"
- "ec2:Stop*"
Resource: "*"
ValidatorNodePolicy:
Type: AWS::IoT::Policy
Properties:
PolicyName: !Join [ "", [ !Ref NodeName,"Policy"]]
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action: "iot:*"
Resource: "*"
ValidatorNodePolicyPrincipalAttachment:
Type: AWS::IoT::PolicyPrincipalAttachment
Properties:
PolicyName: !Join [ "", [ !Ref NodeName,"Policy"]]
Principal: !Join [ "", [ "arn:aws:iot:", !Ref "AWS::Region", ":", !Ref "AWS::AccountId", ":cert/" ,!Ref CertificateId] ]
DependsOn:
- ValidatorNodePolicy
ValidatorNode:
Type: AWS::IoT::Thing
Properties:
ThingName: !Join [ "", [ !Ref NodeName,"Thing"]]
DependsOn:
- ValidatorNodePolicy
ValidatorNodePrincipalAttachment:
Type: AWS::IoT::ThingPrincipalAttachment
Properties:
ThingName: !Join [ "", [ !Ref NodeName,"Thing"]]
Principal: !Join [ "", [ "arn:aws:iot:", !Ref "AWS::Region", ":", !Ref "AWS::AccountId", ":cert/" ,!Ref CertificateId] ]
DependsOn:
- ValidatorNode
ValidatorNodeStartTopicRule:
Type: AWS::IoT::TopicRule
Properties:
RuleName: !Join [ "", [ !Ref NodeName,"StartTopicRule"]]
TopicRulePayload:
Description: 'Get Validator status'
AwsIotSqlVersion: '2016-03-23'
RuleDisabled: false
Sql: !Sub 'SELECT *, topic(3) as thing FROM "$aws/things/${NodeName}Thing/shadow/update/accepted" WHERE state.reported.status = "inactive" OR state.reported.status = "failed"'
Actions:
- Lambda:
FunctionArn: !GetAtt LambdaStartEC2.Arn
- Sns:
MessageFormat: JSON
RoleArn: !GetAtt ValidatorNodeIOTExecRole.Arn
TargetArn: !Ref ValidatorNodeSNSTopic
DependsOn:
- LambdaStartEC2
- LambdaStopEC2
ValidatorNodeStopTopicRule:
Type: AWS::IoT::TopicRule
Properties:
RuleName: !Join [ "", [ !Ref NodeName,"StopTopicRule"]]
TopicRulePayload:
Description: 'Get Validator status'
AwsIotSqlVersion: '2016-03-23'
RuleDisabled: false
Sql: !Sub 'SELECT *, topic(3) as thing FROM "$aws/things/${NodeName}Thing/shadow/update/accepted" WHERE state.reported.status = "active"'
Actions:
- Lambda:
FunctionArn: !GetAtt LambdaStopEC2.Arn
- Sns:
MessageFormat: JSON
RoleArn: !GetAtt ValidatorNodeIOTExecRole.Arn
TargetArn: !Ref ValidatorNodeSNSTopic
DependsOn:
- LambdaStartEC2
- LambdaStopEC2
ValidatorNodeDisconnectedTopicRule:
Type: AWS::IoT::TopicRule
Properties:
RuleName: !Join [ "", [ !Ref NodeName,"DisconnectedTopicRule"]]
TopicRulePayload:
Description: 'Get Validator connection status'
AwsIotSqlVersion: '2016-03-23'
RuleDisabled: false
Sql: !Sub 'SELECT * FROM "$aws/events/presence/disconnected/${NodeName}Thing"'
Actions:
- Lambda:
FunctionArn: !GetAtt LambdaStartEC2.Arn
- Sns:
MessageFormat: JSON
RoleArn: !GetAtt ValidatorNodeIOTExecRole.Arn
TargetArn: !Ref ValidatorNodeSNSTopic
DependsOn:
- LambdaStartEC2
- LambdaStopEC2
ValidatorNodeIOTExecRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: Allow
Principal:
Service: events.amazonaws.com
Action: sts:AssumeRole
Path: /service-role/
Policies:
-
PolicyName: !Join [ "", [ !Ref NodeName,"snsPublish"]]
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: Allow
Action: 'sns:Publish'
Resource: !Ref ValidatorNodeSNSTopic
ValidatorNodeSNSTopic:
Type: AWS::SNS::Topic
ValidatorEmailSubscription:
Type: AWS::SNS::Subscription
Properties:
Protocol: email
Endpoint: !Ref Email
TopicArn: !Ref ValidatorNodeSNSTopic
LambdaStartSubscription:
Type: AWS::SNS::Subscription
Properties:
Protocol: lambda
Endpoint: !GetAtt LambdaStartEC2.Arn
TopicArn: !Ref ValidatorNodeSNSTopic