9
9
- [ Develop locally with cloud resources] ( #develop-locally-with-cloud-resources )
10
10
- [ Diagnose issues] ( #diagnose-issues )
11
11
12
+ ## Architecture
13
+
14
+ ![ Architecture] ( docs/json-serverless.png )
15
+
12
16
## Features
13
17
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
22
20
- Deployment:
23
21
- Deployed in AWS cloud within Minutes by a single command
24
22
- Almost ** zero costs** (First million requests for Lambda are free)
25
23
- 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
26
33
27
34
## Quickstart
28
35
29
- ##### 1. Clone Solution
36
+ ### 1. Clone Solution
30
37
31
38
``` bash
32
- git clone https://github.com/pharindoko/json-serverless.git
39
+ git clone https://github.com/pharindoko/json-serverless.git
33
40
cd json-serverless
34
41
```
35
42
36
- ##### 2. Install dependencies
43
+ ### 2. Install dependencies
37
44
38
45
``` bash
39
46
npm install -g serverless
40
47
npm i
41
48
```
42
49
43
- ##### 3. Verify AWS Access / Credentials
50
+ ### 3. Verify AWS Access / Credentials
44
51
45
52
=> You need to have access to AWS to upload the solution.
46
53
47
54
``` bash
48
55
aws sts get-caller-identity
49
56
```
50
57
51
- ##### 4. Update db.json file in root directory
58
+ ### 4. Update db.json file in root directory
52
59
53
60
- Childproperties are the REST endpoints you create
54
61
- Samplefile: Routes marked ** bold**
@@ -61,8 +68,7 @@ aws sts get-caller-identity
61
68
}
62
69
</code ></pre >
63
70
64
- ##### 5. Deploy via Serverless Framework
65
-
71
+ ### 5. Deploy via Serverless Framework
66
72
67
73
``` bash
68
74
# set --stage parameter for different stages
@@ -72,7 +78,7 @@ serverless deploy --stage dev
72
78
- serverless-webpack is used
73
79
- the build will be triggered automatically
74
80
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
76
82
77
83
<pre >
78
84
<code >
@@ -82,9 +88,9 @@ stage: dev
82
88
region: eu-central-1
83
89
stack: serverless-json-server-dev
84
90
api keys:
85
- serverless-json-server.dev: <b >{API - KEY}</b >
91
+ serverless-json-server.dev: <b >{API- KEY}</b >
86
92
endpoints:
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 >
88
94
ANY - https://xxxxxxx.eu-central-1.amazonaws.com/dev/{proxy+}
89
95
functions:
90
96
app: serverless-json-server-dev-app
@@ -93,45 +99,40 @@ layers:
93
99
Serverless: Removing old service artifacts from S3...
94
100
</pre ></code >
95
101
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
97
107
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
99
112
100
113
1 . 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)
102
115
3 . replace {route} at the end of the url e.g. with posts (default value)
103
116
104
117
Default Schema:
105
118
106
119
``` bash
107
120
Default 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
109
122
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}
113
125
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}
115
128
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
128
129
```
129
130
130
131
What`s my {route} ? -> see [ json-server documentation] ( https://github.com/typicode/json-server )
131
132
132
133
## Customization
133
134
134
- #### Update content of db.json
135
+ ### Update content of db.json
135
136
136
137
1 . update local db.json file in root directory with new values
137
138
2 . re-deploy the stack via serverless framework
@@ -141,10 +142,15 @@ What`s my {route} ? -> see [json-server documentation](https://github.com/typico
141
142
```
142
143
143
144
3 . 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
145
146
146
147
``` 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
+
148
154
```
149
155
150
156
=> 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
153
159
154
160
[ edit service property in serverless.yml (in root directory)] ( https://github.com/pharindoko/json-server-less-lambda/blob/66756961d960c44cf317ca307b097f595799a890/serverless.yml#L8 )
155
161
156
- #### Adapt settings in config/servleressconfig .yml file
162
+ #### Adapt settings in config/appconfig .yml file
157
163
158
164
| Attribute | Description | Type | Default |
159
165
| ---| ---| ---| ---|
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 |
163
169
164
170
## Used Packages
165
171
@@ -170,7 +176,7 @@ curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxx
170
176
171
177
## Components
172
178
173
- - [ NodeJS 8.10] ( https://nodejs.org/en/about/ )
179
+ - [ NodeJS 8.10] ( https://nodejs.org/en/about/ )
174
180
- [ AWS API Gateway] ( https://aws.amazon.com/api-gateway/ )
175
181
- [ AWS Lambda] ( https://aws.amazon.com/lambda/features/ )
176
182
- [ 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
179
185
180
186
db.json file will be loaded directly from your local filesystem. No AWS access is needed.
181
187
182
- #### Start solution
188
+ ### Start solution
183
189
184
190
``` bash
185
191
npm run start
186
192
```
187
193
188
- #### Debug solution
194
+ ### Debug solution
195
+
189
196
If you want to debug locally in VS Code everything is already setup (using webpack with sourcemap support)
190
197
191
198
``` bash
@@ -194,13 +201,24 @@ npm run debug
194
201
195
202
#### 2. Test your API
196
203
197
- To test you can use e.g. [ Postman ] ( https://www.getpostman.com/ )
204
+ #### With Swagger
198
205
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:
201
215
202
216
``` 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}
204
222
```
205
223
206
224
What`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
209
227
210
228
Use same componentes (S3, LowDB) as the lambda does but have code executed locally.
211
229
212
- #### 1. Add .env file to root folder
230
+ ### 1. Add .env file to root folder
213
231
214
232
** 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 >
215
233
** Mind:** This function requires that you have access to AWS (e.g. via credentials)
@@ -224,9 +242,9 @@ cp .env.sample .env
224
242
225
243
| Attribute | Description | Type | Default |
226
244
| ---| ---| ---| ---|
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 |
230
248
231
249
#### 2. Start solution
232
250
@@ -236,13 +254,28 @@ npm run dev
236
254
237
255
#### 3. Test your API
238
256
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)
240
266
241
- - Open Postman
242
- - Enter as Url the endpoints url
267
+ Default Schema:
243
268
244
269
``` 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
+
246
279
```
247
280
248
281
What`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
254
287
** Mind:** The assumption is that the solution has been already deployed<br >
255
288
** Mind:** This function requires that you have access to AWS (e.g. via credentials)
256
289
257
- #### 1. build sources and execute serverless offline
290
+ ### 1. build sources and execute serverless offline
258
291
259
292
- sources will be build with babel in advance to test the functionality.
260
293
- after that sls offline will be started
@@ -276,5 +309,39 @@ Serverless: Remember to use <b>x-api-key</b> on the request headers
276
309
- Replace {route} with the route you want to test e.g. /posts
277
310
278
311
<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