-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws-cf-template-health-collector-v1.0.yaml
275 lines (261 loc) · 7.62 KB
/
aws-cf-template-health-collector-v1.0.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
AWSTemplateFormatVersion: '2010-09-09'
Description: "This CloudFormation template listens for the 'aws.health' event sent by the EventBus Sender for each member account provisioned as 'sender-org-stacks' in AWS Organizations. Additionally, received events are sent to SNS topics and sent to Hangout Chat through Lambda subscribers."
Parameters:
Project:
Type: String
MinLength: '3'
MaxLength: '8'
Description: Project name (3-8 characters)
Default: cops
Region:
Type: String
Description: AWS Region
AllowedValues:
- ap-northeast-2
- us-east-1
- us-west-2
Default: ap-northeast-2
ECRImageUri:
Type: String
Description: AWS Health Event Notifier Image
GchatWebhookUrl:
Type: String
Description: Google Chat Webhook URL
OrgId:
Type: String
MinLength: '4'
MaxLength: '16'
Description: Organization ID to allow events from.
Owner:
Type: String
Description: Resource owner
Team:
Type: String
Description: Team name
Default: PEOps
Resources:
DelibirdLambda:
Type: AWS::Lambda::Function
Properties:
FunctionName: !Sub ${Project}-health-delibird-lambda
PackageType: Image
Architectures:
- arm64
MemorySize: 256
Timeout: 300
Code:
ImageUri: !Ref ECRImageUri
Role: !GetAtt DelibirdLambdaRole.Arn
Environment:
Variables:
GCHAT_WEBHOOK_URL: !Ref GchatWebhookUrl
Tags:
- Key: Name
Value: !Sub ${Project}-health-delibird-lambda
- Key: Project
Value: !Ref Project
- Key: Owner
Value: !Ref Owner
- Key: Team
Value: !Ref Team
DelibirdLambdaRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${Project}HealthDelibirdLambdaRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: !Sub ${Project}LambdaExecutionPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: "arn:aws:logs:*:*:*"
- Effect: "Allow"
Action:
- "ecr:*"
Resource: "*"
- Effect: "Allow"
Action:
- "kms:Decrypt"
Resource: "*"
Tags:
- Key: Name
Value: !Sub ${Project}HealthDelibirdLambdaRole
- Key: Project
Value: !Ref Project
- Key: Owner
Value: !Ref Owner
- Key: Team
Value: !Ref Team
HealthKMS:
Type: AWS::KMS::Key
Properties:
Description: KMS key for SNS encryption
KeySpec: SYMMETRIC_DEFAULT
KeyUsage: ENCRYPT_DECRYPT
KeyPolicy:
Version: '2012-10-17'
Id: 'key-default-1'
Statement:
- Sid: 'Enable IAM User Permissions'
Effect: 'Allow'
Principal:
AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:root'
Action: 'kms:*'
Resource: '*'
- Sid: 'Allow EventBridge to use the key'
Effect: 'Allow'
Principal:
Service: 'events.amazonaws.com'
Action:
- 'kms:Decrypt'
- 'kms:GenerateDataKey'
Resource: '*'
Tags:
- Key: Name
Value: !Sub ${Project}-health-kms
- Key: Project
Value: !Ref Project
- Key: Owner
Value: !Ref Owner
- Key: Team
Value: !Ref Team
HealthKMSAlias:
Type: AWS::KMS::Alias
Properties:
AliasName: !Sub alias/${Project}-health-kms
TargetKeyId: !Ref HealthKMS
HealthTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub ${Project}-aws-health-topic
KmsMasterKeyId: !Ref HealthKMSAlias
Tags:
- Key: Name
Value: !Sub ${Project}-health-topic
- Key: Project
Value: !Ref Project
- Key: Owner
Value: !Ref Owner
- Key: Team
Value: !Ref Team
HealthTopicPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
Topics:
- !Ref HealthTopic
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: "__default_statement_ID"
Effect: Allow
Principal:
AWS: "*"
Action:
- SNS:GetTopicAttributes
- SNS:SetTopicAttributes
- SNS:AddPermission
- SNS:RemovePermission
- SNS:DeleteTopic
- SNS:Subscribe
- SNS:ListSubscriptionsByTopic
- SNS:Publish
Resource: !Ref HealthTopic
Condition:
StringEquals:
AWS:SourceOwner: !Ref AWS::AccountId
- Sid: "AllowEventBridgeToPublish"
Effect: "Allow"
Principal:
Service: "events.amazonaws.com"
Action: "sns:Publish"
Resource: !Ref HealthTopic
HealthTopicSubscription:
Type: AWS::SNS::Subscription
Properties:
TopicArn: !Ref HealthTopic
Protocol: lambda
Endpoint: !GetAtt DelibirdLambda.Arn
HealthEventBus:
Type: AWS::Events::EventBus
Properties:
Name: !Sub ${Project}-health-collector-bus
Description: "Receive all aws.health events and notify to sns topic."
Tags:
- Key: Name
Value: !Sub ${Project}-health-collector-bus
- Key: Project
Value: !Ref Project
- Key: Owner
Value: !Ref Owner
- Key: Team
Value: !Ref Team
HealthEventBusPolicy:
Type: AWS::Events::EventBusPolicy
Properties:
EventBusName: !Ref HealthEventBus
StatementId: "AllowPutHealthEvents"
Statement:
Effect: "Allow"
Principal: "*"
Action: "events:PutEvents"
Resource: !GetAtt HealthEventBus.Arn
HealthEventBusOrgPolicy:
Type: AWS::Events::EventBusPolicy
Properties:
EventBusName: !Ref HealthEventBus
StatementId: "AllowPutHealthEventsForOrganizationId"
Statement:
Effect: "Allow"
Principal: "*"
Action: "events:PutEvents"
Resource: !GetAtt HealthEventBus.Arn
Condition:
StringEquals:
"aws:PrincipalOrgID": !Ref OrgId
HealthProcessorRule:
Type: AWS::Events::Rule
Properties:
Name: !Sub ${Project}-health-processor-rule
Description: "Notify all aws.health events to sns topic"
EventBusName: !Ref HealthEventBus
EventPattern:
source:
- aws.health
- custom.health
Targets:
- Arn: !Ref HealthTopic
Id: HealthTopicTarget
State: "ENABLED"
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref DelibirdLambda
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Ref HealthTopic
Outputs:
LambdaArn:
Description: ARN of the Delibird Lambda Function
Value: !GetAtt DelibirdLambda.Arn
SNSTopicArn:
Description: ARN of the Health SNS Topic
Value: !Ref HealthTopic
EventBusArn:
Description: ARN of the Health Event Bus
Value: !GetAtt HealthEventBus.Arn
ECRArn:
Description: ARN of the ECR
Value: !Select [0, !Split [":", !Ref ECRImageUri]]