Skip to content

Commit 718ad91

Browse files
jacintoAriasdaniel-cottone
authored andcommitted
Update README with info for changing AWS profile (dherault#380)
As commented in dherault#195 we should reflect that AWS profile can only be changed by env variable.
1 parent 6027f19 commit 718ad91

File tree

1 file changed

+61
-36
lines changed

1 file changed

+61
-36
lines changed

README.md

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Serverless Offline Plugin
2+
23
[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com)
34
[![npm version](https://badge.fury.io/js/serverless-offline.svg)](https://badge.fury.io/js/serverless-offline)
45
[![Build Status](https://travis-ci.org/dherault/serverless-offline.svg?branch=master)](https://travis-ci.org/dherault/serverless-offline)
@@ -18,28 +19,29 @@ This [Serverless](https://github.com/serverless/serverless) plugin emulates AWS
1819
To do so, it starts an HTTP server that handles the request's lifecycle like APIG does and invokes your handlers.
1920

2021
**Features:**
21-
- Nodejs λ only.
22-
- Velocity templates support.
23-
- Lazy loading of your files with require cache invalidation: no need for a reloading tool like Nodemon.
24-
- And more: integrations, authorizers, proxies, timeouts, responseParameters, HTTPS, Babel runtime, CORS, etc...
22+
23+
* Nodejs λ only.
24+
* Velocity templates support.
25+
* Lazy loading of your files with require cache invalidation: no need for a reloading tool like Nodemon.
26+
* And more: integrations, authorizers, proxies, timeouts, responseParameters, HTTPS, Babel runtime, CORS, etc...
2527

2628
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).
2729

2830
## Documentation
2931

30-
- [Installation](#installation)
31-
- [Usage and command line options](#usage-and-command-line-options)
32-
- [Usage with Babel](#usage-with-babel)
33-
- [Token authorizers](#token-authorizers)
34-
- [Custom authorizers](#custom-authorizers)
35-
- [AWS API Gateway features](#aws-api-gateway-features)
36-
- [Velocity nuances](#velocity-nuances)
37-
- [Debug process](#debug-process)
38-
- [Scoped execution](#scoped-execution)
39-
- [Simulation quality](#simulation-quality)
40-
- [Credits and inspiration](#credits-and-inspiration)
41-
- [Contributing](#contributing)
42-
- [License](#license)
32+
* [Installation](#installation)
33+
* [Usage and command line options](#usage-and-command-line-options)
34+
* [Usage with Babel](#usage-with-babel)
35+
* [Token authorizers](#token-authorizers)
36+
* [Custom authorizers](#custom-authorizers)
37+
* [AWS API Gateway features](#aws-api-gateway-features)
38+
* [Velocity nuances](#velocity-nuances)
39+
* [Debug process](#debug-process)
40+
* [Scoped execution](#scoped-execution)
41+
* [Simulation quality](#simulation-quality)
42+
* [Credits and inspiration](#credits-and-inspiration)
43+
* [Contributing](#contributing)
44+
* [License](#license)
4345

4446
## Installation
4547

@@ -52,6 +54,7 @@ First, add Serverless Offline to your project:
5254
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.
5355

5456
It should look something like this:
57+
5558
```YAML
5659
plugins:
5760
- serverless-offline
@@ -61,7 +64,7 @@ You can check wether you have successfully installed the plugin by running the s
6164
6265
`serverless`
6366

64-
the console should display *Offline* as one of the plugins now available in your Serverless project.
67+
the console should display _Offline_ as one of the plugins now available in your Serverless project.
6568

6669
## Usage and command line options
6770

@@ -108,11 +111,11 @@ Options passed on the command line override YAML options.
108111

109112
By default you can send your requests to `http://localhost:3000/`. Please note that:
110113

111-
- You'll need to restart the plugin if you modify your `serverless.yml` or any of the default velocity template files.
112-
- The event object passed to your λs has one extra key: `{ isOffline: true }`. Also, `process.env.IS_OFFLINE` is `true`.
113-
- When no Content-Type header is set on a request, API Gateway defaults to `application/json`, and so does the plugin.
114-
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).
115-
Please consider explicitly setting your requests' Content-Type and using separate templates.
114+
* You'll need to restart the plugin if you modify your `serverless.yml` or any of the default velocity template files.
115+
* The event object passed to your λs has one extra key: `{ isOffline: true }`. Also, `process.env.IS_OFFLINE` is `true`.
116+
* When no Content-Type header is set on a request, API Gateway defaults to `application/json`, and so does the plugin.
117+
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).
118+
Please consider explicitly setting your requests' Content-Type and using separate templates.
116119

117120
## Usage with Babel
118121

@@ -123,6 +126,7 @@ To do so you need to install (at least) the es2015 preset in your project folder
123126

124127
Your λ handlers can be required with `babel-register`.
125128
To do so, in your `serverless.yml` file, set options to be passed to babel-register like this:
129+
126130
```yml
127131
custom:
128132
serverless-offline:
@@ -143,6 +147,7 @@ yarn add -D babel-preset-env babel-preset-flow babel-plugin-transform-runtime ba
143147
```
144148
145149
Then, in your `.babelrc`:
150+
146151
```
147152
{
148153
"presets": [
@@ -178,16 +183,19 @@ Serverless-offline will emulate the behaviour of APIG and create a random token
178183
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.
179184

180185
The Custom authorizer is passed an `event` object as below:
186+
181187
```javascript
182188
{
183189
"type": "TOKEN",
184190
"authorizationToken": "<Incoming bearer token>",
185191
"methodArn": "arn:aws:execute-api:<Region id>:<Account id>:<API id>/<Stage>/<Method>/<Resource path>"
186192
}
187193
```
194+
188195
The `methodArn` does not include the Account id or API id.
189196

190197
The plugin only supports retrieving Tokens from headers. You can configure the header as below:
198+
191199
```javascript
192200
"authorizer": {
193201
"type": "TOKEN",
@@ -230,7 +238,7 @@ Works out of the box.
230238
### Lambda and Lambda Proxy Integrations
231239

232240
[Serverless doc](https://serverless.com/framework/docs/providers/aws/events/apigateway#lambda-proxy-integration)
233-
~ [AWS doc](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html)
241+
~ [AWS doc](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html)
234242

235243
Works out of the box. See examples in the manual_test directory.
236244

@@ -243,12 +251,15 @@ Works out of the box. See examples in the manual_test directory.
243251
[AWS doc - AWS::ApiGateway::Resource](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-resource.html)
244252

245253
Example of enabling proxy:
254+
246255
```
247256
custom:
248257
serverless-offline:
249258
resourceRoutes: true
250259
```
260+
251261
or
262+
252263
```
253264
YourCloudFormationMethodId:
254265
Type: AWS::ApiGateway::Method
@@ -259,6 +270,7 @@ or
259270
Uri: 'https://s3-${self:custom.region}.amazonaws.com/${self:custom.yourBucketName}/{proxy}'
260271
......
261272
```
273+
262274
```
263275
custom:
264276
serverless-offline:
@@ -276,6 +288,7 @@ You can set your response's headers using ResponseParameters.
276288
May not work properly. Please PR. (Difficulty: hard?)
277289

278290
Example response velocity template:
291+
279292
```javascript
280293
"responseParameters": {
281294
"method.response.header.X-Powered-By": "Serverless", // a string
@@ -287,6 +300,7 @@ Example response velocity template:
287300
## Velocity nuances
288301

289302
Consider this requestTemplate for a POST endpoint:
303+
290304
```json
291305
"application/json": {
292306
"payload": "$input.json('$')",
@@ -298,6 +312,7 @@ Consider this requestTemplate for a POST endpoint:
298312
Now let's make a request with this body: `{ "id": 1 }`
299313

300314
AWS parses the event as such:
315+
301316
```javascript
302317
{
303318
"payload": {
@@ -309,6 +324,7 @@ AWS parses the event as such:
309324
```
310325

311326
Whereas Offline parses:
327+
312328
```javascript
313329
{
314330
"payload": {
@@ -327,9 +343,9 @@ You may find other differences.
327343

328344
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.
329345

330-
>Unix: `export SLS_DEBUG=*`
346+
> Unix: `export SLS_DEBUG=*`
331347
332-
>Windows: `SET SLS_DEBUG=*`
348+
> Windows: `SET SLS_DEBUG=*`
333349
334350
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.
335351

@@ -343,6 +359,16 @@ The system will start in wait status. This will also automatically start the chr
343359

344360
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.
345361

362+
## Resource permissions and AWS profile
363+
364+
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`.
365+
366+
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.
367+
368+
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.
369+
370+
`AWS_PROFILE=<profile> serverless offline`
371+
346372
## Scoped execution
347373

348374
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
@@ -353,26 +379,25 @@ Serverless offline plugin can invoke shell scripts when a simulated server has b
353379
## Simulation quality
354380

355381
This plugin simulates API Gateway for many practical purposes, good enough for development - but is not a perfect simulator.
356-
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.
382+
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.
357383

358384
## Usage with serverless-offline and serverless-webpack plugin
359385

360-
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.
386+
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.
361387

362-
Add plugins to your `serverless.yml` file:
363-
```yaml
364-
plugins:
365-
- serverless-webpack
366-
- serverless-dynamodb-local
367-
- serverless-offline #serverless-offline needs to be last in the list
368-
```
388+
Add plugins to your `serverless.yml` file:
369389

390+
```yaml
391+
plugins:
392+
- serverless-webpack
393+
- serverless-dynamodb-local
394+
- serverless-offline #serverless-offline needs to be last in the list
395+
```
370396
371397
## Credits and inspiration
372398
373399
This plugin was initially a fork of [Nopik](https://github.com/Nopik/)'s [Serverless-serve](https://github.com/Nopik/serverless-serve).
374400
375-
376401
## Contributing
377402
378403
Yes, thank you!

0 commit comments

Comments
 (0)