Skip to main content

Amazon Web Services

AWS Serverless Infrastructure & CRUD Operations using NodeJS

Istock 1194430859

A serverless architecture is a way to build and run applications and services without having to manage infrastructure. Your application still runs on servers, but all the server management is done by AWS. Serverless framework is a toolkit for creating, deployment of serverless functions like AWS Lambda, publish the message in SNS, send message to SQS, store message into DynamoDB etc. This can be used across multiple cloud providers. Not limited to AWS. AWS handles all the back-end tasks like computing, databases, storage, processing and many more, this in result, allows the user to focus on his program.

Node.js is an open source, cross-platform runtime environment for developing server-side and networking applications. Node.js applications are written in JavaScript and can be run within the Node.js runtime on OSX, Microsoft Windows, and Linux.

Prerequisite:

AWS Serverless Infrastructure Workflow:

Serverless workflow is a fully managed cloud service that coordinates distributed tasks. In Serverless workflow, you can orchestrate distributed tasks in sequence, in branches, or in parallel. Based on the defined steps, Serverless workflow reliably coordinates task executions, tracks the state transition of each task, and executes the user-defined retry logic when necessary to ensure the smooth completion of our flow.

Architecture

Configuration:

  • The Serverless framework is easy to install. First, you need a Node.js12.x runtime version which is supported by AWS Lambda. You’ll also want to ensure your local environment is as close to the production environment as possible. Now install Node.js 12.x runtime or switch between versions of Node.js:

$ nvm install v12.x

  • Now that you have Node.js runtime, proceed to install the Serverless framework CLI globally:

$ npm install -g serverless

  • Check that the installed version of Serverless framework:

$ serverless –version

  • Create a serveless template with nodeJS

$ serverless create –template serverless-aws-infrastructure

By default the following files are generated using the serverless template command.

├── handler.js

└── serverless.yml

Amazon Web Services - Avoid Contact Center Outages: Plan Your Upgrade to Amazon Connect
Avoid Contact Center Outages: Plan Your Upgrade to Amazon Connect

Learn the six most common pitfalls when upgrading your contact center, and how Amazon Connect can help you avoid them.

Get the Guide

Now, handler.js is the Lambda function which contain some boilerplate code or your business logic. This is where our logic stays. The    file serverless.yml contains your API defination and other resources.

Folder Structure

  • Let’s make some modifications to serverless.yml. We’ll change the runtime property to nodejs12.x. Then, we’ll add a new property, region, to the provider object which will deploy the application to the region that we specify in the serverless.yml. Also add the staging deployment like test, dev or prod etc.

Serverless Yml

  • Now the application is ready to deploy:

$ serverless deploy –stage dev –region us-east-1

The Serverless Framework will now wrap everything up into a single bundle & create a CloudFormation file from the serverless.yml and  put it off to AWS S3 bucket. Once the resources are created, and the code is deployed, you’ll see the output in your console/terminal.    The important thing is to note down the endpoints.

Deployment Console

  • We can remove the deployment using the following command:

$ serverless remove –stage dev –region us-east-1

Advantages of serverless applications:

  • Serverless-based applications are scaled on-demand based on the number of resources required to handle requests serving, so there’s no need to fear crashing when you get sudden surges of traffic
  • Concurrent requests are triggered in new container instances
  • Security updates or patches are handled for us
  • You can write code, and when you’re finished with everything, you can straightaway deploy the system, and it’ll be available to the world within a few minutes. Thus, no need to put any effort into or for creating and managing servers.
  • Faster deployment cycle run via a single command, “serverless deploy”
  • Serverless offers an abstraction for cloud infrastructure
  • You can choose appropriate settings as per your products requirement, thus paying only for the functions you use.

How to test endpoints for CRUD operation:

  • Configure authorization: -we need to configure authorization in Postman with the AWS account Client ID & Client Secret

Credential Setup

  • Configure POST API to send message into DynamoDB

Post Api

  • Configure GET API to retrieve messages from DynamoDB

Get Api

Dynamodb View

Summary:

To establish the AWS infrastructure and perform CRUD operations, we have created a NodeJS project using the serverless framework.

Simple instructions are given for installing NodeJS and NPM locally, as well as the simplest approach to set up serverless framework using commands.

After we’ve completed the configuration setup, we can use the command to construct a NodeJS template, and then we can add our business logic to AWS Lambda.

To build AWS resources and conduct back-end functions including computation, databases, storage, processing, scalability, and monitoring, use the serverless.yml file.

Once we have the yml file and logical code ready, we can use the command to deploy it to AWS, which will generate the CloudFormation stack and resources.

As a result, it provides us with the terminal’s endpoints. The Postman API Platform can be set up to run CRUD tasks using the same APIs.

Follow these simple steps to get started with AWS serverless and perform CRUD operations.

Thoughts on “AWS Serverless Infrastructure & CRUD Operations using NodeJS”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Shishir Meshram

Shishir Meshram is a Senior Technical Consultant at Perficient Inc. With over 7 years of expertise in Java and AWS Cloud Development. He is an AWS Certified Developer-Associate. He enjoys exploring new adventurous places and is a traveler.

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram