How can I turn my Restful API into a Serverless application and deploy it to AWS Lambda Step by Step — Part I

Recently I had to do something similar in my current job and I would like to share my knowledge with you guys, let’s apply an old approach for solving problems that almost never fails “Divide and Conquer”, so in order to reach our final goal, let’s break down our big problem into a couple of small problems that are easier to overcome.

  1. Create a lambda function in AWS
  2. Create an API Gateway to redirect all the requests to your AWS Lambda Function
  3. Configure a VPC for your lambda function
  4. Grant internet access to the lambda function in a safety way
  5. Deploy the Node.js Restful API into your Lambda Function

If you want to learn more about amazon lambda functions, their advantages and pricing model you can go to https://aws.amazon.com/lambda/

In this tutorial I am going to explain how you can turn a Restful API built in Node.js + Express + Mongodb in a serverless application, ready to be deployed into AWS Lambda in a safety way through a virtual private network (VPC).

In order to complete this tutorial you will need at least satisfy the next prerequisites:

  • Has an active AWS Account with the corresponding permissions to work with AWS lambda and VPCs
  • Has a Rest API with at least one endpoint for testing

Let’s do it 💪.

1. Create your lambda function in AWS

Go to your AWS Management Console and look for the Lambda service. You will see a dashboard like this:

Image 1

Click on the create function button then you will see a screen like this:

Image 2
Image 3

In the Execution role field choose “Use an existing role” and in the Existing role field chose one role from the list that has the corresponding permissions.

Your AIM Role will need to have at least the AWSLambdaVPCAccessExecutionRole policy attached to be able to execute your Lambda Function inside of a VPC.

Complete the form fields and click on Create function button then your lambda function will appear in the list of functions.

2. Create an API Gateway to redirect all the requests to your AWS Lambda Function

In order to provide access to our Rest API developed in Node.js through our Lambda Function we are going to create an API Gateway using the Amazon API Gateway service. In your AWS Management console under the section Network & Content Delivery you will find the option API Gateway -> Click on it, you will see a screen where you can create a new API Gateway:

Image 4

Select the REST option, Select the New API option, complete the data under settings section. Then click on the Create API button and you’ll see a screen to configure your API Gateway recently created. In the left you’ll find a side panel, under the APIs section you’ll see the list of your API Gateways for the current Region you are in. Select My Api and under the sub-menu Resources you will find all the resources and methods that compose your API Gateway.

Image 5

Select the dropdown Actions, now you will see a list of different options that probably are confusing you.

A resource is an entity of your API Rest for instance /users, /teams/:id, etc. where you can configure different methods or other sub-resources to map your API Rest. You start with the root resource / at the top of the hierarchy.

A method is an abstraction that AWS uses to embodies a request and a response for each one of our Rest endpoints.

Select the Create Resource option. Now you’ll see the screen to create a resource.

Image 6

You must mark the checkbox — Configure as proxy resource.

You should mark the checkbox — Enable API Gateway CORS — This selection will set up a OPTIONS method with basic CORS configuration, allowing all origins, all methods and several common headers.

Then click on Create Resource button. Immediately you will see a second screen to configure how to resolve the requests sending through our proxy resource.

Image 7

Each endpoint must be resolved through a Lambda Function, HTTP URL, Mock, AWS Service or VPC Link. Think on it, you could map each endpoint of your API to an AWS Lambda function. But this isn’t our case, what we wanna do is just to create a special resource that works as a proxy and redirect all the requests sent to our API Gateway without cares which HTTP method or path is being used to our AWS Lambda function, this last one using our node.js application + aws-serverless-express integration finally will be who processes all the request sent through our API Gateway.

In the field Lambda Function type the name of your lambda function in this case is MyApi. Then select the Save button and confirm the dialog that appears.

Image 8
Image 9

This is the diagram you should see after created your resource as a proxy and granted access to execute your Lambda function. You can see how the requests sending to your proxy resource are redirect to your lambda function and receive the responses from the same one.

Our next step is to deploy the first version of our API Gateway and bind it to a stage.

An stage is like your environment where you APIs will be deployed. For example, a test version of your API could be deployed to a stage named beta.

You can try to create a stage in the section Stages but you will need an API deployment to be linked to your stage. In this case how we are just creating an API Gateway we don’t have a deployment option to select, many people is confused about it, even it happened to me 😅.

Image 10

That’s why we should first to deploy our API and the same time create a new stage. In order to do that select the Dropdown Actions and then Select the Deploy API Option.

Image 11
Image 12

In the dropdown Deployment Stage Select [New Stage]

Image 13

After you complete the form data click on deploy button. If everything goes well you will see your API gateway deployed ready to be used.

Image 14

The Invoke URL is the public url of your API Gateway. I know you want to click on it go ahead, probably you will get the next message:

This is usually related to requesting a URL that doesn’t exist. This happens because your Invoke URL is not mapped with any resource path.

Image 15

Now you can go to your Lambda function console and you will see how your Lambda Function is bound with your API Gateway.

Image 16

Here we are going to do our first checkpoint to test that everything is working right.

In the API GATEWAY section you see a clickable URL, go ahead and click on it.

If everything goes well you should see the message that is coded in the source code of your lambda function.

This is the source code of my lambda function:

exports.handler = async (event) => {

// TODO implement

const response = {

statusCode: 200,

body: JSON.stringify(‘Hello from Lambda!’),

};

return response;

};

This is what I got of the browser after clicked in the URL in the API Gateway Section.

Image 17

Until here we have complete the first half of this tutorial. I hope that everything have gone well for each one of you. Now we’ll move forward with the next section of this tutorial which is about run your lambda function into a VPC and grants it access to internet in a safety way that allows your lambda function to connect with third party services like Amazon RDS or mongodb Atlas.

The second part is here.

remember if you like this article don’t forget to share it in your social networks 😄