Deploying a Serverless Function with AWS Lambda

mariehposa

Mariam Adedeji

Posted on September 29, 2024

Deploying a Serverless Function with AWS Lambda

In this tutorial, you will learn how to create a simple AWS Lambda function using JavaScript to handle HTTP requests.

First off, what is Serveless?

Serverless computing allows you to run code without provisioning or managing servers. It basically allows you to focus on coding and forget about infrastructure. AWS Lambda is one of the most popular services for running serverless functions.

Let’s start by highlighting all the steps needed and then explaining them in detail below:

  1. Set up AWS Lambda function
  2. Write a JavaScript function
  3. Set Up an API Gateway Trigger
  4. Test Serverless Function
  5. (Optional) Monitor and Scale

Prerequisites

  1. AWS Account – You’ll need an AWS account to access the AWS Lambda service. If you don’t have one, sign up here.
  2. A Basic Understanding of JavaScript – We'll be writing a simple JS function.
  3. Some Coffee ☕ – To keep things fun, of course!

Step 1 — Set Up AWS Lambda

1) Login to AWS Console:
Go to the AWS Management Console and sign in.

2) Navigate to Lambda:

  • In the search bar, type "Lambda" and select Lambda.
  • Click Create function.

3) Create a New Lambda Function:

  • Select Author from scratch.
  • For the Function name, enter myServerlessFunction.
  • For the Runtime, choose Node.js 20.x (for JavaScript support).
  • Leave other options as default.
  • Click Create Function.

Set up AWS Lambda

You should now have a basic Lambda function ready to be customized!

Set up AWS Lambda

Step 2 - Write Your JavaScript Function

By default, AWS Lambda creates a template function. Let’s modify it to respond to HTTP requests.

1) Edit the Code:
In the Code tab, you’ll see a file named index.mjs. Replace the code with the following:

export const handler = async (event) => {
    const name = event.queryStringParameters?.name || 'World';

    const response = {
        statusCode: 200,
        body: JSON.stringify({
            message: `Hello, ${name}! Welcome to Serverless.`,
        }),
    };

    return response;
};
Enter fullscreen mode Exit fullscreen mode

What this does:

  • Event object: This contains request data. Here, we are reading query parameters (e.g., ?name=John).
  • Response: It returns a 200 status with a JSON body containing a greeting message.

2) Deploy the Function: Click Deploy to save and publish your function changes.

Deploy the Function

Step 3 - Set Up an API Gateway Trigger

To make your Lambda function accessible over HTTP, we’ll use AWS API Gateway.

1) Add API Gateway Trigger:

  • Scroll to the Function 0verview section and click Add trigger.
  • From the dropdown, select API Gateway.
  • Choose Create a new API.
  • For API Type, select HTTP API.
  • For Security, choose Open.
  • Leave the defaults for Additional settings and click Add.

Add API Gateway Trigger

2) Get the API URL: After adding the trigger, you will see the API Gateway URL. Copy this URL, you will use it to call your function.

Step 4 - Test Your Serverless Function

With everything set up, let’s test your function.

1) Open a Web Browser or Postman: Use the API Gateway URL you copied earlier. Test the function by appending the name query parameter to the URL:

https://your-api-url?name=John
Enter fullscreen mode Exit fullscreen mode

2) Result: If everything is working, you should get a response like:

Result with query

If you omit the name query parameter, you should get the default:

Result without query

Step 5 - (Optional) Monitor and Scale

AWS Lambda automatically scales based on the number of requests. You don’t need to worry about managing infrastructure, but you can monitor your function’s performance.

Go to the Monitoring tab: Inside your Lambda function dashboard, you’ll see metrics such as invocation count, duration, errors, etc.

Monitoring tab

Yay! That’s it! You can extend this function to do practically anything, like querying databases, processing files, or integrating with other AWS services.

If you’ve found this article helpful, please leave a like or a comment. If you have any questions or constructive feedback, please let me know in the comment section.

💖 💪 🙅 🚩
mariehposa
Mariam Adedeji

Posted on September 29, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related