DEV Community

Atul Vishwakarma
Atul Vishwakarma

Posted on • Originally published at atulcodes.hashnode.dev

Deploying Your First Serverless Function on AWS Lambda

For a long time, deploying an application meant provisioning a server, installing an operating system, configuring a runtime environment, and paying for that server to run 24/7β€”even if nobody was visiting your site.

Then came Serverless.

Serverless doesn't mean there are no servers. It simply means you don't have to manage them. With AWS Lambda, you just write your code, upload it, and AWS handles the rest. It automatically scales based on traffic, and the best part? You only pay for the exact milliseconds your code is running.

(If you followed my previous tutorial on Setting Up AWS Billing Alerts, you already know how to keep your costs under control!)

Today, we are going to build your first serverless API endpoint using AWS Lambda and API Gateway. Let's dive in.

Step 1: Create the Lambda Function

Log into your AWS Management Console and search for Lambda in the top search bar.

Once you are on the Lambda dashboard, click the bright orange Create function button.

Configure your function with the following settings:

  • Author from scratch: Leave this selected.

  • Function name: MyFirstServerlessAPI

  • Runtime: Select Node.js 24.x (or the latest available Node.js version).

  • Architecture: x86_64

Scroll down to the bottom and click Create function.

Step 2: Write the Serverless Code

You will be redirected to your new function's dashboard. Scroll down to the Code source section. You will see an integrated code editor with a file named index.mjs.

AWS provides a basic "Hello from Lambda" template, but let's update it so it behaves like a real API endpoint that returns JSON data.

Replace the default code with this:

export const handler = async (event) => {
  // We can log the incoming request for debugging
  console.log("Incoming event: ", JSON.stringify(event, null, 2));

  // Create our JSON payload
  const responseBody = {
    message: "Success! You just built your first Serverless API.",
    timestamp: new Date().toISOString(),
    yourRole: "Cloud Engineer in the making"
  };

  // Return the HTTP response
  const response = {
    statusCode: 200,
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify(responseBody),
  };

  return response;
};

Enter fullscreen mode Exit fullscreen mode

After pasting the code, click the Deploy button just above the code editor. If you don't click deploy, AWS won't save your changes!

Step 3: Add API Gateway

Right now, your code exists in the cloud, but there is no way for the outside world to communicate with it. We need to attach a "trigger." In this case, we want an HTTP URL that triggers our code whenever someone visits it.

To do this, we use Amazon API Gateway.

Scroll up to the top of your function dashboard and look at the Function overview diagram. Click on the + Add trigger button.

Configure the trigger with these settings:

  • Select a source: Search for and select API Gateway.

  • Intent: Select Create a new API.

  • API type: Select HTTP API (this is cheaper and simpler than REST API).

  • Security: Select Open (this makes the endpoint public so we can test it in our browser).

Click Add.

Step 4: Test Your Live API Endpoint

AWS will take a few seconds to configure the API Gateway and attach it to your Lambda function.

Once it is done, you will see API Gateway listed in your Function overview. Scroll down to the Configuration tab, and click on Triggers.

You will see an API endpoint URL that looks something like this: https://2eb2yh0j28.execute-api.us-east-1.amazonaws.com/default/MyFirstServerlessAPI

Click that link!

It will open a new tab in your browser, and you should immediately see your formatted JSON response:

{
  "message": "Success! You just built your first Serverless API.",
  "timestamp": "2026-06-09T14:18:40.430Z",
  "yourRole": "Cloud Engineer in the making"
}

Enter fullscreen mode Exit fullscreen mode

The Takeaway

Congratulations! You just deployed a scalable, highly available microservice.

Think about what you didn't have to do today: You didn't configure a VPC, you didn't open ports in a security group, and you didn't install Node.js on a Linux server. AWS handled the entire underlying infrastructure, allowing you to focus entirely on writing business logic.

That is the true power of Serverless.


If this tutorial helped you deploy your first Lambda function, consider β˜• buying me a coffee to support my work on Atul Codes!

Top comments (0)