Here's an example of a simple Lambda function written in Python that responds to an HTTP request:
import json
def lambda_handler(event, context):
# 'event' contains information about the request
print("Received event: " + json.dumps(event))
# 'context' provides runtime information
print("Remaining time (ms):", context.get_remaining_time_in_millis())
# Extracting parameters from the HTTP request
if 'queryStringParameters' in event:
queryParams = event['queryStringParameters']
if 'name' in queryParams:
name = queryParams['name']
else:
name = 'World'
else:
name = 'World'
# Constructing the response object
response = {
"statusCode": 200,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
},
"body": json.dumps({
"message": "Hello, " + name + "!"
})
}
# Returning the response object
return response
In this example:
- The lambda_handler function is the entry point for the Lambda function.
- It receives an event parameter containing HTTP request information.
- It constructs a response object containing a simple greeting message.
- The response is returned in JSON format with appropriate headers.