ฟังก์ชัน Lambda แรกของคุณ (ไพธอน)
เขียนและนำฟังก์ชัน Lambda แรกของคุณไปใช้งานบน AWS ด้วยไพธอน พร้อมเรียนรู้โครงสร้างพื้นฐานและรูปแบบการทำงาน
ฟังก์ชัน Lambda แรกของคุณ (ไพธอน) เป็นบทเรียน Serverless Backend with AWS Lambda & API Gateway ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless Backend with AWS Lambda & API Gateway และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless Backend with AWS Lambda & API Gateway มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Welcome to AWS Lambda!
Time for your first function. AWS Lambda is Function as a Service: upload code, and Lambda runs it with zero servers to provision.
Why Python for Lambda?
Python is a great Lambda runtime — readable, rich libraries, perfect for APIs, data processing, and automation. Beginner-friendly too.
The Lambda Handler Function
Every Lambda needs an entry point: the handler. Lambda calls it with two args — event (input data) and context (runtime info).
Your First Python Lambda
Here's a minimal "Hello World" Lambda. All your logic lives in lambda_handler — run it locally to feel the shape.
import json
def lambda_handler(event, context):
# Your function's logic goes here
message = "Hello from your first Lambda!"
# Lambda functions typically return a dictionary
return {
'statusCode': 200,
'body': json.dumps(message)
}
# This block allows you to test your function locally
if __name__ == "__main__":
# Simulate an empty event and context for a simple test
mock_event = {}
mock_context = None
response = lambda_handler(mock_event, mock_context)
print("Local Test Response:", response)Understanding the 'event'
The event parameter carries the input that triggered your function, delivered as JSON and converted into a Python dict — an API call, an S3 upload, a queue message.
Code: Reading from the Event
Now read a name from the event, falling back to a default. Notice event.get('key', default) for safe access without KeyErrors.
import json
def lambda_handler(event, context):
# Get 'name' from the event, or default to 'there'
name = event.get('name', 'there')
message = f"Hello, {name} from Lambda!"
return {
'statusCode': 200,
'body': json.dumps(message)
}
# Local testing with different events
if __name__ == "__main__":
mock_event_with_name = {"name": "CoddyKit User"}
response_with_name = lambda_handler(mock_event_with_name, None)
print("With name:", response_with_name)
mock_event_no_name = {}
response_no_name = lambda_handler(mock_event_no_name, None)
print("Without name:", response_no_name)The 'context' Object
The context object exposes runtime info like aws_request_id, function_name, and remaining execution time. Handy even if you skip it often.
Returning a Standard Response
When API Gateway triggers your function, it expects a specific shape: a statusCode and a body that's a JSON string. That's how the client gets a clean response.
Code: Complete API Response
Here's a fuller, API-ready response with a Content-Type header and structured JSON body. We fake a context for local testing.
import json
def lambda_handler(event, context):
name = event.get('name', 'World')
# Simulate a context object for local testing
request_id = getattr(context, 'aws_request_id', 'local-test-id')
response_message = f"Greetings, {name}! Your request ID is {request_id}."
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json'
},
'body': json.dumps({
'message': response_message,
'input_data': event # Echoing input for demonstration
})
}
# Local testing with a dummy context
if __name__ == "__main__":
class MockContext:
aws_request_id = 'mock-request-123'
mock_event = {"name": "CoddyKit"}
mock_context_obj = MockContext()
response = lambda_handler(mock_event, mock_context_obj)
print("Full API Response:", response)How Lambda Executes Your Code
On each event, Lambda spins up (or reuses) a container, loads your code, calls the handler, runs it, and returns the result — then waits or shuts down.
Quick Check
Which of the following best describes the purpose of the event parameter in a Python Lambda handler function?
Recap: Your First Lambda!
Recap: Lambda runs code with no servers. The handler is your entry point, event carries input as a dict, context gives runtime info, and you return a status + JSON body.
คำถามที่พบบ่อย
บทเรียน “ฟังก์ชัน Lambda แรกของคุณ (ไพธอน)” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ฟังก์ชัน Lambda แรกของคุณ (ไพธอน)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless Backend with AWS Lambda & API Gateway ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless Backend with AWS Lambda & API Gateway มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ฟังก์ชัน Lambda แรกของคุณ (ไพธอน)”
เขียนและนำฟังก์ชัน Lambda แรกของคุณไปใช้งานบน AWS ด้วยไพธอน พร้อมเรียนรู้โครงสร้างพื้นฐานและรูปแบบการทำงาน คุณปฏิบัติ Serverless Backend with AWS Lambda & API Gateway ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless Backend with AWS Lambda & API Gateway หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless Backend with AWS Lambda & API Gateway บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ฟังก์ชัน Lambda แรกของคุณ (ไพธอน)” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Serverless Backend with AWS Lambda & API Gateway นี้ได้ไหม
ได้ บทเรียน Serverless Backend with AWS Lambda & API Gateway ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ไร้เซิร์ฟเวอร์คืออะไร
- ภาพรวมบริการหลักของ AWS
- ฟังก์ชัน Lambda แรกของคุณ (ไพธอน)
- ทำความเข้าใจรูปแบบการทำงานของ Serverless