Codementor Events

Getting started with Serverless framework + AWS Lambda and API Gateway

Published Jan 29, 2020
Getting started with Serverless framework + AWS Lambda and API Gateway

Requirements

NodeJS and NPM

Let’s start

First, you need to install serverless framework globally on your computer:

npm install -g serverless

then

serverles version

You should see the version:

1*KC8XEEQ7mWDUDm1EyHbJKA.png

Now, we are ready to create our first function. The below command will create a new service based on a template.

serverless create --template aws-nodejs --path hello-serverless

You should see something like:

1*okyaEUc8F-4g-6G_1TjK8g.png

Go to the folder by typing

cd hello-serverless

And then open the project with your favorite code editor. The template has two important files. The first one is the handler.js, which is executed when the lambda function is triggered and in this case just returns a status code 200 and a message as body response

1*Dp7-M_V8ZA5YljYdSMX2DA.png

The second one is the serverless.yml file and is magic. It contains all the needed configuration to deploy our API to AWS (or azure or google cloud, etc). It has a lot of comments, but after delete them, the file looks like:

1*d3soqYv_1Da-Jg6YF385wg.png

This file allows us to define a lot of configurations but now, we will just to define our API functions. Each function has a name, a handler (a method that will be triggered when your function is invoked), and events. An event could be anything that is capable to trigger the function. AWS provides events like watchers and others, but we will focus on an HTTP event, so when a specified path is requested, this function will be thrown.

Let’s define an event:

1*yXDHpSM8_yfIt69Bm5q-Fg.png

As you can see, we could define a path name (hello in this sample) to invoke this function, and it will be invoked by using the HTTP GET method.

That’s all, let’s run it:

serverless invoke local --function hello

It will show your function response:

1*_-7oIicNFDH42V7kmH8GYw.png

Awesome! No?

But what happens if you want to create an API with a lot of routes and need to debug your code and executing multiple requests through your browser (or POSTMAN). So you will need a more powerful tool.

To do this, there is plugin capable to simulate an Amazon API Gateway, it is serverless-offline and help us to run aour API locally.

So, let’s install it:

npm init

Set a project name, etc or just leave it as default by pressing enter, enter enter….

npm install serverless-offline --save-dev

Before executing our API locally, we need to specify that we are using this plugin, so add the following in the serverless.yml as the below image shows:

1*s8za6s9yQLLumiQYyE6TRw.png

Now, you are able to run your API:

serverless -- offline

1*-_JPMW0zEJZX3xIeIMpvgQ.png

If you make a request to the hello path from your browser, you should be the following:

1*buQorzLIObm_wKZpiIyVMA.png

You can find the complete project in my GitHub repo.

That’s all for this article. In the next one, I will show you how to add credentials to your project and then deploy it to AWS.

Thansk for reading!!

Discover and read more posts from Wilfredo Alcala
get started