This example project shows how to deploy KoopJS application to AWS Lambda using the serverless framework.
requires Node.js 8+
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.
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.
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.
This example uses the serverless-offline to run the local dev server. Simply runs
npm run start
Deploy Lambda functions and APIs with one command:
npm run deploy
Remove all deployed Lambda functions and APIs:
npm run remove
MIT