On this page
How to Deploy Your First Serverless Function on AWS Lambda
A step-by-step tutorial to deploy a Python function on AWS Lambda with an HTTP trigger, test it, and read its logs — no servers to manage.
Quick answer
- An AWS Lambda function is code plus a trigger; the simplest trigger to start with is a Function URL.
- Write a handler that takes an event and returns a response, upload it, and test with a URL.
- Success is an HTTP 200 response and a matching entry in CloudWatch Logs.
Step 1 — Write the function
Create a file lambda_function.py:
import json
def lambda_handler(event, context):
name = event.get("queryStringParameters", {}).get("name", "world")
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"message": f"Hello, {name}!"})
}The handler receives an event (the request data) and returns a response object.
Step 2 — Create the function in the console
In the AWS Lambda console, choose Create function → Author from scratch, name it hello-function, and choose the Python runtime. Paste the code above into the editor and click Deploy.
How to verify it worked: the editor saves without errors and shows “Changes deployed.”
Step 3 — Add a Function URL
In the function’s Configuration → Function URL, enable it with Auth type: NONE for a public test endpoint, then copy the generated URL.
Step 4 — Invoke the function
curl "https://YOUR-ID.lambda-url.REGION.on.aws/?name=Ada"How to verify it worked: the response is {"message": "Hello, Ada!"} with an HTTP 200.
Step 5 — Read the logs
Add a print statement, redeploy, and invoke again. In the Lambda console’s Monitor → View logs in CloudWatch, you’ll see the invocation with your print output. How to verify it worked: each invocation produces a log stream entry, proving observability is working.
Step 6 — Add a secret properly
Don’t paste an API key into the code. Add it in Configuration → Environment variables as API_KEY, then read it in the handler with os.environ["API_KEY"].
Where this bites vibecoders
The first Lambda an AI assistant generates often works — then it’s left with a public Function URL and no auth, or a secret hardcoded in the source. Treat “it returned 200” as step one, not the end: lock down the endpoint and move secrets into environment variables before calling anything done.
Where AI coding assistants get this wrong
- Hardcoding secrets in the handler instead of using environment variables.
- Leaving a public Function URL with
Auth type: NONEon anything non-trivial. - Ignoring timeout and memory, so long work gets killed with a generic error.
- Writing a handler that can’t parse its own event shape and fails on the first real request.
Checklist
- Keep the handler small and single-purpose.
- Test the real event shape, not a made-up one.
- Store secrets in environment variables, never in code.
- Set timeout and memory to match the workload.
- Confirm logs are flowing before you rely on the function.
FAQ
What is a Lambda handler?
A handler is the entry-point function the Lambda runtime calls, receiving an event and a context and returning a response. Its name is configured in the function’s runtime settings (here, lambda_function.lambda_handler).
How much does Lambda cost?
Lambda bills by invocations and compute time (GB-seconds), with a generous free tier. A rarely-called function costs essentially nothing; a high-traffic one can add up. Set up billing alerts so costs stay visible.
When should I not use Lambda?
For long-running processes (a function has a maximum timeout), latency-critical endpoints sensitive to cold starts, or steady always-on workloads where a server is cheaper. See What Is Serverless Computing?.
Related topics
- How to Deploy Your First App on AWS for Free
- What Is Serverless Computing?
- How to Manage Secrets and Environment Variables Properly
- What Is FinOps?