Skip to content

Latest commit

 

History

History
51 lines (36 loc) · 2.31 KB

README.md

File metadata and controls

51 lines (36 loc) · 2.31 KB

Title: Simple Hello World Lambda Function in Python

Description:

This Python script defines a Lambda function named lambda_handler that returns a simple "Hello world!" message in a JSON response format. It's designed for deployment in serverless environments like AWS Lambda.

Prerequisites:

  • Python 3.x installed

How to Use:

  1. Save the Code: Create a Python file (e.g., hello_world.py) and paste the following code:
#!/usr/bin/python3

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "headers": {
            "Content-Type": "text/plain"
        },
        "body": "Hello world!"
    }

if __name__ == "__main__":
    lambda_handler('demo', 'run')  # For local testing (optional)
  1. Local Testing (Optional):

    • If you want to test the function locally, uncomment the code block under if __name__ == "__main__":. This will execute the function and print the JSON response to the console.
  2. Deployment:

    • The specific deployment steps will vary depending on your chosen serverless platform (e.g., AWS Lambda, Google Cloud Functions). Refer to the documentation of your platform for detailed instructions.

Further Considerations:

  • This is a basic example. For production-ready Lambda functions, consider error handling, logging, environment variables, and security best practices.
  • The Lambda runtime environment might have limitations on file system access and dependencies. Be mindful of external libraries when deploying.

Additional Notes:

  • The #!/usr/bin/python3 line at the beginning specifies the Python interpreter to use when running the script directly.
  • The lambda_handler function takes two arguments:
    • event: This dictionary contains information about the event that triggered the Lambda function (e.g., HTTP request details).
    • context: This object provides additional information about the context of the Lambda execution (e.g., remaining execution time, memory utilization).
  • The function returns a dictionary with the following keys:
    • statusCode: The HTTP status code of the response (200 indicates success).
    • headers: A dictionary containing HTTP headers for the response (here, setting "Content-Type" to "text/plain").
    • body: The actual content of the response ("Hello world!" in this case).