-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcloud_formation.yaml
379 lines (379 loc) · 11 KB
/
cloud_formation.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
AWSTemplateFormatVersion: 2010-09-09
Parameters:
awsSecretsName:
Type: String
Default: 'cidrbot-private-key-pem'
awsSecretsKey:
Type: String
Default: ''
dbTableRoomsName:
Type: String
Default: cidrbot-rooms
dbTableInstallationsName:
Type: String
Default: cidrbot-active-installations
dbTableAuthName:
Type: String
Default: cidrbot-active-auth-requests
apiGitAuthName:
Type: String
Default: cidrbot-git-auth
appId:
Type: String
Default: ''
appClientId:
Type: String
Default: ''
appClientSecret:
Type: String
Default: ''
apiGitAuthMappingKeyName:
Type: String
Default: cidr-git-auth
apiGitMappingKeyName:
Type: String
Default: cidr-git-webhook
apiGitName:
Type: String
Default: cidrbot-api-git
apiName:
Type: String
Default: cidrbot-api
securityGroupId:
Type: String
Default: ''
subnetId:
Type: String
Default: ''
lambdaExecutionRole:
Type: String
Default: ''
organizationId:
Type: String
Default: ''
regionName:
Type: String
Default: ''
webexBotId:
Type: String
Default: ''
webexAccessToken:
Type: String
Default: ''
webexBotName:
Type: String
Default: ''
githubBotName:
Type: String
Default: ''
apiGatewayStageName:
Type: String
Default: '*'
apiGatewayHTTPMethod:
Type: String
Default: '*'
gitLambdaFunctionName:
Type: String
AllowedPattern: '[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+'
Default: cidrbot-gitauth
lambdaFunctionName:
Type: String
AllowedPattern: '[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+'
Default: cidrbot
lambdaDomainName:
Type: String
Default: ''
lambdaAcmCert:
Type: String
Default: ''
route53RecordGroupSetHostedZone:
Type: String
Default: ''
weeklyTimerName:
Type: String
AllowedPattern: '[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+'
Default: timer-weekly
dailyTimerName:
Type: String
AllowedPattern: '[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+'
Default: timer-daily
Resources:
LambdaFunction:
Type: 'AWS::Lambda::Function'
Properties:
Environment:
Variables:
BASE_WEBHOOK_PATH: !Sub '/${lambdaFunctionName}'
DYNAMODB_AUTH_TABLE: !Ref dbTableAuthName
DYNAMODB_INSTALLATION_TABLE: !Ref dbTableInstallationsName
DYNAMODB_ROOM_TABLE: !Ref dbTableRoomsName
ORGANIZATION_ID: !Ref organizationId
REGION_NAME: !Ref regionName
TARGET_URL: !Sub 'https://${lambdaDomainName}/${lambdaFunctionName}'
WEBEX_BOT_ID: !Ref webexBotId
WEBEX_TEAMS_ACCESS_TOKEN: !Ref webexAccessToken
WEBEX_BOT_NAME: !Ref webexBotName
GITHUB_BOT_NAME: !Ref githubBotName
APP_ID: !Ref appId
CLIENTID: !Ref appClientId
CLIENTSECRET: !Ref appClientSecret
SECRET_NAME: !Ref awsSecretsName
Runtime: python3.8
Timeout: 180
Handler: lambda_function.lambda_handler
Role: !Ref lambdaExecutionRole
FunctionName: !Ref lambdaFunctionName
Code:
ZipFile: |
import logging
import datetime
import json
import socket
def lambda_handler(event, handle):
logger.debug('new event received: %s', str(event))
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({"testkey ": "testval"})
}
VpcConfig:
SecurityGroupIds:
- !Ref securityGroupId
SubnetIds:
- !Ref subnetId
Description: Base cidrbot lambda function
lambdaApiGatewayInvoke:
Type: 'AWS::Lambda::Permission'
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !GetAtt LambdaFunction.Arn
Principal: apigateway.amazonaws.com
SourceArn: !Sub >-
arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${Api}/${apiGatewayStageName}/${apiGatewayHTTPMethod}/${lambdaFunctionName}
DependsOn:
- LambdaFunction
lambdaApiGitGatewayInvoke:
Type: 'AWS::Lambda::Permission'
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !GetAtt LambdaFunction.Arn
Principal: apigateway.amazonaws.com
SourceArn: !Sub >-
arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ApiGit}/${apiGatewayStageName}/${apiGatewayHTTPMethod}/${lambdaFunctionName}
Api:
Type: 'AWS::ApiGatewayV2::Api'
Properties:
Name: !Ref apiName
RouteKey: !Sub 'ANY /${lambdaFunctionName}'
Description: Lambda api for webex webhooks
ProtocolType: HTTP
Target: !GetAtt LambdaFunction.Arn
DependsOn:
- LambdaFunction
ApiGit:
Type: 'AWS::ApiGatewayV2::Api'
Properties:
Name: !Ref apiGitName
RouteKey: !Sub 'ANY /${lambdaFunctionName}'
Description: Lambda api for github webhooks
ProtocolType: HTTP
Target: !GetAtt LambdaFunction.Arn
ApiMapping:
Type: 'AWS::ApiGatewayV2::ApiMapping'
Properties:
DomainName: !Ref lambdaDomainName
ApiId: !Ref Api
Stage: $default
ApiMappingGit:
Type: 'AWS::ApiGatewayV2::ApiMapping'
DependsOn:
- gitApiStage
Properties:
ApiMappingKey: !Ref apiGitMappingKeyName
DomainName: !Ref lambdaDomainName
ApiId: !Ref ApiGit
Stage: !Ref gitApiStage
gitApiStage:
Type: AWS::ApiGatewayV2::Stage
Properties:
ApiId: !Ref ApiGit
StageName: github
AutoDeploy: true
ApiDomainName:
Type: 'AWS::ApiGatewayV2::DomainName'
Properties:
DomainName: !Ref lambdaDomainName
DomainNameConfigurations:
- EndpointType: REGIONAL
CertificateArn: !Ref lambdaAcmCert
Route53Record:
Type: 'AWS::Route53::RecordSet'
Properties:
AliasTarget:
DNSName: !GetAtt ApiDomainName.RegionalDomainName
HostedZoneId: !GetAtt ApiDomainName.RegionalHostedZoneId
HostedZoneId: !Ref route53RecordGroupSetHostedZone
Name: !Ref lambdaDomainName
Type: A
DailyTimerRuleTest:
Type: 'AWS::Events::Rule'
Properties:
Name: !Ref dailyTimerName
Description: ScheduledRule
ScheduleExpression: cron(0 15 * * ? *)
State: DISABLED
Targets:
- Id: lambda-timer
Arn: !GetAtt LambdaFunction.Arn
Input: !Sub |
{
"Type": "Timer"
}
WeeklyTimerRule:
Type: 'AWS::Events::Rule'
Properties:
Name: !Ref weeklyTimerName
Description: ScheduledRule
ScheduleExpression: cron(0 16 ? * 2 *)
State: DISABLED
Targets:
- Id: lambda-weekly-timer
Arn: !GetAtt LambdaFunction.Arn
Input: !Sub |
{
"Type": "Weekly Timer"
}
TimerPermission:
Type: 'AWS::Lambda::Permission'
Properties:
FunctionName: !Ref lambdaFunctionName
Action: 'lambda:InvokeFunction'
Principal: events.amazonaws.com
SourceArn:
'Fn::GetAtt':
- DailyTimerRuleTest
- Arn
WeeklyTimerPermission:
Type: 'AWS::Lambda::Permission'
Properties:
FunctionName: !Ref lambdaFunctionName
Action: 'lambda:InvokeFunction'
Principal: events.amazonaws.com
SourceArn:
'Fn::GetAtt':
- WeeklyTimerRule
- Arn
GitLambdaFunction:
Type: 'AWS::Lambda::Function'
Properties:
Environment:
Variables:
CALLBACKURL: !Sub 'https://${lambdaDomainName}/${apiGitAuthMappingKeyName}/${gitLambdaFunctionName}'
CLIENTID: !Ref appClientId
CLIENTSECRET: !Ref appClientSecret
DYNAMODB_AUTH_TABLE: !Ref dbTableAuthName
DYNAMODB_INSTALLATION_TABLE: !Ref dbTableInstallationsName
DYNAMODB_ROOM_TABLE: !Ref dbTableRoomsName
WEBEX_BOT_ID: !Ref webexBotId
WEBEX_TEAMS_ACCESS_TOKEN: !Ref webexAccessToken
GITHUB_BOT_NAME: !Ref githubBotName
APP_ID: !Ref appId
REGION_NAME: !Ref regionName
SECRET_NAME: !Ref awsSecretsName
ORGANIZATION_ID: !Ref organizationId
Runtime: python3.8
Timeout: 180
Handler: lambda_function.lambda_handler
Role: !Ref lambdaExecutionRole
FunctionName: !Ref gitLambdaFunctionName
Code:
ZipFile: |
import logging
import datetime
import json
import socket
def lambda_handler(event, handle):
logger.debug('new event received: %s', str(event))
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({"testkey ": "testval"})
}
VpcConfig:
SecurityGroupIds:
- !Ref securityGroupId
SubnetIds:
- !Ref subnetId
Description: cidrbot git auth lambda function
gitLambdaPermissions:
Type: 'AWS::Lambda::Permission'
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !GetAtt GitLambdaFunction.Arn
Principal: apigateway.amazonaws.com
SourceArn: !Sub >-
arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${GitAuthApi}/${apiGatewayStageName}/${apiGatewayHTTPMethod}/${gitLambdaFunctionName}
GitAuthApi:
Type: 'AWS::ApiGatewayV2::Api'
Properties:
Name: !Ref apiGitAuthName
RouteKey: !Sub 'ANY /${gitLambdaFunctionName}'
Description: Lambda api for git auth callbacks
ProtocolType: HTTP
Target: !GetAtt GitLambdaFunction.Arn
GitAuthApiMapping:
Type: 'AWS::ApiGatewayV2::ApiMapping'
Properties:
ApiMappingKey: !Ref apiGitAuthMappingKeyName
DomainName: !Ref lambdaDomainName
ApiId: !Ref GitAuthApi
Stage: $default
roomsTable:
Type: 'AWS::DynamoDB::Table'
Properties:
AttributeDefinitions:
- AttributeName: room_id
AttributeType: S
KeySchema:
- AttributeName: room_id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
TableName: !Ref dbTableRoomsName
installationsTable:
Type: 'AWS::DynamoDB::Table'
Properties:
AttributeDefinitions:
- AttributeName: installation_id
AttributeType: S
KeySchema:
- AttributeName: installation_id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
TableName: !Ref dbTableInstallationsName
authRequestsTable:
Type: 'AWS::DynamoDB::Table'
Properties:
AttributeDefinitions:
- AttributeName: state
AttributeType: S
KeySchema:
- AttributeName: state
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
TableName: !Ref dbTableAuthName
awsSecretsManagerSecret:
Type: 'AWS::SecretsManager::Secret'
Properties:
Name: !Ref awsSecretsName
Description: "Private key for cidrbot app"
SecretString: !Ref awsSecretsKey