This article teaches you how to create a Serverless RESTful API on AWS with the Serverless Framework. DynamoDB is used to store the data. The example’s source code is available on GitHub and can be used to speed up your project.
If you are interested in defining the API in OpenAPI Specification or Swagger Specification read Create a serverless RESTful API with API Gateway, Swagger, Lambda, and DynamoDB instead!
If you are interested in describing the API in CloudFormation read Create a serverless RESTful API with API Gateway, CloudFormation, Lambda, and DynamoDB instead!
API Gateway provides a HTTP API endpoint that is fully configurable. You define the HTTP resources (like
/user), the HTTP methods on that resources (like
DELETE, …) and the integration (e.g. Lambda function) that should be called to process the request. The Lambda function can then run whatever logic is needed to answer the request. API Gateway responds to the caller with the result of the Lambda function. The following figure demonstrates this flow.
Wiring together and deploying API Gateway and Lambda is time-consuming and error-prone. The Serverless Framework does all the heavy lifting for you. With just one command you get started.
The Serverless Framework is under active development by Serverless, Inc., which was recently funded by the investors behind Docker, New Relic and Meteor.
To setup a new project you first need to install the Serverless Framework:
Now it’s time to create a new project folder:
As a user of the framework you will get in touch with a YAML configuration file called
The first part of the configuration defines the runtime:
After that the interesting part starts: functions:
Let’s look at them line by line:
getUsers: Name of the function.
handler.js exporting the function
The magic happens inside the
events: section where API Gateway is defined as an event source. HTTP
GET requests on resource
/user are mapped to the
The implementation of the function in
handler.js is the same as with plain AWS Lambda.
With one command you can deploy your service.
This will package all the Source files, upload the zip file to S3 and create everything that is needed to do what you described in the configuration file. You can
serverless deploy multiple times to update your service.
To remove the deployed service you run:
Have a look at the following example to see the Serverless Framework in action.
clone the repository
install the Serverless Framework
switch to the
serverless-framework folder and install the dependencies
deploy the API
export the API endpoint from above in a environment variable to easily make calls the the API next.
create a user
create a task
mark task as complete
create a task with a category
list tasks by category
remove the API
The Serverless Framework makes deploying an serverless RESTful API very easy. You used a Lambda function to implement the functionality: each HTTP request invokes a Lambda function. You have very limited overhead to operate your API because you only need to configure your API and implement the functionality. You don’t need to care about servers, scaling, and all the operational overhead.
To automate the configuration of API Gateway you used CloudFormation.
The Serverless Framework also allows you to use SNS, SQS, S3, Scheduler, … as event sources.