-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
329 lines (309 loc) · 9.02 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
---
AWSTemplateFormatVersion: '2010-09-09'
Transform: "AWS::Serverless-2016-10-31"
Description: >
test-apis
# Parameters are populated atm with the default values
Parameters:
Environment:
Description: Environment to run under
Type: String
Default: "local"
AllowedValues:
- "local"
- "qa"
- "prod"
LocalEnvironmentType:
Description: Type of Local Environment to run under
Type: String
Default: "one"
AllowedValues:
- "one"
- "two"
PassedInParameter:
Description: An example parameter passed into the template
Type: String
Default: ""
# Environment mapped variables
Mappings:
LocalEnvironmentSettings:
one:
DependencyUrl: 'http://172.17.0.1:3001/local/one/dependency'
two:
DependencyUrl: 'http://host.docker.internal:3001/local/two/dependency'
Environments:
local:
DependencyUrl: 'http://host.docker.internal:3001/local/dependency'
qa:
DependencyUrl: 'http://172.17.0.1:3001/qa/dependency'
prod:
DependencyUrl: 'http://my.server.com:3001/prod/dependency'
# apply some conditions
Conditions:
LocalEnvironment: !Equals [ !Ref Environment, 'local' ]
NotLocalEnvironment: !Not [ !Equals [ !Ref Environment, 'local' ] ]
# Global function env vars
Globals:
Api:
Cors:
AllowMethods: 'OPTIONS'
Function:
Environment:
Variables:
DEPENDENCY_URL: !FindInMap [Environments, !Ref Environment, DependencyUrl]
# the if will get resolved
DEPENDENCY_URL_IF: !If [
LocalEnvironment,
!FindInMap [LocalEnvironmentSettings, !Ref LocalEnvironmentType, DependencyUrl],
!FindInMap [Environments, !Ref Environment, DependencyUrl]
]
MY_PARAMETER_VARIABLE:
Ref: PassedInParameter
MY_SECRET:
Fn::Sub: "{{resolve:secretsmanager:${SecretType}:SecretString:secretValue}}"
Runtime: nodejs14.x
Timeout: 15
Layers:
- !Ref BaseLayer
# Http API Gateway and Resources
Resources:
# http api with routes defined on function events,
# events are routed through the auth function first
TestHttpApiv1:
Type: AWS::Serverless::HttpApi
Properties:
Auth:
DefaultAuthorizer: LambdaRequestAuthorizer
Authorizers:
LambdaRequestAuthorizer:
FunctionPayloadType: REQUEST
FunctionArn: !GetAtt lambdaAuthorizer.Arn
CorsConfiguration: true
DefinitionBody:
'Fn::Transform':
Name: AWS::Include
Parameters:
Location: openapi.yaml
StageName: v1
# http api with routes defined on function events
TestHttpApiv2:
Type: AWS::Serverless::HttpApi
Properties:
Auth:
DefaultAuthorizer: SimpleLambdaRequestAuthorizer
Authorizers:
SimpleLambdaRequestAuthorizer:
FunctionArn: !GetAtt simpleLambdaAuthorizer.Arn
EnableSimpleResponses: true
Identity:
Headers:
- Authorization
CorsConfiguration: 'localhost'
DefinitionBody:
'Fn::Transform':
Name: AWS::Include
Parameters:
Location: openapi.yaml
StageName: v2
# http api with token authorizer
TestHttpApiv3:
Type: AWS::Serverless::HttpApi
Properties:
Auth:
DefaultAuthorizer: SimpleLambdaRequestAuthorizer
Authorizers:
SimpleLambdaRequestAuthorizer:
FunctionArn: !GetAtt simpleLambdaAuthorizer.Arn
EnableSimpleResponses: true
CorsConfiguration: true
DefinitionBody:
'Fn::Transform':
Name: AWS::Include
Parameters:
Location: openapi.yaml
StageName: v3
# http api with external authorizer that will be ignored
TestHttpApiv4:
Type: AWS::Serverless::HttpApi
Properties:
Auth:
DefaultAuthorizer: ExternalAuthorizer
Authorizers:
ExternalAuthorizer:
FunctionArn:
Fn::ImportValue: ExternalAuthorizer
EnableSimpleResponses: true
CorsConfiguration: true
DefinitionBody:
'Fn::Transform':
Name: AWS::Include
Parameters:
Location: openapi.yaml
StageName: v4
# http api with no auth and cors
TestHttpApiv5:
Type: AWS::Serverless::HttpApi
Properties:
CorsConfiguration:
AllowCredentials: true
AllowHeaders: 'Authorization, *'
AllowMethods: 'GET, POST, DELETE, *'
AllowOrigins: 'http://localhost, https://stackoverflow.com'
ExposeHeaders: 'Date, x-api-id'
MaxAge: 100
DefinitionBody:
'Fn::Transform':
Name: AWS::Include
Parameters:
Location: openapi.yaml
StageName: v5
# base layer for functions, this isn't used atm the handlers run against your dev dependencies instead
BaseLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: base_layer
Description: Base layer for containers
ContentUri: base
CompatibleRuntimes:
- nodejs14.x
RetentionPolicy: Delete
Metadata:
BuildMethod: nodejs14.x
# a secret from secret manager, we don't do anything with this yet
EncryptionSecret:
Type: AWS::SecretsManager::Secret
Properties:
Name: my-secret
Description: My Secret
GenerateSecretString:
SecretStringTemplate: "{}"
GenerateStringKey: "secretValue"
ExcludeCharacters: '"@/\\'
# functions
pathParamTestGet:
Type: AWS::Serverless::Function
Properties:
Handler: handlers/v1/index.pathParamTestGet
Events:
Apiv1:
Type: HttpApi
Properties:
Path: /{pathParam}/test/
Method: get
ApiId: !Ref TestHttpApiv1
doublePathParamTestGet:
Type: AWS::Serverless::Function
Properties:
Handler: handlers/v1/index.doublePathParamTestGet
Events:
Apiv1:
Type: HttpApi
Properties:
Path: /{pathParam1}/test/{pathParam2}/testagain
Method: get
ApiId: !Ref TestHttpApiv1
testGet:
Type: AWS::Serverless::Function
Properties:
Handler: handlers/v1/index.testGet
Events:
Apiv1Get:
Type: HttpApi
Properties:
Path: /test
Method: get
ApiId: !Ref TestHttpApiv1
Apiv1Delete:
Type: HttpApi
Properties:
Path: /test
Method: delete
ApiId: !Ref TestHttpApiv1
Apiv2:
Type: HttpApi
Properties:
Path: /test
Method: get
ApiId: !Ref TestHttpApiv2
Apiv3:
Type: HttpApi
Properties:
Path: /test
Method: get
ApiId: !Ref TestHttpApiv3
Apiv5:
Type: HttpApi
Properties:
Path: /test
Method: get
ApiId: !Ref TestHttpApiv5
proxyTestGet:
Type: AWS::Serverless::Function
Properties:
Handler: handlers/v1/index.proxyTestGet
Events:
Apiv1:
Type: HttpApi
Properties:
Path: /proxy/{proxy}+
Method: get
ApiId: !Ref TestHttpApiv1
testPost:
Type: AWS::Serverless::Function
Properties:
Handler: handlers/v1/index.testPost
Events:
Apiv2:
Type: HttpApi
Properties:
Path: /test
Method: post
ApiId: !Ref TestHttpApiv2
scheduledPost:
Type: AWS::Serverless::Function
Properties:
Handler: handlers/v1/index.scheduledPost
Events:
Apiv2:
Type: HttpApi
Properties:
Path: /scheduled
Method: post
ApiId: !Ref TestHttpApiv2
EveryTenMinutes:
Type: Schedule
Properties:
Schedule: 'rate(10 minutes)'
Name: EveryTenMinutes
Description: Run every 10 minutes
Enabled: true
# an authorizer function
lambdaAuthorizer:
Type: AWS::Serverless::Function
Properties:
Handler: handlers/v1/index.authorizer
simpleLambdaAuthorizer:
Type: AWS::Serverless::Function
Properties:
Handler: handlers/v1/index.simpleAuthorizer
Outputs:
TestHttpApiv1:
Description: "API Gateway endpoint URL for test api v1"
Value:
Fn::Sub: https://${TestHttpApiv1}.execute-api.${AWS::Region}.amazonaws.com/v1
TestHttpApiv2:
Description: "API Gateway endpoint URL for test api v2"
Value:
Fn::Sub: https://${TestHttpApiv2}.execute-api.${AWS::Region}.amazonaws.com/v2
TestHttpApiv3:
Description: "API Gateway endpoint URL for test api v3"
Value:
Fn::Sub: https://${TestHttpApiv3}.execute-api.${AWS::Region}.amazonaws.com/v3
TestHttpApiv4:
Description: "API Gateway endpoint URL for test api v4"
Value:
Fn::Sub: https://${TestHttpApiv4}.execute-api.${AWS::Region}.amazonaws.com/v4
TestHttpApiv5:
Description: "API Gateway endpoint URL for test api v5"
Value:
Fn::Sub: https://${TestHttpApiv5}.execute-api.${AWS::Region}.amazonaws.com/v5