Deploy a web application with AWS Lambda

Ananya Dasgupta
3 min readJan 2, 2024

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

2. Install Dependencies:

npm install express aws-serverless-express serverless -S

3. 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);
};

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

4. 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

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

Functions:

  • Describes the functions in your serverless application.
  • Specifies the handler (entry point) for each function.
functions:
app:
handler: src/app.handler

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

5. 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

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.

`https://ixnjy43dxk.execute-api.ap-south-1.amazonaws.com/dev/ ` is the API Gateway endpoint that was created for your Lambda function.
You can use this endpoint 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!

--

--

Ananya Dasgupta
Ananya Dasgupta

Written by Ananya Dasgupta

Tech Enthusiast working as a DevOps Engineer

No responses yet