1.6.16 • Published 3 years ago

json-serverless-lib v1.6.16

Weekly downloads
70
License
MIT
Repository
github
Last release
3 years ago

JSON Serverless Renovate enabled Build Status License: MIT

Architecture

Architecture

Features

  • Easily generate routes and resources for the Api via (json-server)
  • New: Added Swagger UI support
  • Deployment:
    • Deployed in AWS cloud within Minutes by a single command
    • Almost zero costs (First million requests for Lambda are free)
    • Less maintenance as the deployed solution runs serverless
  • Security:
    • Secured with https by default.
    • Optional: Use a generated API Key
  • Customization:
    • This solution written in Typescript can be easily extended for additional enhanced scenarios
      • adding user authentication
      • own custom domain
      • additional routes etc.
    • Develop and debug solution locally in Visual Studio Code

Quickstart

1. Clone Solution

git clone https://github.com/pharindoko/json-serverless.git
cd json-serverless

2. Install dependencies

npm install -g serverless
npm i

3. Verify AWS Access / Credentials

=> You need to have access to AWS to upload the solution.

aws sts get-caller-identity

4. Update db.json file in root directory

5. Deploy via Serverless Framework

# set --stage parameter for different stages
serverless deploy --stage dev
  • serverless-webpack is used
  • the build will be triggered automatically

6. When the deployment with serverless framework was successful you can see following output

7. Test your Api

With Swagger

Open the {ENDPOINTURL}: https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/ that you received as output

MIND: If you have set enableApiKeyAuth to true => SwaggerUI )

With Curl

  1. replace the url with the url provided by serverless (see above)
  2. replace the {API-KEY} with the key you get from serverless (see above)
  3. replace {route} at the end of the url e.g. with posts (default value)

Default Schema:

Default route is posts: (see db.json)
curl -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/posts

# or another route given in db.json file
curl -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}

# with enableApiKeyAuth=true
curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}

What`s my {route} ? -> see json-server documentation

Customization

Update content of db.json

  1. update local db.json file in root directory with new values
  2. re-deploy the stack via serverless framework

     sls deploy
  3. delete db.json file in S3 Bucket

  4. Make a GET request against the root url https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api
curl -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api

# with enableApiKeyAuth=true
curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}

=> With the next request a new db.json file will be created in the S3 Bucket

Change Stackname

edit service property in serverless.yml (in root directory)

Adapt settings in config/appconfig.yml file

AttributeDescriptionTypeDefault
readOnlyMake API readonly - all API - write operations are forbidden (http 403))stringfalse
enableSwaggerEnable swagger and swagger UI supportstringtrue 
enableApiKeyAuthMake your routes private by using an additional ApiKeybooleanfalse
jsonFilepath of json file that will be usedstringdb.json
enableJSONValidationvalidate JSON file at startbooleantrue

Used Packages

Components

Develop and debug locally

db.json file will be loaded directly from your local filesystem. No AWS access is needed.

Start solution

npm run start

Debug solution

If you want to debug locally in VS Code everything is already setup (using webpack with sourcemap support)

npm run debug

Test your API

With Swagger

Open the {ENDPOINTURL}: http://localhost:3000/ that you received as output

With Curl

  1. replace the url with the url provided by serverless (see above)
  2. replace the {API - KEY} with the key you get from serverless (see above)
  3. replace {route} at the end of the url e.g. with posts (default value)

Default Schema:

Default route is posts: (see db.json)
curl -H "Content-Type: application/json" http://localhost:3000/api/posts

#or another route given in db.json file
curl -H "Content-Type: application/json" http://localhost:3000/api/{route}

What`s my {route} ? -> see json-server documentation

Develop locally with cloud resources

Use same components (S3, LowDB) as the lambda does but have code executed locally.

1. Add .env file to root folder

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 Mind: This function requires that you have access to AWS (e.g. via credentials)

  • Copy the .env file from .env.sample in the root folder
cp .env.sample .env
  • Required: Adapt settings in .env file
AttributeDescriptionTypeDefault
S3FileJSON file used as db to read and write (will be created with a default json value - customize in db.json)stringdb.json
S3BucketS3-Bucket - this bucket must already exist in AWSstringjson-server-less-lambda-dev 
readOnlyall API - write operations are forbidden (http 403))booleanfalse

