DEV Community

Cover image for Deploy a web application with AWS Lambda
AnanyaDasgupta
AnanyaDasgupta

Posted on • Updated on • Originally published at Medium

Deploy a web application with AWS Lambda

Lambda is the serverless computing solution provided by AWS.
According to official AWS documentation,

“AWS Lambda is a compute service that lets you run code without provisioning or managing servers.”

Lambda follows the principles of serverless computing. In serverless computing, you don’t have to worry about server and operating system maintenance, capacity provisioning, automatic scaling, and logging.

AWS Lambda takes care of the provisioning of the underlying infrastructure. It ensures the code is run on a high-availability computing infrastructure and administers the solution.
The developer only needs to provide the codebase in one of the languages supported by AWS Lambda.
Refer to this link for the languages supported by Lambda.

In this blog post, I’ll guide you through the process of creating a simple Node.js API and deploying it using the Serverless Framework on AWS Lambda.

Prerequisites

Before we dive into the development and deployment process, make sure you have the following tools installed:

  • Node.js and npm

  • AWS CLI

  • Serverless Framework

Setting Up the Project

  1. Initialize Node.js Project: Here, I am using VS Code to write the code. You can use any code editor of your choice.
  mkdir lambda-api
  cd lambda-api
  npm init -y
Enter fullscreen mode Exit fullscreen mode
  1. Install Dependencies:

    npm install express aws-serverless-express serverless -S

  2. Create a folder src in the root directory of your project. Navigate to the src directory and create a file named app.js

const express = require('express');
const awsServerlessExpress = require('aws-serverless-express');

const app = express();

app.get('/api/hello', (req, res) => {
  res.json({ message: 'Hello from your Lambda API!' });
});

module.exports.handler = (event, context) => {
   awsServerlessExpress.proxy(server, event, context);
};

Enter fullscreen mode Exit fullscreen mode

This is a simple API that prints the message “Hello from your Lambda API!” on the browser.

  1. Configure serverless.yml:

The serverless.yaml file is a configuration file used in serverless computing environments, particularly with the Serverless Framework.
The serverless.yaml file is used to define various aspects of your serverless application, such as functions, events, resources, and provider-specific settings.

service: lambda-api

provider:
  name: aws
  runtime: nodejs14.x

functions:
  app:
    handler: src/app.handler
    events:
      - http:
          path: /
          method: ANY
          cors: true
      - http:
          path: /{proxy+}
          method: ANY
          cors: true
Enter fullscreen mode Exit fullscreen mode

Here’s a general overview of theserverless.yaml file:

Service Information:

  • service: Specifies the name of your service.

  • provider: Defines the cloud provider (AWS, Azure, Google Cloud, etc.).

  • runtime: Specifies the runtime for your functions (Node.js, Python, Java, etc.).

service: lambda-api

provider:
  name: aws
  runtime: nodejs14.x
Enter fullscreen mode Exit fullscreen mode

Functions:

  • Describes the functions in your serverless application.

  • Specifies the handler (entry point) for each function.

functions:
      app:
        handler: src/app.handler
Enter fullscreen mode Exit fullscreen mode

Events:

  • Defines events that trigger your functions (HTTP events, S3 events, etc.).
functions:
      app:
        handler: src/app.handler
        events:
          - http:
              path: /
              method: ANY
              cors: true
          - http:
              path: /{proxy+}
              method: ANY
              cors: true
Enter fullscreen mode Exit fullscreen mode
  1. Configure AWS CLI:

Install the AWS CLI on your machine and configure it with your AWS Access Key ID, Secret Access Key, default region, and output format using the following command:

aws configure

Deploying to AWS Lambda

`npx serverless deploy --stage dev --region ap-south-1`
Enter fullscreen mode Exit fullscreen mode

This command deploys your Lambda function and provides an endpoint URL. The Serverless Framework automatically configures API Gateway as part of the deployment process.

API Gateway is a fully managed service by AWS that makes it easy to create, publish, maintain, monitor, and secure APIs at any scale. When you deploy a Serverless Framework project, it creates an API Gateway endpoint, and you can find the URL of this endpoint in the output of the deployment process.

Image for serverless deploy output

[https://ixnjy43dxk.execute-api.ap-south-1.amazonaws.com/dev/](https://ixnjy43dxk.execute-api.ap-south-1.amazonaws.com/dev/) is the API Gateway endpoint that was created for this Lambda function.
You can use the endpoint created for your function to access your API and test your deployed Lambda function. It’s the entry point for your serverless application from the internet.

Conclusion

You’ve successfully created a simple Node.js API and deployed it using the Serverless Framework on AWS Lambda. Serverless architecture provides a powerful and scalable solution for various applications, and with the right tools, the development and deployment process becomes seamless.

Feel free to explore additional features and integrations, and share your experience with the Serverless community. Happy coding!

Top comments (0)