This framework has been developed to provide a solid starting point for 'serverless' NodeJS projects. Big thanks to Postlight NY for inspiring this project with their Serverless Babel Starter.
Note: Currently, this starter kit specifically targets AWS.
Creating and deploying a new function takes two steps, which you can see in action with this repo's default Hello World function (if you're already familiar with Serverless, you're probably familiar with these steps).
In the functions section of ./serverless.yml, you have to add your new function like so:
# Declare REST endpoints, or event (SNS,SQS,S3 etc.) handlers here.
functions:
ping:
handler: src/ping.default
events:
- http:
path: ping
method: get
# Ping every 5 minutes to avoid cold starts
- schedule:
rate: rate(5 minutes)
enabled: trueThis is a single function declaration that creates a GET endpoint at /ping, which runs the default export from ./src/ping.ts.
Event - http creates the an AWS API Gateway endpoint.
Event - schedule pings the function once every 5 minutes in order to prevent cold starts.
This starter kit's Hello World function is a ping/pong (which you will of course get rid of). Found at ./src/ping.ts. There you can see a basic function that's intended to work in conjunction with API Gateway (i.e., it is web-accessible).
Like most Serverless functions, the ping function accepts an:
- event
Object - context
Object - callback
Function
When your function is completed, you execute the callback with your response. (If you've never used Serverless, have a read through their docs.
You can develop and test your lambda functions locally in a few different ways.
To run the hello function with the event data defined in fixtures/event.json (with live reloading), run:
yarn watch:helloTo spin up a local dev server that will more closely match the API Gateway endpoint/experience:
yarn serveWhen you add a new function to your serverless config, you don't need to also add it as a new entry
for Webpack. The serverless-webpack plugin allows us to follow a simple convention in our serverless.yml
file which is uses to automatically resolve your function handlers to the appropriate file:
functions:
ping:
handler: src/ping.defaultAs you can see, the path to the file with the function has to explicitly say where the handler
file is. (If your function weren't the default export of that file, you'd do something like:
src/ping.notDefault instead.)
Lambda functions will go "cold" if they haven't been invoked for a certain period of time (estimates vary, and AWS doesn't offer a clear answer). From the Serverless blog:
Cold start happens when you execute an inactive (cold) function for the first time. It occurs while your cloud provider provisions your selected runtime container and then runs your function. This process, referred to as cold start, will increase your execution time considerably.
A frequently running function won't have this problem, but you can keep your function running hot by scheduling a regular ping to your lambda function. Here's what that looks like in your serverless.yml:
functions:
ping:
handler: src/ping.default
events:
# Ping every 5 minutes to avoid cold starts
- schedule:
rate: rate(5 minutes)
enabled: trueOnce you have configured the ping in your serverless.yml file; You can use the runWarm HOF to wrap your function.
import runWarm from './utils'
const myFunc = (event, context, callback) => {
// Your function logic
}
export default runWarm(myFunc)Assuming you've already set up your default AWS credentials (or have set a different AWS profile via the profile field):
sls deploy --stage $STAGE --service $SERVICEAfter you've deployed, the output of the deploy script will give you the API endpoint for your deployed function(s), so you should be able to test the deployed API via that URL.