99- [ Develop locally with cloud resources] ( #develop-locally-with-cloud-resources )
1010- [ Diagnose issues] ( #diagnose-issues )
1111
12+ ## Architecture
13+
14+ ![ Architecture] ( docs/json-serverless.png )
15+
1216## Features
1317
14- - Development:
15- - Easily setup routes and resources for the REST Api via json file. [ (via json-server)] ( https://github.com/typicode/json-server )
16- - This solution written in ** NodeJS** can be easily extended for additional enhanced scenarios
17- - adding user authentication
18- - own custom domain
19- - additional routes etc.
20- - Develop and test solution locally in Visual Studio Code
21- - Security: This Api is secured via API Key and https by default.
18+ - Easily setup routes and resources for the REST Api via json file. [ (via json-server)] ( https://github.com/typicode/json-server )
19+ - ** New:** Added Swagger UI support
2220- Deployment:
2321 - Deployed in AWS cloud within Minutes by a single command
2422 - Almost ** zero costs** (First million requests for Lambda are free)
2523 - Less maintenance as the deployed solution runs ** serverless**
24+ - Security:
25+ - Secured with https by default.
26+ - Optional: Use a generated API Key
27+ - Customization:
28+ - This solution written in ** NodeJS** can be easily extended for additional enhanced scenarios
29+ - adding user authentication
30+ - own custom domain
31+ - additional routes etc.
32+ - Develop and debug solution locally in Visual Studio Code
2633
2734## Quickstart
2835
29- ##### 1. Clone Solution
36+ ### 1. Clone Solution
3037
3138``` bash
32- git clone https://github.com/pharindoko/json-serverless.git
39+ git clone https://github.com/pharindoko/json-serverless.git
3340cd json-serverless
3441```
3542
36- ##### 2. Install dependencies
43+ ### 2. Install dependencies
3744
3845``` bash
3946npm install -g serverless
4047npm i
4148```
4249
43- ##### 3. Verify AWS Access / Credentials
50+ ### 3. Verify AWS Access / Credentials
4451
4552=> You need to have access to AWS to upload the solution.
4653
4754``` bash
4855aws sts get-caller-identity
4956```
5057
51- ##### 4. Update db.json file in root directory
58+ ### 4. Update db.json file in root directory
5259
5360- Childproperties are the REST endpoints you create
5461- Samplefile: Routes marked ** bold**
@@ -61,8 +68,7 @@ aws sts get-caller-identity
6168}
6269</code ></pre >
6370
64- ##### 5. Deploy via Serverless Framework
65-
71+ ### 5. Deploy via Serverless Framework
6672
6773``` bash
6874# set --stage parameter for different stages
@@ -72,7 +78,7 @@ serverless deploy --stage dev
7278- serverless-webpack is used
7379- the build will be triggered automatically
7480
75- ##### 6. When the deployment with serverless framework was successful you can see following output
81+ ### 6. When the deployment with serverless framework was successful you can see following output
7682
7783<pre >
7884<code >
@@ -82,9 +88,9 @@ stage: dev
8288region: eu-central-1
8389stack: serverless-json-server-dev
8490api keys:
85- serverless-json-server.dev: <b >{API - KEY}</b >
91+ serverless-json-server.dev: <b >{API- KEY}</b >
8692endpoints:
87- ANY - <b >https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/</b >
93+ ANY - <b >https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/ <== {ENDPOINTURL} </b >
8894 ANY - https://xxxxxxx.eu-central-1.amazonaws.com/dev/{proxy+}
8995functions:
9096 app: serverless-json-server-dev-app
@@ -93,45 +99,40 @@ layers:
9399Serverless: Removing old service artifacts from S3...
94100</pre ></code >
95101
96- ##### 7. Test your Api
102+ ### 7. Test your Api
103+
104+ #### With Swagger
105+
106+ Open the {ENDPOINTURL}: https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/ that you received as output
97107
98- ##### With Curl
108+ ** MIND** : If you have set enableApiKeyAuth to true => [ SwaggerUI] ( #Cannot-use-Swagger-UI-when-enableApiKeyAuth-is-true )
109+ )
110+
111+ #### With Curl
99112
1001131 . replace the url with the url provided by serverless (see above)
101- 2 . replace the {API - KEY} with the key you get from serverless (see above)
114+ 2 . replace the {API- KEY} with the key you get from serverless (see above)
1021153 . replace {route} at the end of the url e.g. with posts (default value)
103116
104117Default Schema:
105118
106119``` bash
107120Default route is posts: (see db.json)
108- curl -H " x-api-key: {API - KEY} " -H " Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/posts
121+ curl -H " Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/ posts
109122
110- # or another route given in db.json file
111- curl -H " x-api-key: {API - KEY}" -H " Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/{route}
112- ```
123+ # or another route given in db.json file
124+ curl -H " Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}
113125
114- ##### With Postman
126+ # with enableApiKeyAuth=true
127+ curl -H " x-api-key: {API-KEY}" -H " Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}
115128
116- - Create a new GET Request and add these values to the header section
117-
118- | Key| Value|
119- | ---| ---|
120- | x-api-key | {API - KEY}|
121- | Content-Type | application/json|
122-
123- - Enter as Url the endpoints url
124-
125- ``` bash
126- https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/{route}
127- # e.g. default value: https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/posts
128129```
129130
130131What`s my {route} ? -> see [ json-server documentation] ( https://github.com/typicode/json-server )
131132
132133## Customization
133134
134- #### Update content of db.json
135+ ### Update content of db.json
135136
1361371 . update local db.json file in root directory with new values
1371382 . re-deploy the stack via serverless framework
@@ -141,10 +142,15 @@ What`s my {route} ? -> see [json-server documentation](https://github.com/typico
141142 ```
142143
1431443 . delete db.json file in S3 Bucket
144- 4 . Make a GET request against the root url https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/
145+ 4 . Make a GET request against the root url https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api
145146
146147``` bash
147- curl -H " x-api-key: {API - KEY}" -H " Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev
148+ curl -H " Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api
149+
150+ # with enableApiKeyAuth=true
151+ curl -H " x-api-key: {API-KEY}" -H " Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}
152+
153+
148154```
149155
150156=> With the next request a new db.json file will be created in the S3 Bucket
@@ -153,13 +159,13 @@ curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxx
153159
154160[ edit service property in serverless.yml (in root directory)] ( https://github.com/pharindoko/json-server-less-lambda/blob/66756961d960c44cf317ca307b097f595799a890/serverless.yml#L8 )
155161
156- #### Adapt settings in config/servleressconfig .yml file
162+ #### Adapt settings in config/appconfig .yml file
157163
158164| Attribute | Description | Type | Default |
159165| ---| ---| ---| ---|
160- | S3FILE | JSON file used as db to read and write (will be created with a default json value - customize in db.json) | string | db.json |
161- | S3BUCKET | S3-Bucket - this bucket must already exist in AWS | string | json-server-less-lambda-dev |
162- | READONLY | all API - write operations are forbidden (http 403)) | boolean | false |
166+ | readOnly | Make API readonly - all API - write operations are forbidden (http 403)) | string | false |
167+ | enableSwagger | Enable swagger and swagger UI support | string | true |
168+ | enableApiKeyAuth | Make your routes private by using an additional ApiKey | boolean | false |
163169
164170## Used Packages
165171
@@ -170,7 +176,7 @@ curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxx
170176
171177## Components
172178
173- - [ NodeJS 8.10] ( https://nodejs.org/en/about/ )
179+ - [ NodeJS 8.10] ( https://nodejs.org/en/about/ )
174180- [ AWS API Gateway] ( https://aws.amazon.com/api-gateway/ )
175181- [ AWS Lambda] ( https://aws.amazon.com/lambda/features/ )
176182- [ AWS S3] ( https://aws.amazon.com/s3/ )
@@ -179,13 +185,14 @@ curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxx
179185
180186db.json file will be loaded directly from your local filesystem. No AWS access is needed.
181187
182- #### Start solution
188+ ### Start solution
183189
184190``` bash
185191npm run start
186192```
187193
188- #### Debug solution
194+ ### Debug solution
195+
189196If you want to debug locally in VS Code everything is already setup (using webpack with sourcemap support)
190197
191198``` bash
@@ -194,13 +201,24 @@ npm run debug
194201
195202#### 2. Test your API
196203
197- To test you can use e.g. [ Postman ] ( https://www.getpostman.com/ )
204+ #### With Swagger
198205
199- - Open Postman
200- - Enter as Url the endpoints url
206+ Open the {ENDPOINTURL}: http://localhost:3000/ that you received as output
207+
208+ #### With Curl
209+
210+ 1 . replace the url with the url provided by serverless (see above)
211+ 2 . replace the {API - KEY} with the key you get from serverless (see above)
212+ 3 . replace {route} at the end of the url e.g. with posts (default value)
213+
214+ Default Schema:
201215
202216``` bash
203- https://localhost:3000/{route} # e.g. default value: https://localhost:3000/posts/
217+ Default route is posts: (see db.json)
218+ curl -H " Content-Type: application/json" http://localhost:3000/api/posts
219+
220+ # or another route given in db.json file
221+ curl -H " Content-Type: application/json" http://localhost:3000/api/{route}
204222```
205223
206224What`s my {route} ? -> see [ json-server documentation] ( https://github.com/typicode/json-server )
@@ -209,7 +227,7 @@ What`s my {route} ? -> see [json-server documentation](https://github.com/typico
209227
210228Use same componentes (S3, LowDB) as the lambda does but have code executed locally.
211229
212- #### 1. Add .env file to root folder
230+ ### 1. Add .env file to root folder
213231
214232** Mind:** If you haven`t deployed the solution yet, please create a private S3-Bucket and .json - file manually or deploy the solution first to AWS via serverless framework<br >
215233** Mind:** This function requires that you have access to AWS (e.g. via credentials)
@@ -224,9 +242,9 @@ cp .env.sample .env
224242
225243| Attribute | Description | Type | Default |
226244| ---| ---| ---| ---|
227- | S3FILE | JSON file used as db to read and write (will be created with a default json value - customize in db.json) | string | db.json |
228- | S3BUCKET | S3-Bucket - this bucket must already exist in AWS | string | json-server-less-lambda-dev |
229- | READONLY | all API - write operations are forbidden (http 403)) | boolean | false |
245+ | S3File | JSON file used as db to read and write (will be created with a default json value - customize in db.json) | string | db.json |
246+ | S3Bucket | S3-Bucket - this bucket must already exist in AWS | string | json-server-less-lambda-dev |
247+ | readOnly | all API - write operations are forbidden (http 403)) | boolean | false |
230248
231249#### 2. Start solution
232250
@@ -236,13 +254,28 @@ npm run dev
236254
237255#### 3. Test your API
238256
239- To test you can use e.g. [ Postman] ( https://www.getpostman.com/ )
257+ #### With Swagger
258+
259+ Open the {ENDPOINTURL}: http://localhost:3000/ that you received as output
260+
261+ #### With Curl
262+
263+ 1 . replace the url with the url provided by serverless (see above)
264+ 2 . replace the {API - KEY} with the key you get from serverless (see above)
265+ 3 . replace {route} at the end of the url e.g. with posts (default value)
240266
241- - Open Postman
242- - Enter as Url the endpoints url
267+ Default Schema:
243268
244269``` bash
245- https://localhost:3000/{route} # e.g. default value: https://localhost:3000/posts
270+ Default route is posts: (see db.json)
271+ curl -H " Content-Type: application/json" http://localhost:3000/api/posts
272+
273+ # or another route given in db.json file
274+ curl -H " Content-Type: application/json" http://localhost:3000/api/{route}
275+
276+ # with enableApiKeyAuth=true
277+ curl -H " x-api-key: {API-KEY}" -H " Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}
278+
246279```
247280
248281What`s my {route} ? -> see [ json-server documentation] ( https://github.com/typicode/json-server )
@@ -254,7 +287,7 @@ serverless-offline will help you to troubleshoot issues with the lambda executio
254287** Mind:** The assumption is that the solution has been already deployed<br >
255288** Mind:** This function requires that you have access to AWS (e.g. via credentials)
256289
257- #### 1. build sources and execute serverless offline
290+ ### 1. build sources and execute serverless offline
258291
259292- sources will be build with babel in advance to test the functionality.
260293- after that sls offline will be started
@@ -276,5 +309,39 @@ Serverless: Remember to use <b>x-api-key</b> on the request headers
276309- Replace {route} with the route you want to test e.g. /posts
277310
278311<pre ><code >
279- curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" http://localhost:3000/{route}
280- </pre ></code >
312+ curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" http://localhost:3000/api/{route}
313+ </pre ></code >
314+
315+ ## FAQ
316+
317+ ### How can I change the lambda region or stack name
318+
319+ Please have a look to the serverless guideline: https://serverless.com/framework/docs/providers/aws/guide/deploying/
320+
321+ ### Cannot use Swagger UI when enableApiKeyAuth is true
322+
323+ The apiKey is set in AWS API Gateway. This means all requests (even the standard route) need to use the API-KEY.
324+
325+ If you want to see the Swagger UI you need to add a plugin e.g. ModHeader to Chrome and add the needed headers:
326+ - Content-Type: application/json
327+ - x-api-key: {provided by sls info in the output after deployment}
328+
329+ ![ ModHeader] ( docs/header.png )
330+
331+ ### I forgot the API-KEY I have set
332+
333+ Ensure you have credentials for AWS set.
334+
335+ ``` bash
336+ sls info
337+ ```
338+
339+ ### Destroy the stack in the cloud
340+
341+ ``` bash
342+ sls remove
343+ ```
344+
345+ ### I deployed the solution but I get back a http 500 error
346+
347+ Check Cloudwatch Logs in AWS - the issue should be describe there. Log has the same name as the stack that has been created.
0 commit comments