Deploying a Simple Serverless Node.js Application on AWS Lambda Functions

Written by nuralem | Published 2023/05/31
Tech Story Tags: aws | aws-lambda | serverless | nodejs | backend | javascript | nodejs-apps | tutorial

TLDRThis tutorial is designed for beginners in software development. We will cover deploying a Serverless Node.js application on Amazon Web Services (AWS) One of these services is AWS Lambda, a serverless computing service that runs your code in response to events. In this article, we will discuss how to set up your AWS account and create a simple app.via the TL;DR App

Welcome to this comprehensive guide, where we will cover deploying a Serverless Node.js application on Amazon Web Services (AWS), one of the most popular cloud service providers globally. This tutorial is designed for beginners in software development, so we will take things slow and explain concepts in detail.

Before we dive in, it's important to understand what we're working with.

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, which lets developers create scalable network applications. AWS, on the other hand, offers a suite of cloud services that allow you to host and scale your applications. One of these services is AWS Lambda, a serverless computing service that runs your code in response to events.

In this article, we will discuss:

  1. Prerequisites
  2. Setting up your AWS account
  3. Creating a simple Node.js application
  4. Installing and configuring the Serverless Framework
  5. Deploying your application on AWS using the Serverless Framework

Prerequisites

  1. A basic understanding of JavaScript and Node.js.
  2. Node.js and npm (Node Package Manager) are installed on your local machine. You can download them from here.
  3. An AWS account. If you don't have one, you can create it from here.

Setting up your AWS account

After setting up your AWS account, you need to create a new user in AWS Identity and Access Management (IAM):

  1. Login to your AWS Management Console.
  2. Navigate to IAM, and then click on "Users." (Image 1)

  1. Click on "Add users" (Image 2).

  1. Enter a username and press the next button.
  2. Set permissions by attaching existing policies directly. For simplicity, use "AdministratorAccess" In a production environment, it's advised to limit the permissions, but for this guide, we will give admin access to our account (Image 3). Click Next.

  1. Review the details and click on "Create user."After the user is created, you should click on the name of our account and go to “Security credentials“ and click on “Create access key“ (Image 4).

  1. Select “Third-party-service“ and accept Alternative recommended options. Setup description and press “Create access key“. You'll be shown an access key ID and a secret access key. Make sure to store them safely, as you won't be able to view the secret access key again.

Creating a Simple Node.js Application

How to create a simple app, I have described in my previous articles How to Deploy a Serverless Node.js Application on Azure and Deploying a Serverless Node.js Application on Google Cloud.

Firstly, it's important to note that AWS Lambda functions do not support listening on HTTP ports like a regular Node.js application might. Instead, AWS Lambda functions are event-driven, meaning they respond to events like HTTP requests through the API Gateway.

Secondly, AWS Lambda functions should export a handler function that takes an event object, context object, and a callback as parameters. This function is what AWS Lambda will call when your function is invoked.

Therefore, your original code should be modified to fit this model. Here's the modified app.js code:

exports.handler = async (event, context) => {
    const response = {
        statusCode: 200,
        headers: {
            'Content-Type': 'text/plain',
        },
        body: 'Hello World\n',
    };
    return response;
};

A package.js will be:

{
  "name": "hackernoon",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Save all these files to your project folder and you are ready to go.

Installing and Configuring the Serverless Framework

The Serverless Framework helps you develop and deploy AWS Lambda functions. It's a CLI tool that needs to be installed globally.

  1. Install the Serverless Framework:

    npm install -g serverless
    

  2. Setup your AWS credentials with the Serverless Framework:

    serverless config credentials --provider aws --key <your_key> --secret <your_secret>
    

Replace <your_key> and <your_secret> with the access key ID and secret access key you got when creating the IAM user.

  1. In your project directory, create a new file named serverless.yml.

  2. Open serverless.yml and add the following configuration:

    service: hackernoon-test
    
    provider:
      name: aws
      runtime: nodejs18.x
    
    functions:
      hello:
        handler: app.handler
        events:
          - http:
              path: hello
              method: get
    

This configuration defines a new service named hackernoon-test. It specifies that we're using AWS as our provider and Node.js 18.x as our runtime. It also defines a function named hello that will execute our handler function in app.js.

Deploying your Application on AWS

Now that we've set up our application and the Serverless Framework, we can deploy our application:

  1. Deploy the service:

    serverless deploy
    

  2. Open your browser and navigate to your fresh link:

    Deploying hackernoon-test to stage dev (us-east-1)
    
    ✔ Service deployed to stack hackernoon-test-dev (58s)
    
    endpoint: GET - https://37c01fcgfj.execute-api.us-east-1.amazonaws.com/dev/hello                     
    functions:
      hello: hackernoon-test-dev-hello (531 B)                      
    

And there you have it! You've just deployed your first Serverless Node.js application on AWS. As you can see, the Serverless Framework simplifies the process, letting you focus on writing your application.

Conclusion

In this guide, we've covered the basics of deploying a Serverless Node.js application on AWS. We've only scratched the surface of what's possible with AWS and the Serverless Framework, and there's much more to explore. Remember, the best way to learn is by doing, so don't be afraid to experiment and build your own applications.

Happy coding!


Written by nuralem | Software engineer
Published by HackerNoon on 2023/05/31