Skip to content

An example project to use KoopJS with AWS Lambda + API Gateway

License

Notifications You must be signed in to change notification settings

koopjs/koop-serverless-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koop-serverless-example

This example project shows how to deploy KoopJS application to AWS Lambda using the serverless framework.

requires Node.js 8+

Prerequisite

AWS access key with privileges to:

  • read/write S3
  • manage Lambda
  • manage API Gateway

The serverless framework uses the key to deploy and manage Lambda functions on AWS. To know how to setup the credential for serverless, take a look at this guide.

Code

Koop app

This example uses the serverless-http library to wrap an existing Koop app into AWS Lambda.

You should be able to develop your Koop app much like the usual way. But instead of running directly, the app needs to be exported for serverless:

// src/index.js

const Koop = require("koop");
const serverless = require("serverless-http");

// initiate a Koop app
const koop = new Koop();

// configure the Koop app

// wrap the Koop server with the serverless framework
module.exports.handler = serverless(koop.server);

Since the Koop app is going to run as a Lambda function, you may need to reconsider the caching and data persistence strategies.

serverless.yml

The Lambda function is configured with the file servless.yml. The configuration shows how to configure a lambda function for API Gateway events.

service: koop-serverless-example

provider:
  name: aws
  runtime: nodejs10.x

functions:
  # Each API should have one corresponding function
  get-data:
    handler: src/index.handler
    events:
      # The "http" event defines an API at the API Gateway
      - http:
          path: /my-provider/{host}/FeatureServer/0
          method: get
          request:
            # Each parameter and query string need to be explicitly specified
            parameters:
              paths:
                host: true

For more details and options, check the Serverless AWS Guide.

Development

Test

This example uses the serverless-offline to run the local dev server. Simply runs

npm run start

Deploy

Deploy Lambda functions and APIs with one command:

npm run deploy

Remove

Remove all deployed Lambda functions and APIs:

npm run remove

Lincese

MIT

About

An example project to use KoopJS with AWS Lambda + API Gateway

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages