Learn AWS-LAMBDA with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Simple AWS Lambda Function (Python)
# aws_lambda/demo/lambda_function.py
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': 'Hello, Lambda!'
}
A simple AWS Lambda function in Python that returns 'Hello, Lambda!' when invoked.
2
AWS Lambda Function with Query Parameters
# aws_lambda/demo/query.py
def lambda_handler(event, context):
name = event.get('queryStringParameters', {}).get('name', 'Guest')
return {
'statusCode': 200,
'body': f'Hello, {name}!'
}
Lambda reading query parameters from API Gateway and responding.
3
AWS Lambda Function with JSON Response
# aws_lambda/demo/json.py
def lambda_handler(event, context):
return {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': '{"message": "Hello JSON!"}'
}
Lambda returning JSON data.
4
AWS Lambda Function Handling POST Requests
# aws_lambda/demo/post.py
def lambda_handler(event, context):
if event.get('httpMethod') == 'POST':
body = event.get('body')
return {
'statusCode': 200,
'body': f'Received: {body}'
}
return {
'statusCode': 405,
'body': 'Method Not Allowed'
}
Lambda processing POST request body from API Gateway.
5
AWS Lambda Function Using Environment Variables
# aws_lambda/demo/env.py
import os
def lambda_handler(event, context):
api_key = os.environ.get('MY_API_KEY', 'NotSet')
return {
'statusCode': 200,
'body': f'API Key length: {len(api_key)}'
}
Lambda accessing environment variables.
6
AWS Lambda Function Redirect
# aws_lambda/demo/redirect.py
def lambda_handler(event, context):
return {
'statusCode': 302,
'headers': {'Location': 'https://example.com'},
'body': ''
}
Lambda returning a redirect response.
7
AWS Lambda Function with Custom Headers
# aws_lambda/demo/headers.py
def lambda_handler(event, context):
return {
'statusCode': 200,
'headers': {'X-Custom-Header': 'LambdaExample'},
'body': 'Custom headers added'
}
Lambda adding custom headers in response.
8
AWS Lambda Function Fetching External API
# aws_lambda/demo/fetch.py
import requests
def lambda_handler(event, context):
response = requests.get('https://api.github.com')
return {
'statusCode': 200,
'body': response.text
}
Lambda fetching data from an external API and returning it.
9
AWS Lambda Function Returning HTML
# aws_lambda/demo/html.py
def lambda_handler(event, context):
html = '<!DOCTYPE html><html><body><h1>Hello HTML!</h1></body></html>'
return {
'statusCode': 200,
'headers': {'Content-Type': 'text/html'},
'body': html
}
Lambda responding with a simple HTML page.
10
AWS Lambda Function Conditional Response
# aws_lambda/demo/method.py
def lambda_handler(event, context):
method = event.get('httpMethod')
if method == 'POST':
return {'statusCode': 200, 'body': 'You sent a POST request'}
return {'statusCode': 200, 'body': 'You sent a GET request'}
Lambda responding differently based on HTTP method.