What is Lambda
AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. It is a computing service that runs code in response to events and automatically manages the computing resources required by that code.
How it works
AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers. You can trigger Lambda from over 200 AWS services and software as a service (SaaS) applications, and only pay for what you use.
I share those infromation from AWS Lambd 😁
Create a Lambda Function
Here i use Serverless for Lambda function. By using Serverless make easier to create and deploy a serverless function.
Install a serverless
package.
npm install -g serverless
After installing the serverless. Run serverless
from terminal
serverless
output
Now select a project type. Here i select AWS — Node.js — HTTP API
. Here i project name aws-node-http-api-project
.
Here i don’t need to serverless dashboard because we not need the serverless dashboard so we set to No. And we not need to deploy it also selected No.
Now open the project code at Visual Studio Code
The file list
At serverless.yaml there region is not given. Better you have to define the region. If you not give any region then it default region us-east-1
will be deployed.
service: aws-node-http-api-project
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs18.x
region: ap-southeast-1 # Region is Singapore
functions:
api:
handler: index.handler
events:
- httpApi:
path: /
method: get
The Index.js is like a hello World 😁
module.exports.handler = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: "Go Serverless v3.0! Your function executed successfully!",
input: event,
},
null,
2
),
};
};
So now deploy the serverless.
serverless deploy
Here had a profile so use my profile for deploy.
serverless deploy --aws-profile arn
So visit the site using the link https://n7bu0mlvah.execute-api.ap-southeast-1.amazonaws.com
Lambda Function deployed.
Hope so you learn, how to deploy a lambda function by using serverless framework.
Happy Learning ….