How I Built My First Serverless API with AWS Lambda and API Gateway

Shashank RajputShashank Rajput
3 min read

A practical beginner's journey into the world of serverless technology


When I first heard about serverless architecture, I imagined some magical system where code simply lived on the cloud and scaled infinitely. No servers, no infrastructure headaches — just pure focus on building functionality.

Driven by this curiosity, I decided to create my first serverless API using AWS Lambda and API Gateway. Here's the step-by-step journey of how I did it.


Step 1: Understanding What Serverless Means

In simple words, serverless means:

  • No managing physical or virtual servers.

  • You write code; cloud provider (AWS) runs it.

  • You only pay when your code executes.

For my first project, I planned to build a simple API that returns a "Hello World" response.


Step 2: Setting Up AWS Lambda

  1. Logged into AWS Console.

  2. Went to AWS Lambda service.

  3. Clicked Create function > Author from scratch.

  4. Filled basic details:

    • Function name: helloWorldAPI

    • Runtime: Node.js 18.x (you can pick Python, Go, etc.)

  5. Selected "Create a new role with basic Lambda permissions".

  6. Clicked Create Function.

Inside the function, I wrote a basic Node.js handler:

exports.handler = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify({ message: "Hello, Serverless World!" }),
  };
};

Then I clicked Deploy to save my function.


Step 3: Exposing Lambda via API Gateway

Lambda functions are not public by default. So, I needed API Gateway to create a REST API endpoint.

  1. Navigated to API Gateway service.

  2. Clicked Create API > REST API > Build.

  3. Named it helloWorldAPI.

  4. Created a Resource: /hello

  5. Under /hello, added a GET Method:

    • Integration type: Lambda Function

    • Selected helloWorldAPI Lambda.

  6. Saved and gave permissions to API Gateway to invoke the Lambda.


Step 4: Deploying the API

Now that the API was wired to Lambda, it needed to be deployed.

  1. Clicked Actions > Deploy API.

  2. Created a new Stage called prod.

  3. Deployment created a public URL like:

https://abc123xyz.execute-api.ap-south-1.amazonaws.com/prod/hello

When I opened this URL in my browser, I got:

{"message": "Hello, Serverless World!"}

Success! My first serverless API was live on the Internet.


Step 5: Testing the API

I tested it using Postman:

  • Sent a GET request to the endpoint.

  • Received 200 OK with the correct message.

I also tried a few wrong methods (POST, PUT) to see how the API behaved (it gave method not allowed, as expected).


Challenges I Faced

  • IAM Permissions: Initially, my Lambda wasn't accessible because I didn't assign correct permissions to API Gateway.

  • Deployment Mistakes: After updating Lambda code, I forgot to redeploy API Gateway and wondered why changes weren't visible.

  • Timeout Errors: Misconfigured my timeout settings for bigger functions later.


Final Thoughts

Creating a serverless API using AWS Lambda and API Gateway opened up a new world for me. Instead of worrying about setting up servers, configuring load balancers, and scaling databases, I could just focus on writing code.

Serverless isn't just about simplicity; it's about rapid experimentation, scaling without headaches, and only paying for what you use.

If you're a beginner looking to start building backend services, I highly recommend creating a small Lambda function and exposing it through API Gateway. It's a rewarding experience that demystifies how modern cloud-native apps work.

Takeaway:

"The first API you deploy serverlessly might return just a 'Hello World', but it opens a whole world of possibilities!"

Happy Building!

0
Subscribe to my newsletter

Read articles from Shashank Rajput directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shashank Rajput
Shashank Rajput