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
This plugin is updated by its users, I just do maintenance and ensure that PRs are relevant to the community. In other words, if you [find a bug or want a new feature](https://github.com/dherault/serverless-offline/issues), please help us by becoming one of the [contributors](https://github.com/dherault/serverless-offline/graphs/contributors):v: ! See the [contributing section](#contributing). We are looking for maintainers, see [this issue](https://github.com/dherault/serverless-offline/issues/304).
*[Usage and command line options](#usage-and-command-line-options)
24
+
*[Usage with Babel](#usage-with-babel)
25
+
*[Token authorizers](#token-authorizers)
26
+
*[Custom authorizers](#custom-authorizers)
27
+
*[AWS API Gateway features](#aws-api-gateway-features)
28
+
*[Velocity nuances](#velocity-nuances)
29
+
*[Debug process](#debug-process)
30
+
*[Scoped execution](#scoped-execution)
31
+
*[Simulation quality](#simulation-quality)
32
+
*[Credits and inspiration](#credits-and-inspiration)
33
+
*[Contributing](#contributing)
34
+
*[License](#license)
31
35
32
36
## Installation
33
37
@@ -40,6 +44,7 @@ First, add Serverless Offline to your project:
40
44
Then inside your project's `serverless.yml` file add following entry to the plugins section: `serverless-offline`. If there is no plugin section you will need to add it to the file.
41
45
42
46
It should look something like this:
47
+
43
48
```YAML
44
49
plugins:
45
50
- serverless-offline-python
@@ -49,7 +54,7 @@ You can check wether you have successfully installed the plugin by running the s
49
54
50
55
`serverless`
51
56
52
-
the console should display *Offline* as one of the plugins now available in your Serverless project.
57
+
the console should display _Offline_ as one of the plugins now available in your Serverless project.
53
58
54
59
## Usage and command line options
55
60
@@ -72,13 +77,16 @@ All CLI options are optional:
72
77
--region -r The region used to populate your templates. Default: the first region for the first stage found.
73
78
--noTimeout -t Disables the timeout feature.
74
79
--noEnvironment Turns off loading of your environment variables from serverless.yml. Allows the usage of tools such as PM2 or docker-compose.
80
+
--resourceRoutes Turns on loading of your HTTP proxy settings from serverless.yml.
75
81
--dontPrintOutput Turns off logging of your lambda outputs in the terminal.
76
82
--httpsProtocol -H To enable HTTPS, specify directory (relative to your cwd, typically your project dir) for both cert.pem and key.pem files.
77
83
--skipCacheInvalidation -c Tells the plugin to skip require cache invalidation. A script reloading tool like Nodemon might then be needed.
78
84
--corsAllowOrigin Used as default Access-Control-Allow-Origin header value for responses. Delimit multiple values with commas. Default: '*'
79
85
--corsAllowHeaders Used as default Access-Control-Allow-Headers header value for responses. Delimit multiple values with commas. Default: 'accept,content-type,x-api-key'
80
86
--corsDisallowCredentials When provided, the default Access-Control-Allow-Credentials header value will be passed as 'false'. Default: true
81
-
--exec "<script>" When provided, a shell script is executed when the server starts up, and the server will shut domn after handling this command.
87
+
--exec "<script>" When provided, a shell script is executed when the server starts up, and the server will shut down after handling this command.
88
+
--noAuth Turns off all authorizers
89
+
--preserveTrailingSlash Used to keep trailing slashes on the request path
82
90
```
83
91
84
92
Any of the CLI options can be added to your `serverless.yml`. For example:
@@ -94,11 +102,11 @@ Options passed on the command line override YAML options.
94
102
95
103
By default you can send your requests to `http://localhost:3000/`. Please note that:
96
104
97
-
-You'll need to restart the plugin if you modify your `serverless.yml` or any of the default velocity template files.
98
-
-The event object passed to your λs has one extra key: `{ isOffline: true }`. Also, `process.env.IS_OFFLINE` is `true`.
99
-
-When no Content-Type header is set on a request, API Gateway defaults to `application/json`, and so does the plugin.
100
-
But if you send an `application/x-www-form-urlencoded` or a `multipart/form-data` body with an `application/json` (or no) Content-Type, API Gateway won't parse your data (you'll get the ugly raw as input), whereas the plugin will answer 400 (malformed JSON).
101
-
Please consider explicitly setting your requests' Content-Type and using separate templates.
105
+
* You'll need to restart the plugin if you modify your `serverless.yml` or any of the default velocity template files.
106
+
* The event object passed to your λs has one extra key: `{ isOffline: true }`. Also, `process.env.IS_OFFLINE` is `true`.
107
+
* When no Content-Type header is set on a request, API Gateway defaults to `application/json`, and so does the plugin.
108
+
But if you send an `application/x-www-form-urlencoded` or a `multipart/form-data` body with an `application/json` (or no) Content-Type, API Gateway won't parse your data (you'll get the ugly raw as input), whereas the plugin will answer 400 (malformed JSON).
109
+
Please consider explicitly setting your requests' Content-Type and using separate templates.
102
110
103
111
## Usage with Babel
104
112
@@ -109,6 +117,7 @@ To do so you need to install (at least) the es2015 preset in your project folder
109
117
110
118
Your λ handlers can be required with `babel-register`.
111
119
To do so, in your `serverless.yml` file, set options to be passed to babel-register like this:
120
+
112
121
```yml
113
122
custom:
114
123
serverless-offline:
@@ -118,6 +127,41 @@ custom:
118
127
119
128
Here is the full list of [babel-register options](https://babeljs.io/docs/usage/require/)
120
129
130
+
## Usage with Flow
131
+
132
+
If you're using [Flow](https://flow.org/en/) in your service, you'll need to update your `babelOptions` as mentioned [above](#usage-with-babel).
133
+
134
+
Ensure that `babel-preset-flow` and `transform-flow-strip-types` are installed and properly configured in your project.
See the [docs](https://flow.org/en/docs/install/) for additional details on setting up Flow.
156
+
157
+
Finally, add the `"flow"` preset to your `babelOptions`:
158
+
159
+
```yml
160
+
custom:
161
+
serverless-offline:
162
+
babelOptions:
163
+
presets: ["env", "flow"]
164
+
```
121
165
122
166
## Token Authorizers
123
167
@@ -130,16 +174,19 @@ Serverless-offline will emulate the behaviour of APIG and create a random token
130
174
Only [custom authorizers](https://aws.amazon.com/blogs/compute/introducing-custom-authorizers-in-amazon-api-gateway/) are supported. Custom authorizers are executed before a Lambda function is executed and return an Error or a Policy document.
131
175
132
176
The Custom authorizer is passed an `event` object as below:
@@ -195,6 +279,7 @@ You can set your response's headers using ResponseParameters.
195
279
May not work properly. Please PR. (Difficulty: hard?)
196
280
197
281
Example response velocity template:
282
+
198
283
```javascript
199
284
"responseParameters": {
200
285
"method.response.header.X-Powered-By":"Serverless", // a string
@@ -206,6 +291,7 @@ Example response velocity template:
206
291
## Velocity nuances
207
292
208
293
Consider this requestTemplate for a POST endpoint:
294
+
209
295
```json
210
296
"application/json": {
211
297
"payload": "$input.json('$')",
@@ -217,6 +303,7 @@ Consider this requestTemplate for a POST endpoint:
217
303
Now let's make a request with this body: `{ "id": 1 }`
218
304
219
305
AWS parses the event as such:
306
+
220
307
```javascript
221
308
{
222
309
"payload": {
@@ -228,6 +315,7 @@ AWS parses the event as such:
228
315
```
229
316
230
317
Whereas Offline parses:
318
+
231
319
```javascript
232
320
{
233
321
"payload": {
@@ -246,9 +334,9 @@ You may find other differences.
246
334
247
335
Serverless offline plugin will respond to the overall framework settings and output additional information to the console in debug mode. In order to do this you will have to set the `SLS_DEBUG` environmental variable. You can run the following in the command line to switch to debug mode execution.
248
336
249
-
>Unix: `export SLS_DEBUG=*`
337
+
>Unix: `export SLS_DEBUG=*`
250
338
251
-
>Windows: `SET SLS_DEBUG=*`
339
+
>Windows: `SET SLS_DEBUG=*`
252
340
253
341
Interactive debugging is also possible for your project if you have installed the node-inspector module and chrome browser. You can then run the following command line inside your project's root.
254
342
@@ -262,6 +350,16 @@ The system will start in wait status. This will also automatically start the chr
262
350
263
351
Depending on the breakpoint, you may need to call the URL path for your function in seperate browser window for your serverless function to be run and made available for debugging.
264
352
353
+
## Resource permissions and AWS profile
354
+
355
+
Lambda functions assume an IAM role during execution: the framework creates this role and set all the permission provided in the `iamRoleStatements` section of `serverless.yml`.
356
+
357
+
However, serverless offline makes use of your local AWS profile credentials to run the lambda functions and that might result in a different set of permissions. By default, the aws-sdk would load credentials for you default AWS profile specified in your configuration file.
358
+
359
+
You can change this profile directly in the code or by setting proper environment variables. Setting the `AWS_PROFILE` environment variable before calling `serverless` offline to a different profile would effectively change the credentials, e.g.
360
+
361
+
`AWS_PROFILE=<profile> serverless offline`
362
+
265
363
## Scoped execution
266
364
267
365
Serverless offline plugin can invoke shell scripts when a simulated server has been started up for the purposes of integration testing. Downstream plugins may tie into the
@@ -272,26 +370,25 @@ Serverless offline plugin can invoke shell scripts when a simulated server has b
272
370
## Simulation quality
273
371
274
372
This plugin simulates API Gateway for many practical purposes, good enough for development - but is not a perfect simulator.
275
-
Specifically, Lambda currently runs on Node v4.3.2 and v6.10.0, whereas *Offline* runs on your own runtime where no memory limits are enforced.
373
+
Specifically, Lambda currently runs on Node v4.3.2 and v6.10.0, whereas _Offline_ runs on your own runtime where no memory limits are enforced.
276
374
277
375
## Usage with serverless-offline and serverless-webpack plugin
278
376
279
-
Run `serverless offline start`. In comparison with `serverless offline`, the `start` command will fire an `init` and a `end` lifecycle hook which is needed for serverless-offline and serverless-dynamodb-local to switch off ressources.
280
-
281
-
Add plugins to your `serverless.yml` file:
282
-
```yaml
283
-
plugins:
284
-
- serverless-webpack
285
-
- serverless-dynamodb-local
286
-
- serverless-offline #serverless-offline needs to be last in the list
287
-
```
377
+
Run `serverless offline start`. In comparison with `serverless offline`, the `start` command will fire an `init` and a `end` lifecycle hook which is needed for serverless-offline and serverless-dynamodb-local to switch off ressources.
288
378
379
+
Add plugins to your `serverless.yml` file:
380
+
381
+
```yaml
382
+
plugins:
383
+
- serverless-webpack
384
+
- serverless-dynamodb-local
385
+
- serverless-offline #serverless-offline needs to be last in the list
386
+
```
289
387
290
388
## Credits and inspiration
291
389
292
390
This plugin was initially a fork of [Nopik](https://github.com/Nopik/)'s [Serverless-serve](https://github.com/Nopik/serverless-serve).
0 commit comments