You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
AWS Lambda is Amazon's serverless compute service. You upload your code, configure a trigger, and Lambda runs your function in response to events — scaling automatically from zero to thousands of concurrent executions. There are no servers to provision, no operating systems to patch, and no capacity to plan.
When an event triggers a Lambda function, the following sequence occurs:
Event Source Lambda Service Execution Environment
+-----------+ +----------------+ +---------------------+
| API GW | -----> | Invoke API | -----> | Init runtime |
| S3 Event | | Route event | | Load function code |
| SQS Msg | | Scale if | | Execute handler() |
| Schedule | | needed | | Return response |
+-----------+ +----------------+ +---------------------+
Lambda supports a wide range of programming languages through managed runtimes:
| Runtime | Language | Typical Cold Start |
|---|---|---|
nodejs20.x | JavaScript / TypeScript | ~100–300 ms |
python3.12 | Python | ~100–300 ms |
java21 | Java | ~500–3000 ms |
dotnet8 | C# / F# | ~400–1500 ms |
provided.al2023 | Any (custom runtime) | Varies |
ruby3.3 | Ruby | ~200–500 ms |
Custom runtimes let you bring any language — Go, Rust, C++, PHP — by implementing the Lambda Runtime API. Compiled languages like Go and Rust achieve very fast cold starts (~10–50 ms).
Every Lambda function consists of a handler — the entry point that the runtime calls when an event arrives.
// index.mjs
export const handler = async (event, context) => {
console.log('Event:', JSON.stringify(event));
const name = event.queryStringParameters?.name || 'World';
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: `Hello, \${name}!` }),
};
};
# lambda_function.py
import json
def handler(event, context):
print(f"Event: {json.dumps(event)}")
name = event.get("queryStringParameters", {}).get("name", "World")
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"message": f"Hello, {name}!"}),
}
event ObjectThe event parameter contains the data from the trigger source. Its structure varies by source:
| Trigger Source | Event Contents |
|---|---|
| API Gateway | HTTP method, path, headers, query params, body |
| S3 | Bucket name, object key, event type |
| SQS | Message body, message attributes, receipt handle |
| DynamoDB Streams | New/old item images, event type (INSERT, MODIFY, REMOVE) |
| EventBridge | Source, detail-type, detail payload |
| Scheduled (cron) | Scheduled event metadata |
context ObjectThe context parameter provides runtime information:
{
functionName: 'my-function',
functionVersion: '$LATEST',
memoryLimitInMB: '256',
logGroupName: '/aws/lambda/my-function',
logStreamName: '2024/01/15/[$LATEST]abc123',
awsRequestId: 'a1b2c3d4-5678-90ab-cdef-example',
getRemainingTimeInMillis: [Function] // Time left before timeout
}
Lambda supports two invocation modes:
The caller waits for the function to complete and receives the response directly.
Caller ----request----> Lambda ----response----> Caller
(blocks until complete)
Used by: API Gateway, Application Load Balancer, SDK Invoke calls
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.