My First Serverless Function: AWS Lambda for Beginners
No servers to configure, no operating system to patch, and it fits in the free tier. These are my notes from building my first AWS Lambda function, from writing the code to calling it from the internet, written by someone still learning.
Why I’m writing this
While studying for AWS Cloud Practitioner, “serverless” kept showing up in every module, and I realized I could repeat the definition without really understanding it. So I built the smallest thing I could: a function that answers a web request. This article is my notes from that process. I’m not an expert; I’m writing it the way I wish someone had explained it to me.
What “serverless” actually means
There are still servers. The difference is that they’re not your problem. With an EC2 instance, you choose the machine, install the operating system, keep it updated and pay for every hour it’s on, even when nobody uses it.
With AWS Lambda, you only upload your code. AWS runs it when something triggers it, like a web request, a file uploaded to S3, or a schedule. When the code finishes, it stops, and you pay only for the requests and the time it actually ran. If nobody calls your function for a week, it costs nothing that week.
Lambda also has a monthly free allowance that doesn’t expire: one million requests and 400,000 GB-seconds of compute time. For learning, you’re very unlikely to leave it.
The three pieces of a Lambda function
Every function I’ve looked at has the same structure, and understanding these three pieces made everything else easier.
The handler is the function AWS calls. In Python, it’s usually named
lambda_handler . Think of it as the main of your program.The event is the input. It’s a dictionary with information about whatever triggered the function. For a web request, it contains the path, the headers and the query parameters.
The context is information about the execution itself: how much time is left, how much memory the function has, and a unique ID for the request, which is very useful when you’re looking for something in the logs.
Step 1: Create the function
In the Lambda console, choose Create function, then Author from scratch. Give it a name like
hello-builder , choose a recent Python runtime such as Python 3.13, and leave the architecture as it is.Before you click Create, open Advanced settings and check Enable function URL. A function URL gives your function its own HTTPS address, so you can call it from a browser without setting up anything else. For the auth type, choose NONE for this lab; I’ll come back to what that means.
Step 2: Write the code
Replace the sample code with this:
python
import jsonfrom datetime import datetime, timezone, timedelta
# Guatemala is UTC-6 all year (no daylight saving time)GUATEMALA = timezone(timedelta(hours=-6))
def lambda_handler(event, context): params = event.get("queryStringParameters") or {} name = params.get("name", "builder")
now = datetime.now(GUATEMALA)
body = { "message": f"Hello, {name}!", "guatemala_time": now.strftime("%Y-%m-%d %H:%M"), "request_id": context.aws_request_id, }
print(f"Greeted {name}")
return { "statusCode": 200, "headers": {"Content-Type": "application/json"}, "body": json.dumps(body), }It reads an optional
name from the URL, gets the current time in Guatemala, and returns everything as JSON. The print line looks unimportant, but it’s how you’ll see what happened later.Click Deploy. Until you deploy, your changes only exist in the editor.
Step 3: Call it from the internet
Copy the function URL from the top of the function page. It looks like
https:// followed by a long ID and .lambda-url.us-east-1.on.aws/ . Open it in your browser, or use curl:bash
curl "https://YOUR-FUNCTION-ID.lambda-url.us-east-1.on.aws/?name=Steven"You should get something like this:
json
{"message": "Hello, Steven!", "guatemala_time": "2026-11-12 08:45", "request_id": "..."}That’s it. No server, no web framework, no port configuration. When I saw that response for the first time, the definition of serverless finally made sense.
Step 4: Look at the logs
Every time the function runs, Lambda sends its output to Amazon CloudWatch Logs, in a log group named
/aws/lambda/hello-builder . From the function page, open the Monitor tab and choose View CloudWatch logs.Inside each log stream you’ll find your
Greeted Steven line, plus a REPORT line that Lambda adds automatically. That line shows how long the function ran, how much it was billed for and how much memory it used. It’s the best way to understand what you’re actually paying for.You might notice that the first request after a while is slower than the next ones. That’s called a cold start: Lambda has to prepare a new environment for your code before running it. The following requests reuse that environment and are faster. For a small function like this one, the difference is barely noticeable.
A word about security
Choosing NONE as the auth type means anyone with the URL can call your function. For a greeting that returns the time, that’s fine. For anything that reads data, writes to a database or costs money per call, it isn’t.
The alternative is the AWS_IAM auth type, where only requests signed with valid AWS credentials are accepted. That’s the right default once a function does something real. Two more habits I’m trying to build: give the function’s execution role only the permissions it needs, and never put passwords or keys directly in the code.
Clean up
When you’re done, delete the function from the Lambda console. Then go to CloudWatch Logs and delete the
/aws/lambda/hello-builder log group, because logs aren’t deleted with the function. Leaving a test function online with a public URL is exactly the kind of thing that gets forgotten.What I learned
Building one small function taught me more than reading about serverless several times. The handler, event and context pattern repeats in every Lambda I’ve looked at since, and the logs explain the pricing better than any diagram. My next step is to connect a function to something other than a URL, like a file arriving in S3, to see how the event changes.
If you also built your first Lambda recently, tell me in the comments what you made it do.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article