Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Creating Serverless Microservices With AWS Lambda and Node.js

DZone 's Guide to

Creating Serverless Microservices With AWS Lambda and Node.js

Learn about the advantages of microservices for serverless and how to set up microservices with AWS Lambda.

· Microservices Zone ·
Free Resource

In serverless architecture, the server will not run all the time. The server will be active only when a request is there for processing, and the rest of the time, the server will be in sleep mode. Unlike server architecture, a monthly subscription plan need not be taken, as the user only has to pay for the server's active time, which helps to reduce the server cost.

Image titleMicroservice architecture is a method of developing a system in small modules which communicate internally with each other through a lightweight mechanism like HTTP/REST with JSON.

Advantages of Microservice Architecture

  • Each module of the application can be developed, tweaked, and redeployed independently.

  • Different modules of the application can be built with different technologies.

  • Each module usually manages its unique databases, so decentralized data management can be done easily.

Reasons to Use Microservice Architecture

In monolithic applications, the server side handles all the application's HTTP requests, which execute logic and do CRUD operations on the database, so if one component needs to scale, the whole application needs to scale. This leads us to the reason for using microservice architecture.

Below are the steps for creating an AWS account and AWS Lambda function with a REST API in AWS Cloud with minimal code.

Creating an AWS Account

Go to https://aws.amazon.com/ and click on "Create a Free Account."

Now you have to enter your email address and select the “I am a new user” option, then click on "Sign in" using our secure server.

After that, you need to enter your full name, retype your email address, and set the password for your account, then click on "Create Account."

In the Contact Information tab, select "Personal Account" and fill in your personal details, then click on "Create Account" and "Continue."

Now you have to enter your bank details for payment and click on "Verify Card" and "Continue."

In the "Identity Verification" tab, you need to verify your mobile number by entering a Captcha and your mobile number with the extension. Once your number is verified, click on "Continue" to select your Support Plan.

Select your plan according to your need and click "Continue."

This will create your AWS account. For Sign-In click on Sign-In console and enter your email address, enter your password, and click on "Sign in using the secure server."

Creating an AWS Lambda Function

After you sign in, go to the "Services" tab and search for "Lambda," then select "Lambda."


Click on "Get started now."

                                                                                                                                                                     

On the "Select blueprint" page, select "runtime" from the dropdown and select your blueprint accordingly. I am selecting Node.js 6.10 and Blank Function as my blueprint.


After that, you need to select how you will call the Lambda function by clicking on the blank square box, or you can skip this by clicking on "next." I am skipping this step.

In the "configure function" tab, enter the function name, description, and environment in which you want to run and change the function code in the Lambda function code tab. I have created a Lambda function which will reverse a string, and written logic it, in the Lambda function code tab.

Scroll Down and set the role for the function. Select "Create new role" from the template(s) and enter your role name.

Click on the "Advanced" tab; here, memory for the function is set for 128mb and timeout for the running function is set for 3s, which I think is enough. Click on "Next."

After that, review the function, then click on "Create function."

You can test your Lambda function by clicking on "Test."

You can see your function output in the execution results. I have created function of a reverse string of “Hello from Lambda” so you can see my result.

Creating the API for a Lambda Function

Go to the "service" tab and search for "API Gateway," then click on "API Gateway." Click on "Get Started."

Click on New API and enter the API's name and description, then click on "Create API."

I am creating an API whose request type is "Get," so click on "Actions" and then "Create Method," in which you have to select GET as the method.


Click on the right button below "GET." Then select the Lambda region, enter your Lambda function name, and click "Save."

To create the API, you need to add permission, so click on "OK."

In the GET Method Execution Screen, click on "Integration Request."

You can check whether your Lambda function is selected or not.

Expand the "Body Mapping Templates" tab and select “When there are no templates defined (recommended)” and enter the Content Type as “application/json” by clicking on "Add mapping template" and clicking the right button.

Enter your JSON object in a General template, which helps you to pass input in headers, then click "Save."

Now the API is ready; to deploy the API, click on the Actions menu and click "Deploy API."

Select "New Stage" for the deployment stage and enter the stage name. You can also enter a production description and deployment description — then click on "Deploy."

You can call an API by clicking on "Invoke URL."

To use the input of the API in your Lambda function, you can get a value from the event object. I am here modifying my Lambda function by going into the Lambda tab and selecting your function name, which you want to edit. Once you edited the code, click on "Save and Test."

Now you can use an API by URL followed by parameters in the query string.

 

Topics:
aws ,serverless ,microservices ,node.js ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}