AWS Builder Center

Your First Serverless API: Lambda and API Gateway Without the Guesswork

A practical walkthrough of building a small serverless API with AWS Lambda and API Gateway, covering IAM permissions, proxy integration, and the beginner mistakes that trip up most first attempts.

A few semesters back, a friend of mine wanted to build a small app for tracking gym attendance at our hostel. Nothing fancy, just a way to log who showed up and pull some numbers at the end of the week. The problem was he didn't want to rent a server, patch it, babysit it, and then pay for it to sit idle at 3 a.m. when nobody was using it.
That's basically the pitch for serverless computing on AWS. You write a function, AWS runs it only when someone calls it, and you stop thinking about the machine underneath. For a small project like a gym tracker, or honestly for a lot of real production systems, this setup with AWS Lambda and Amazon API Gateway is often the fastest way to get from idea to working API.

What Problem Are We Actually Solving

Say you want an endpoint like  GET /attendance  that returns a list of check-ins, or  POST /attendance  that logs a new one. In a traditional setup you'd spin up an EC2 instance, install a web server, write routing logic, and keep the whole thing running around the clock even if three people use it a day.
With Lambda, you write the function that handles the request. API Gateway sits in front of it and turns HTTP requests into events that Lambda understands. AWS handles scaling, patching, and availability. You pay per request and per millisecond of execution time, not for idle hours.

The Core Concepts, Before the Code

A couple of things are worth understanding before you touch the console.
Lambda functions are stateless. Each invocation is basically fresh. If you need to remember anything between calls (a user's login session, a counter, actual data), you store it somewhere else, like DynamoDB or S3. Don't try to keep state in memory and expect it to survive between calls, because Lambda might spin up a new instance of your function at any time.
API Gateway is just a router with extra features. It maps HTTP methods and paths to specific Lambda functions, and it can also handle things like request validation, throttling, and API keys. For a beginner project, you mostly care about the routing part.
IAM roles decide what your function is allowed to touch. This trips up almost everyone the first time. Your Lambda function doesn't automatically have permission to read from DynamoDB or write to S3 just because it's in the same AWS account. You attach a role with specific permissions, and if you forget, you'll get an  AccessDenied  error that has nothing to do with your actual code.

Building It: A Basic Example

Let's say we're logging attendance entries into DynamoDB. Here's a simplified version of what the Lambda function might look like in Python.
python
import json
import boto3
import uuid
from datetime import datetime

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('AttendanceLog')

def lambda_handler(event, context):
method = event['httpMethod']

if method == 'POST':
body = json.loads(event['body'])
entry = {
'id': str(uuid.uuid4()),
'name': body['name'],
'timestamp': datetime.utcnow().isoformat()
}
table.put_item(Item=entry)
return {
'statusCode': 201,
'body': json.dumps(entry)
}

if method == 'GET':
response = table.scan()
return {
'statusCode': 200,
'body': json.dumps(response['Items'])
}

return {'statusCode': 405, 'body': 'Method not allowed'}
A few things going on here.  event['httpMethod']  tells us whether this was a GET or a POST, which is how API Gateway passes that info along by default (this is the older REST API proxy format; HTTP APIs structure the event slightly differently, so check which one you're using). The function talks directly to DynamoDB using  boto3 , AWS's Python SDK.
To wire this up, you'd create the function in Lambda, then in API Gateway create a resource like  /attendance , attach GET and POST methods, and point each one to this Lambda function using Lambda proxy integration. That last part matters because it changes how the request and response are shaped, and it's an easy thing to get wrong on your first attempt.
You also need to grant this function's execution role permission to read and write to that specific DynamoDB table. You can do this by attaching a policy like the AWS managed  AmazonDynamoDBFullAccess  while learning, though for anything beyond a class project you'll want to scope that down to just the actions and table you actually need.

When This Setup Actually Makes Sense

Serverless isn't the answer to everything, and it's worth being honest about that. It's a strong fit when your traffic is unpredictable or low-volume, when you want to avoid managing infrastructure, or when you're prototyping something and don't want to commit to a fixed server cost. A student project, a hackathon backend, or an internal tool used by a handful of people fits this pattern well.
It's a worse fit for workloads that need to run continuously, need very low and consistent latency, or involve long-running processes. Lambda functions have execution time limits, and I'd recommend checking the current maximum in the AWS documentation rather than relying on a number you read somewhere, since these limits have changed over the years and I don't want to state one here that might be outdated by the time you read this.

Mistakes I Made (So You Don't Have To)

Forgetting to enable Lambda proxy integration in API Gateway, which means the event object doesn't look the way you expect and your code throws confusing errors about missing keys.
Not setting CORS headers, which is invisible until you try to call the API from a browser-based frontend and everything silently fails with a CORS error in the console.
Assuming permissions are automatic. If your function can't talk to DynamoDB, S3, or anything else, check the IAM role attached to the function before you assume your code is broken.
Testing only in the Lambda console and never through the actual API Gateway endpoint. The two environments behave a little differently, especially around how the event object is structured, so test the real URL before you call it done.

Key Takeaways

  • Lambda runs your code on demand and charges based on usage, not uptime, which makes it a good fit for unpredictable or low-traffic workloads.
  • API Gateway routes HTTP requests to your Lambda functions and needs to be configured with proxy integration to pass requests through cleanly.
  • IAM roles control what your function can actually do, and most "my code is broken" moments early on turn out to be permission issues instead.
  • This combination is great for prototypes and small projects, but check current AWS limits and pricing directly from the documentation since those details change over time.
  • Test against the real API Gateway URL, not just inside the Lambda console, before assuming your API is working.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article