-
Notifications
You must be signed in to change notification settings - Fork 115
/
template.yaml
325 lines (306 loc) · 10.1 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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
Description: >
Amazon API Gateway definition synchronization with ReadMe
Globals:
Function:
Tracing: Active
Parameters:
APIGWID:
Description: ID of the API Gateway endpoint to be monitored for changes
Type: String
Default: <API Gateway ID here>
APIGWStage:
Description: Stage of the API Gateway endpoint to be monitored for changes
Type: String
Default: <API Gateway stage name here>
APIGWType:
Description: Amazon API Gateway endpoint type (REST or HTTP)
Type: String
Default: REST
AllowedValues:
- "REST"
- "HTTP"
RMProjectVersion:
Description: ReadMe Version number of your docs project, for example, v3.0.
Type: String
Default: <ReadMe project version number here>
RMAPIDefinitionID:
Description: ReadMe ID of the API specification. The unique ID for each API can be found by navigating to your API Definitions page. Use "None" if it is a new API and you do not know definition ID yet.
Type: String
Default: None
RMAPIKey:
Description: ReadMe API key to be used for authentication
Type: String
Default: <ReadMe API key here>
NotificationEmail:
Description: ReadMe administrative user email for notifications
Type: String
Default: ReadMe admin email here
Mappings:
APIGatewayClients:
REST:
name: apigateway
HTTP:
name: apigatewayv2
Conditions:
IsRESTAPI: !Equals
- !Ref APIGWType
- "REST"
Resources:
# API definition synchronization AWS Lambda function and Amazon EventBridge rules
ReadMeSyncFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.9
MemorySize: 128
Timeout: 100
EventInvokeConfig:
MaximumRetryAttempts: 0
Handler: src/sync.lambda_handler
Description: API Gateway->ReadMe synchronization function
Events:
DeploymentCreated:
Type: EventBridgeRule
Properties:
Pattern:
Fn::Sub:
- |
{
"detail": {
"eventSource": ["apigateway.amazonaws.com"],
"requestParameters": {
"${stagenamefieldname}": ${stagenamevalue},
"${apiidfieldname}": ["${APIGWID}"]
},
"eventName": ["CreateDeployment"],
"errorCode": [ { "exists": false } ]
}
}
-
apiidfieldname: !If [IsRESTAPI, "restApiId", "apiId"]
stagenamefieldname: !If [IsRESTAPI, "createDeploymentInput", "stageName"]
stagenamevalue:
!If
- IsRESTAPI
- !Sub "{\"stageName\": [\"${APIGWStage}\"]}"
- !Sub "[\"${APIGWStage}\"]"
StageUpdated:
Type: EventBridgeRule
Properties:
Pattern:
Fn::Sub:
- |
{
"detail": {
"eventSource": ["apigateway.amazonaws.com"],
"requestParameters": {
"${apiidfieldname}": ["${APIGWID}"]
},
"responseElements": {
"stageName": ["${APIGWStage}"]
},
"eventName": ["UpdateStage"],
"errorCode": [ { "exists": false } ]
}
}
-
apiidfieldname: !If [IsRESTAPI, "restApiId", "apiId"]
StageDeleted:
Type: EventBridgeRule
Properties:
Pattern:
Fn::Sub:
- |
{
"detail": {
"eventSource": ["apigateway.amazonaws.com"],
"requestParameters": {
"stageName": ["${APIGWStage}"],
"${apiidfieldname}": ["${APIGWID}"]
},
"eventName": ["DeleteStage"],
"errorCode": [ { "exists": false } ]
}
}
-
apiidfieldname: !If [IsRESTAPI, "restApiId", "apiId"]
APIDeleted:
Type: EventBridgeRule
Properties:
Pattern:
Fn::Sub:
- |
{
"detail": {
"eventSource": ["apigateway.amazonaws.com"],
"requestParameters": {
"${apiidfieldname}": ["${APIGWID}"]
},
"eventName": ["${eventname}"],
"errorCode": [ { "exists": false } ]
}
}
-
apiidfieldname: !If [IsRESTAPI, "restApiId", "apiId"]
eventname: !If [IsRESTAPI, "DeleteRestApi", "DeleteApi"]
Environment:
Variables:
PARAMETERS_PREFIX: !Sub "/${AWS::StackName}/"
APIGW_SDK_CLIENT_NAME: !FindInMap [APIGatewayClients, !Ref APIGWType, name]
Policies:
- SSMParameterReadPolicy:
ParameterName: !Sub "${AWS::StackName}/*"
- AWSSecretsManagerGetSecretValuePolicy:
SecretArn: !Ref RMAPIKeySecret
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- apigateway:GET
Resource:
'Fn::If':
- IsRESTAPI
- !Sub 'arn:aws:apigateway:${AWS::Region}::/restapis/${APIGWID}/stages/${APIGWStage}/exports/oas30'
- !Sub 'arn:aws:apigateway:${AWS::Region}::/apis/${APIGWID}/exports/OAS30'
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ssm:PutParameter
Resource:
- !Sub 'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${AWS::StackName}/RM-APIDefinitionID'
Tags:
Stack: !Sub "${AWS::StackName}"
# Synchronization function Amazon CloudWatch Logs log group to override default indefinete log retention period
ReadMeSyncFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${ReadMeSyncFunction}"
RetentionInDays: 7
# Configuration parameters in AWS Systems Manager Parameter Store
APIGWIDParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Sub "/${AWS::StackName}/APIGW-ID"
Type: String
Value: !Sub "${APIGWID}"
Description: SSM Parameter for API gateway ID
Tags:
Stack: !Sub "${AWS::StackName}"
APIGWStageParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Sub "/${AWS::StackName}/APIGW-Stage"
Type: String
Value: !Sub "${APIGWStage}"
Description: SSM Parameter for API gateway stage name
Tags:
Stack: !Sub "${AWS::StackName}"
APIGWTypeParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Sub "/${AWS::StackName}/APIGW-Type"
Type: String
Value: !Sub "${APIGWType}"
Description: SSM Parameter for API gateway endpoint type
Tags:
Stack: !Sub "${AWS::StackName}"
RMAPIDefinitionIDParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Sub "/${AWS::StackName}/RM-APIDefinitionID"
Type: String
Value: !Sub "${RMAPIDefinitionID}"
Description: SSM Parameter for ReadMe API definition ID
Tags:
Stack: !Sub "${AWS::StackName}"
RMAPIProjectVersionParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Sub "/${AWS::StackName}/RM-ProjectVersion"
Type: String
Value: !Sub "${RMProjectVersion}"
Description: SSM Parameter for ReadMe project version
Tags:
Stack: !Sub "${AWS::StackName}"
# ReadMe API key (used for authentication) in AWS Secrets Manager
RMAPIKeySecret:
Type: AWS::SecretsManager::Secret
Properties:
Name: !Sub "/${AWS::StackName}/RM-APIKey"
Description: ReadMe API key
SecretString: !Sub "${RMAPIKey}"
Tags:
-
Key: Stack
Value: !Sub "${AWS::StackName}"
# Amazon SNS topic for notifications and alerts
NotificationsTopic:
Type: AWS::SNS::Topic
Properties:
KmsMasterKeyId: !Ref NotificationsTopicKMSKey
Tags:
- Key: "Stack"
Value: !Sub "${AWS::StackName}"
NotificationsTopicKMSKey:
Type: AWS::KMS::Key
Properties:
Description: CMK for SNS notifications topic
Enabled: true
EnableKeyRotation: True
KeyPolicy:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- "cloudwatch.amazonaws.com"
- "sns.amazonaws.com"
Action:
- "kms:GenerateDataKey*"
- "kms:Decrypt"
Resource: "*"
- Effect: Allow
Principal:
AWS:
- !Sub "arn:aws:iam::${AWS::AccountId}:root"
Action:
- "kms:*"
Resource: "*"
KeySpec: SYMMETRIC_DEFAULT
KeyUsage: ENCRYPT_DECRYPT
PendingWindowInDays: 30
Tags:
- Key: "Stack"
Value: !Sub "${AWS::StackName}"
# Alarms for synchronization failures
ReadMeSyncFunctionErrorsAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- !Ref NotificationsTopic
ComparisonOperator: GreaterThanOrEqualToThreshold
Dimensions:
- Name: FunctionName
Value: !Ref ReadMeSyncFunction
EvaluationPeriods: 1
MetricName: Errors
Namespace: AWS/Lambda
Period: 60
Statistic: Sum
Threshold: 1.0
# Subscription for administrator to the notifications topic
TestTopicSubscription:
Type: AWS::SNS::Subscription
Properties:
Endpoint: !Ref NotificationEmail
Protocol: email
TopicArn: !Ref NotificationsTopic
Outputs:
NotificationsTopic:
Description: SNS Topic to be used for the notifications subscriptions
Value: !Ref NotificationsTopic