It’s only been a few years since AWS Lambda, and it remains the most popular way to experiment with serverless technology, If you are not familiar with serverless, it’s a model of development in which managing, provisioning, and scaling servers is hypothetical away from application development, Servers do exist in a serverless world, but they are completely managed by the cloud provider, allowing developers to focus on packaging their code for deployment. As a leading software development company, we are explaining about what is Serverless APIs with AWS Lambda, with details.
AWS Lambda is a type of FaaS (Function-as-a-Service) offering that allows code execution on demand in response to pre-specified events or requests. This post will introduce you to AWS Lambda and guide you through creating and deploying Lambda functions with Node.js and AWS SAM (Serverless Application Model).
Before you proceed with this tutorial, ensure that you have Node.js 14.x LTS installed on your computer, as this is the latest release that AWS Lambda supports at the time of writing. However, the contents of this article should stay relevant even when newer releases are supported. Also, ensure you sign up for a free AWS account if you don’t have one already.
Install the AWS CLI and AWS SAM CLI
The exact way to install both CLI tools will differ depending on your operating system. You can install or upgrade to the latest version of AWS CLI for Linux.
●Here are the versions of AWS CLI and AWS SAM CLI that I installed while writing this guide:
Create Your First AWS Lambda Function with Node.js
Let’s start by writing a simple hello world function to demonstrate how AWS Lambda works. Run the command below to initialize a new project:
$ sam init –runtime nodejs15.x –name aws-lambda-nodejs-example
When prompted, choose AWS Quick Start Templates under template source, Zip under package type, and Hello World Example under application templates.
Once the command exits, change into the freshly minted aws-lambda-nodejs-example folder. It should have the following folder structure:
Here’s a short description of the important files and directories in the project:
● template.yaml: Defines the AWS resources for your application.
● template.yaml: hello-world/app.js: Contains the Lambda function logic.
● template.yaml: hello-world/package.json: Contains any Node.js dependencies required by the application.
● template.yaml: hello-world/tests/: Contains unit tests for your Lambda functions.
Open up the template.yaml file in your text editor and take note of the following lines:
These lines describe the name of your Lambda function (HelloWorldFunction), the runtime used to execute it (nodejs14.x), and the type of trigger for the function (Api). This indicates that your Lambda function will execute when a GET request is sent to the /hello route via API Gateway. Note that there are several other ways to invoke Lambda functions.
The CodeUri line indicates that the code for the HelloWorldFunction is in the hello-world directory. The Handler property specifies app.js as the file with the function code, which should have a named export called lambdaHandler.
Open up the hello-world/app.js file and examine its contents:
This simple function takes two parameters and returns a response object containing a ‘hello world’ message. The first parameter is the JSON payload sent by the invoker of the function, while the second is the context object which contains information about the function invocation and execution environment. This handler is async, so you can use return or throw to return a response or an error, respectively. Non-async handlers must use a third callback parameter.
Testing Your AWS Lambda Function Locally
Before deploying your function, you’ll want to test it locally to confirm it works as expected. To do so, run the following SAM command at the root of your project directory:
Once the application is running, make a GET request to http://localhost:3000/hello. This will cause AWS SAM to start a Docker container to run the function. Once the container is up and running, the function will execute and the following result will be returned:
Deploying the Lambda Function to the AWS Cloud
Once you are happy with how your function runs locally, you can deploy it to the AWS Cloud through the SAM CLI. First, run sam build to generate artifacts that target AWS Lambda’s execution environment:
Next, run sam deploy –guided to deploy the function
Once deployment is successful, you will see the API Gateway URL in the output. It should have the following structure: