Skip to content

Commit 7235233

Browse files
committed
Merge branch 'master' into add_authorizer_support
2 parents 358e205 + 36bb3a4 commit 7235233

File tree

5 files changed

+94
-6
lines changed

5 files changed

+94
-6
lines changed

CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# 1.5.1(10.07.2018)
2-
## Features
3-
- Added support for Shared API Gateway
4-
51
# 0.2.0(08.01.2017)
62
## Breaking Changes
73
The service name is added to the statemachine prefix.

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,53 @@ stepFunctions:
169169
definition:
170170
```
171171

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+
172219
#### Enabling CORS
173220

174221
To set CORS configurations for your HTTP endpoints, simply modify your event configurations as follows:
@@ -210,6 +257,21 @@ stepFunctions:
210257

211258
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.
212259

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+
213275
#### Customizing request body mapping templates
214276

215277
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`:

lib/deploy/events/apiGateway/validate.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ module.exports = {
2626
cors.origin = http.cors.origin || '*';
2727
cors.allowCredentials = cors.allowCredentials || http.cors.allowCredentials;
2828

29+
// when merging, last one defined wins
30+
if (_.has(http.cors, 'maxAge')) {
31+
cors.maxAge = http.cors.maxAge;
32+
}
33+
2934
corsPreflight[http.path] = cors;
3035
}
3136

lib/deploy/events/apiGateway/validate.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,29 @@ describe('#httpValidate()', () => {
413413
expect(validated.events).to.be.an('Array').with.length(1);
414414
expect(validated.events[0].http.cors.methods).to.deep.equal(['POST', 'OPTIONS']);
415415
});
416+
417+
it('should set cors Access-Control-Max-Age headers', () => {
418+
serverlessStepFunctions.serverless.service.stepFunctions = {
419+
stateMachines: {
420+
first: {
421+
events: [
422+
{
423+
http: {
424+
method: 'POST',
425+
path: '/foo/bar',
426+
cors: {
427+
origin: '*',
428+
maxAge: 86400,
429+
},
430+
},
431+
},
432+
],
433+
},
434+
},
435+
};
436+
437+
const validated = serverlessStepFunctions.httpValidate();
438+
expect(validated.events[0].http.cors.origin).to.equal('*');
439+
expect(validated.events[0].http.cors.maxAge).to.equal(86400);
440+
});
416441
});

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-step-functions",
3-
"version": "1.6.0",
3+
"version": "1.5.0",
44
"description": "The module is AWS Step Functions plugin for Serverless Framework",
55
"main": "lib/index.js",
66
"scripts": {
@@ -32,7 +32,7 @@
3232
"istanbul": "^0.4.4",
3333
"mocha": "^5.2.0",
3434
"mocha-lcov-reporter": "^1.2.0",
35-
"serverless": "^1.28.0",
35+
"serverless": "^1.27.3",
3636
"sinon": "^1.17.5"
3737
},
3838
"dependencies": {

0 commit comments

Comments
 (0)