Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
57 views
in Information Technology by (155k points)
Example of creating a Lambda function using Python

Please log in or register to answer this question.

1 Answer

+1 vote
by (155k points)

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.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...