You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+62Lines changed: 62 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -169,6 +169,53 @@ stepFunctions:
169
169
definition:
170
170
```
171
171
172
+
#### Share API Gateway and API Resources
173
+
174
+
You can [share the same API Gateway](https://serverless.com/framework/docs/providers/aws/events/apigateway/#share-api-gateway-and-api-resources) between multiple projects by referencing its REST API ID and Root Resource ID in serverless.yml as follows:
175
+
176
+
```yml
177
+
service: service-name
178
+
provider:
179
+
name: aws
180
+
apiGateway:
181
+
# REST API resource ID. Default is generated by the framework
182
+
restApiId: xxxxxxxxxx
183
+
# Root resource, represent as / path
184
+
restApiRootResourceId: xxxxxxxxxx
185
+
186
+
functions:
187
+
...
188
+
```
189
+
If your application has many nested paths, you might also want to break them out into smaller services.
190
+
191
+
However, Cloudformation will throw an error if we try to generate an existing path resource. To avoid that, we reference the resource ID:
192
+
193
+
```yml
194
+
service: service-a
195
+
provider:
196
+
apiGateway:
197
+
restApiId: xxxxxxxxxx
198
+
restApiRootResourceId: xxxxxxxxxx
199
+
# List of existing resources that were created in the REST API. This is required or the stack will be conflicted
200
+
restApiResources:
201
+
/users: xxxxxxxxxx
202
+
203
+
functions:
204
+
...
205
+
```
206
+
207
+
Now we can define endpoints using existing API Gateway ressources
208
+
209
+
```yml
210
+
stepFunctions:
211
+
stateMachines:
212
+
hello:
213
+
events:
214
+
- http:
215
+
path: users/create
216
+
method: POST
217
+
```
218
+
172
219
#### Enabling CORS
173
220
174
221
To set CORS configurations for your HTTP endpoints, simply modify your event configurations as follows:
@@ -210,6 +257,21 @@ stepFunctions:
210
257
211
258
Configuring the cors property sets Access-Control-Allow-Origin, Access-Control-Allow-Headers, Access-Control-Allow-Methods,Access-Control-Allow-Credentials headers in the CORS preflight response.
212
259
260
+
To enable the Access-Control-Max-Age preflight response header, set the maxAge property in the cors object:
261
+
262
+
```yml
263
+
stepFunctions:
264
+
stateMachines:
265
+
SfnApiGateway:
266
+
events:
267
+
- http:
268
+
path: /playground/start
269
+
method: post
270
+
cors:
271
+
origin: '*'
272
+
maxAge: 86400
273
+
```
274
+
213
275
#### Customizing request body mapping templates
214
276
215
277
The plugin generates default body mapping templates for `application/json` and `application/x-www-form-urlencoded` content types. If you'd like to add more content types or customize the default ones, you can do so by including them in `serverless.yml`:
0 commit comments