Create API with AWS API Gateway and Serverless Framework

Serverless is framework is one of the most widely adopted frameworks for build serverless applications, you can build great solutions for Azure, AWS or Google Cloud, in my case, I will use AWS.

To start is so easy, first, we install the serverless node package and configure it with AWS.

npm install -g serverless

Configure Serverless Framework with AWS

You need to get API key and secret from AWS dashboard go to users and go to tab Security and credentials and click in Create keys.

Copy the key and secret and modify the next serverless command with your information and it will store your settings for the project.

serverless config credentials --provider aws --key yourkey --secret secretaccesskey
Serverless: Setting up AWS...
Serverless: Saving your AWS profile in "~/.aws/credentials"...
Serverless: Success! Your AWS access keys were stored under the "default" profile.

Using Serverless CLI

Create a directory simple app and from our terminal go to it and run the next command, It will generate 2 files serverless.yml and handler.js

sls create --template aws-nodejs --name nba

Serverless.yml it is the file contains the configuration of our serverless to set service name, provider, resource and lambdas.

Our next step is rename name the service and function to the NBA, including events of HTTP and set the path, type of HTTP method.

service: nba 
name: aws
runtime: nodejs8.10
region: us-east-1
handler: handler.players
- http:
path: players/list
method: get

Handler.js it contains the code for our code to be executed, in short words Lambda and API Gateway work together to simplify the API Gateway allow to access to our lambda code, so we can update our lambda code without touch our API Gateway.

We edit our handler.js and return a list of NBA players.

module.exports.players = async event => {
return {
statusCode: 200,
body: JSON.stringify(
message: playerList()
const playerList = () => {
return [
{ name: "Lebron", number: 23 },
{ name: "Irving", number: 11 },
{ name: "Lillard", number: 2 }

From our terminal, write sls deploy command and it will deploy and return a url and more information about our API.

anys-imac:nbaAPI dany$ sls deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service file to S3 (353 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
Serverless: Stack update finished...
Service Information
service: nba
stage: dev
region: us-east-1
stack: nba-dev
resources: 11
api keys:
hello: nba-dev-hello

That’s it!

Hopefully, that will give you a bit of a head-start on Serverless, and help you avoid some of the more common mistakes. If you enjoyed this post, share it.

Thanks for reading!