DEV Community

Arnob
Arnob

Posted on

AWS Lambda Introduction & Create A Serverless Function

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.

serverless.com

Install a serverless package.

npm install -g serverless
Enter fullscreen mode Exit fullscreen mode

After installing the serverless. Run serverless from terminal

serverless
Enter fullscreen mode Exit fullscreen mode

output

captionless image

Now select a project type. Here i select AWS β€” Node.js β€” HTTP API . Here i project name aws-node-http-api-project .

captionless image

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

captionless image

The file list

captionless image

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
Enter fullscreen mode Exit fullscreen mode

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
    ),
  };
};
Enter fullscreen mode Exit fullscreen mode

So now deploy the serverless.

serverless deploy
Enter fullscreen mode Exit fullscreen mode

Here had a profile so use my profile for deploy.

serverless deploy --aws-profile arn
Enter fullscreen mode Exit fullscreen mode


captionless image

So visit the site using the link [https://n7bu0mlvah.execute-api.ap-southeast-1.amazonaws.com](https://n7bu0mlvah.execute-api.ap-southeast-1.amazonaws.com/)

captionless image

Lambda Function deployed.

Hope so you learn, how to deploy a lambda function by using serverless framework.

Happy Learning ….

Top comments (0)