Skip to main content

Building Your First Lambda

Prerequisites

  • AWS account (free tier available)
  • Basic Python knowledge
  • ~30 minutes

Simple Explanation

What it is

You will build and run a tiny Lambda function so you can see the serverless workflow end to end.

Why we need it

Concepts are easier to trust once you deploy something yourself and see the logs, metrics, and results.

Benefits

  • Hands-on confidence with the Lambda console and runtime.
  • Clear understanding of events, handlers, and responses.
  • A foundation for Level 2 projects that add APIs and storage.

Tradeoffs

  • Console steps are manual and not repeatable like IaC.
  • AWS-specific flow that you will later generalize.

Real-world examples (architecture only)

  • HTTP test event → Function → Return JSON response.
  • Scheduled event → Function → Write one record.

Step 1: Create a Lambda Function

  1. Go to AWS Lambda Console
  2. Click Create Function
  3. Choose Author from scratch
  4. Fill in:
    • Function name: HelloServerless
    • Runtime: Python (see AWS runtime support)
    • Architecture: x86_64
  5. Default IAM role: Let AWS create one for you
  6. Click Create Function

Step 2: Write Code

AWS opens the code editor. Replace the default code:

Python

import json
from datetime import datetime

def lambda_handler(event, context):
print(f'Event received: {event}')

name = event.get('name', 'World')

return {
'statusCode': 200,
'body': json.dumps({
'message': f'Hello, {name}!',
'timestamp': datetime.now().isoformat(),
}),
}

What this does: Logs the incoming event, then returns a JSON response with a greeting and timestamp.

Step 3: Deploy

Click Deploy (button in top right).

You'll see a confirmation: "Changes deployed successfully."

Step 4: Test

Click Test tab.

Create a test event:

{
"name": "Serverless"
}

Click Invoke or press the Test button.

Expected Output

Response:
{
"statusCode": 200,
"body": "{\"message\":\"Hello, Serverless!\",\"timestamp\":\"<iso8601>\"}"
}

CloudWatch Logs

START RequestId: abc123...
Event received: {"name": "Serverless"}
END RequestId: abc123...
REPORT Duration: <duration>

Step 5: Inspect the Result

  • Response: Your function's return value
  • Logs: Everything printed with print()
  • Duration: How long the function took
  • Billed Duration: Time AWS charges you for

Step 6: Try More Tests

Test with different inputs:

{ "name": "Alice" }
{ }
{ "name": "Maarifa Student" }

Notice how your function handles different inputs.

Step 7: Monitor

Click the Monitor tab to see:

  • Invocations: How many times your function ran
  • Duration: Average execution time
  • Errors: Any failed invocations

What Just Happened?

  1. ✅ You wrote a function
  2. ✅ AWS provisioned infrastructure
  3. ✅ You tested it
  4. ✅ No server management required

This is serverless.

Next Steps

Your function currently takes a JSON input and returns a JSON output. In Level 2: Build, you'll:

  • Connect this function to an API Gateway (make it HTTP accessible)
  • Trigger it from S3 events or other AWS services
  • Build a complete application

Cleanup

To avoid charges:

  1. Go to the Lambda console
  2. Select your function
  3. Click Actions > Delete

(Free tier covers enough invocations that this won't cost anything, but it's good practice)

Key Takeaway

You just built and deployed a serverless function without touching a single server. That's the power of Lambda.


Project (Cloud-Agnostic)

Create a simple "greeting" function that accepts a name and returns a message with a timestamp.

Deliverables:

  1. Describe the vendor-neutral architecture (event source, compute, state, observability).
  2. Map each component to AWS or GCP services.
  3. Explain why the chosen runtime and trigger fit your use case.

If you want feedback, email your write-up to [email protected].


References