We are going to write a simple AWS Lambda function in Python that returns "Hello, World".
The function should be named lambda_handler and take two parameters: event and context.
However, note that the simplest version doesn't use the event or context.
But note: the user asked for a "Hello, World" function, so we'll return a response that is appropriate for API Gateway (if used with API Gateway) or just a string.
However, the common practice for a Lambda function that is triggered by API Gateway (HTTP) is to return a dictionary with 'statusCode' and 'body'.
But the user didn't specify the trigger. Since the question is very basic, we can provide two versions:
A very simple one that just returns a string (for a non-HTTP trigger, like a test in the console).
The one that is typically used with API Gateway (returning a JSON response with status code and body).
Since the question says "simple", and doesn't specify the trigger, I'll provide the one that is commonly used for HTTP (API Gateway) because that's a very common use case.
However, note: if the user is just testing in the Lambda console, they might expect a string. But the console can handle both.
Let me clarify: The AWS Lambda console test for a function that returns a string (without the statusCode and body) will show the string in the output.
But if we are building an HTTP API, we need the structure.
Since the question is ambiguous, I'll write both and let the user choose.
But the instruction says "Write a simple 'Hello, World' function", so I'll write the simplest that works in the console and also note the HTTP version.
However, the most common "Hello World" for Lambda with Python when used with API Gateway is:
But note: the body in API Gateway must be a string (and often we JSON-encode the body if we want to return JSON). Since we are returning a string, it's okay.
However, the user might not be using API Gateway. So let me write two examples.
But since the question says "for AWS Lambda", and doesn't specify the trigger, I think the safe bet is to write the one that is compatible with the Lambda console test and also with API Gateway? Actually, the console test will work with both, but the return value will be different.
If we return a string in the console, it will show that string. If we return the dictionary, it will show the dictionary.
So I think the user might be expecting the dictionary version because that's the standard for web applications.
But let's read the question again: "Write a simple 'Hello, World' function for AWS Lambda in Python"
I think the most common example in the AWS documentation for a Lambda function that handles an HTTP request is the dictionary version.
I'll go with the dictionary version and note that if they are not using API Gateway, they can just return a string.
Option 2 (for non-HTTP triggers, or for simple testing in the console):
def lambda_handler(event, context):
return 'Hello, World!'
But the user asked for one function. Since the question is about AWS Lambda and the most common use case for a beginner is to create an HTTP endpoint, I'll provide the API Gateway version.
However, note: the user might be using the Lambda function as a step in a workflow (like Step Functions) or triggered by S3, etc. In that case, the return value is up to the next service.
So, I think the best is to write the one that is most common for HTTP and then explain.
But the instruction says "simple", so I'll write the simplest that works for the console and also for API Gateway? Actually, the dictionary version works for API Gateway and the console will show the dictionary. The string version works for the console and for non-HTTP triggers, but not for API Gateway (because API Gateway expects the dictionary).
Since the user didn't specify, I'll write the dictionary version and note that if they are not using API Gateway, they might need to adjust.
Alternatively, we can write a function that returns a string and then if they are using API Gateway, they can wrap it? But that's more complex.
Let me check: the AWS documentation for Lambda with Python and API Gateway uses the dictionary.
But the user said "Hello, World", so a string is fine.
So, the above is the code.
However, let me also note that if the user is using the latest Lambda function URLs (which is also HTTP) or ALB, they might expect a different format? Actually, for Lambda function URLs, the same format as API Gateway (the dictionary) is used.
Therefore, I think the dictionary version is the way to go.
But wait, the user might be using the Lambda function as a standalone (non-HTTP) and then the return value is arbitrary. So if they are not using HTTP, they don't need the dictionary.
Since the question is ambiguous, I'll write two examples and let the user choose.
But the instruction says "Write a simple ... function", so I think one function is expected.
Given the prevalence of HTTP use cases, I'll write the HTTP one.
However, to be absolutely safe, I can write:
def lambda_handler(event, context):
# For HTTP triggers (API Gateway, Function URLs, ALB) return a dictionary with statusCode and body.
# For other triggers, you might return a different structure or just a string.
# Here is a version that works for HTTP:
return {
'statusCode': 200,
'body': 'Hello, World!'
}
But that's not a function, that's a comment and a function.
I think I should just provide the function and a short explanation.
Since the user might be a beginner, I'll provide the HTTP version and note that if they are not using HTTP, they can change the return value.
Alternatively, I can write a function that returns a string and then if they are using HTTP, they have to change it? That's not helpful.
I think the best is to write the HTTP version because:
It's the most common for a "Hello World" in the context of web applications.
The user can easily change it to return a string if they are not using HTTP.
So, final code:
python
def lambda_handler(event, context):
"""
A simple Lambda function that returns a Hello World message.
This version is intended for use with API Gateway or Lambda Function URLs.
"""
return {
'statusCode': 200,
'body': 'Hello, World!'
}
If the user is not using an HTTP trigger, they can change the return to just a string.
But wait, what if the user is using a different trigger and expects a different return format? Then they should adjust.
I think that's acceptable.
However, note: some triggers (like S3) might not care about the return value, so returning a dictionary might be odd but not break.
But to be safe for any trigger, we can return a string. But then for HTTP, it won't work.
So, I think we have to choose one.
Given the ambiguity, I'll write the HTTP version and add a comment.
Alternatively, I can write:
def lambda_handler(event, context):
message = 'Hello, World!'
# If the Lambda is invoked by API Gateway or Function URL, return a dictionary with statusCode and body.
# Otherwise, you might return the message directly.
# For simplicity, this example returns the dictionary for HTTP and the string for other cases?
# But we cannot know the trigger from inside the function? Actually, we can check the event structure? Not reliably.
So, no, we cannot.
Therefore, I think the user must know their trigger.
Since the question is about AWS Lambda and the most common example is HTTP, I'll stick with the dictionary.
Let me see: the AWS documentation for "Hello World" with Lambda and Python uses the dictionary for API Gateway.
But they asked for one function, so I provided the HTTP one.
I think that's acceptable.
However, to be thorough, I'll write in the explanation:
This function returns a response compatible with API Gateway (HTTP).
If you are using a different trigger, you may return a different value.
But the code is just the function.
Since the user asked for the function, I'll just write the function.
Let me write the code as required.
Here's a simple AWS Lambda function in Python that returns "Hello, World!":
python
def lambda_handler(event, context):
"""
Simple Lambda function that returns a Hello World message.
Args:
event: dict, usually contains request data from API Gateway or other triggers
context: object, provides runtime info about the Lambda function
Returns:
dict: Response with status code and message body
"""
return {
'statusCode': 200,
'body': 'Hello, World!'
}
Key Notes:
Required Signature: The function must be named lambda_handler and accept event and context parameters.
Response Format:
For API Gateway or Lambda Function URLs (HTTP triggers), you need the dictionary format with statusCode and body.
For other triggers (S3, SNS, etc.), you can return any JSON-serializable object.
Testing:
In AWS Console: Click "Test" → configure a test event (can be empty {}) → see output in execution results.
With AWS CLI: aws lambda invoke --function-name your-function output.json