2. Start solution

npm run dev

3. Test your API

With Swagger

Open the {ENDPOINTURL}: http://localhost:3000/ that you received as output

With Curl

  1. replace the url with the url provided by serverless (see above)
  2. replace the {API - KEY} with the key you get from serverless (see above)
  3. replace {route} at the end of the url e.g. with posts (default value)

Default Schema:

Default route is posts: (see db.json)
curl -H "Content-Type: application/json" http://localhost:3000/api/posts

# or another route given in db.json file
curl -H "Content-Type: application/json" http://localhost:3000/api/{route}

# with enableApiKeyAuth=true
curl -H "x-api-key: {API-KEY}" -H "Content-Type: application/json" https://xxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/{route}

What`s my {route} ? -> see json-server documentation

Diagnose issues

serverless-offline will help you to troubleshoot issues with the lambda execution in a fast manner.

Mind: The assumption is that the solution has been already deployed Mind: This function requires that you have access to AWS (e.g. via credentials)

1. build sources and execute serverless offline

  • sources will be build with typescript (tsc) in advance to test the functionality.
  • after that sls offline will be started

2. make api calls

  • Use a new terminal window and start to make api calls.
  • Replace {API-KEY} with the api key in the sls offline output (see above).
  • Replace {route} with the route you want to test e.g. /posts

FAQ

How can I change the lambda region or stack name

Please have a look to the serverless guideline: https://serverless.com/framework/docs/providers/aws/guide/deploying/

Cannot use Swagger UI when enableApiKeyAuth is true

The apiKey is set in AWS API Gateway. This means all requests (even the standard route) need to use the API-KEY.

If you want to see the Swagger UI you need to add a plugin e.g. ModHeader to Chrome and add the needed headers:

  • Content-Type: application/json
  • x-api-key: {provided by sls info in the output after deployment}

ModHeader

I forgot the API-KEY I have set

Ensure you have credentials for AWS set.

sls info

Destroy the stack in the cloud

sls remove

I deployed the solution but I get back a http 500 error

Check Cloudwatch Logs in AWS - the issue should be describe there. Log has the same name as the stack that has been created.

1.6.16

3 years ago

1.6.15

3 years ago

1.6.14

3 years ago

1.6.12

3 years ago

1.6.11

3 years ago

1.6.7

4 years ago

1.6.6

4 years ago

1.6.5

4 years ago

1.6.4

4 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.52

4 years ago

1.5.50

4 years ago

1.5.51

4 years ago

1.5.49

4 years ago

1.5.48

4 years ago

1.5.47

4 years ago

1.5.46

4 years ago

1.5.45

4 years ago

1.5.44

4 years ago

1.5.43

4 years ago

1.5.42

4 years ago

1.5.41

4 years ago

1.5.39

4 years ago

1.5.40

4 years ago

1.5.37

4 years ago

1.5.36

4 years ago

1.5.35

4 years ago

1.5.34

4 years ago

1.5.33

4 years ago

1.5.32

4 years ago

1.5.31

4 years ago

1.5.30

4 years ago

1.5.28

4 years ago

1.5.27

4 years ago

1.5.26

4 years ago

1.5.25

4 years ago

1.5.24

4 years ago

1.5.23

4 years ago

1.5.22

4 years ago

1.5.21

4 years ago

1.5.18

4 years ago

1.5.17

4 years ago

1.5.19

4 years ago

1.5.20

4 years ago

1.5.16

4 years ago

1.5.15

4 years ago

1.5.14

4 years ago

1.5.13

4 years ago

1.5.12

4 years ago

1.5.11

4 years ago

1.5.10

4 years ago

1.5.9

4 years ago

1.5.7

4 years ago

1.5.6

4 years ago

0.1.1

4 years ago

0.0.6-alpha.28

4 years ago

0.0.6-alpha.25

4 years ago

0.0.6-alpha.24

4 years ago

0.0.6-alpha.23

4 years ago

0.0.6-alpha.22

4 years ago

0.0.6-alpha.20

4 years ago

0.0.6-alpha.18

4 years ago

0.0.6-alpha.17

4 years ago

0.0.6-alpha.9

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.2-alpha.1

4 years ago

0.0.1

4 years ago