We will create a straightforward serverless API that responds with “Hello, Serverless!”. This involves writing a basic AWS Lambda function and setting up AWS API Gateway to trigger the Lambda function when a request is made to a specific URL in a web browser.
Serverless computing is a cloud-computing execution model where the cloud provider dynamically manages the allocation and provisioning of servers. In a serverless setup, developers write code in the form of functions, and the cloud provider runs these functions in response to events.
It lets you run code without provisioning or managing servers. You only pay for the compute time you consume.
AWS API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs.
Before getting started, you’ll need:
aws configure
and provide your AWS Access Key, Secret Key, region, and output format.serverless-offline
plugin so we can run it later locallynpm install -g serverless serverless-offline
serverless create --template aws-nodejs --path my-service
cd my-service
npm init -y
npm install --save aws-sdk
Open the handler.js
file and write a simple function:
'use strict';
module.exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Hello, Serverless!',
input: event,
},
null,
2
),
};
};
Open serverless.yml
and define your service configuration:
service: my-service
provider:
name: aws
runtime: nodejs18.x
region: us-east-1
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get
plugins:
- serverless-offline
serverless offline --httpPort 8080
Open your browser or use a tool like Postman to make a GET request to your endpoint URL (http://localhost:8080/hello). You should receive a response with the message “Hello, Serverless!”.
serverless deploy
AWS Serverless offers a powerful and scalable way to build modern applications without worrying about infrastructure management. By leveraging services like AWS Lambda and API Gateway, you can quickly deploy and manage applications with ease
Thankyou for reading, Happy Coding!
© 2024 Khaalis Technologies. All Rights Reserved. Designed by HTML Codex