Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 17, 2024 05:59 am GMT

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 dont 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, Ill 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
  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);};

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-apiprovider:  name: aws  runtime: nodejs14.xfunctions:  app:    handler: src/app.handler    events:      - http:          path: /          method: ANY          cors: true      - http:          path: /{proxy+}          method: ANY          cors: true

Heres 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-apiprovider:  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
  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`

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. Its the entry point for your serverless application from the internet.

Conclusion

Youve 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!


Original Link: https://dev.to/ananyadasgupta/deploy-a-web-application-with-aws-lambda-2km3

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To