API Gateway สำหรับปลายทาง Lambda
สร้าง RESTful API ที่ขยายระบบได้และปลอดภัยด้วยการผสาน AWS Lambda กับ Amazon API Gateway พร้อมจัดการการยืนยันตัวตนและการจับคู่คำขอ/การตอบกลับ
API Gateway สำหรับปลายทาง Lambda เป็นบทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
API Gateway & Lambda: The Duo
Welcome! In this lesson, we'll connect AWS Lambda with Amazon API Gateway. This powerful combination lets you build scalable, serverless APIs without managing any servers.
Think of API Gateway as the front door for your applications, routing requests to the right backend service, often a Lambda function.
Why Use API Gateway?
API Gateway offers several benefits when exposing Lambda functions:
- Scalability: Handles thousands of concurrent API calls.
- Traffic Management: Manages traffic, including throttling and caching.
- Security: Provides built-in authentication and authorization.
- Monitoring: Integrates with CloudWatch for logging and monitoring.
API Gateway's Core Function
API Gateway acts as a "proxy" between clients (like web browsers or mobile apps) and your backend services (like Lambda functions).
It takes incoming HTTP requests, transforms them if needed, invokes your Lambda function, and then sends the function's response back to the client.
Setting Up Your First API
To create an API Gateway endpoint for Lambda, you define:
- Resources: The URL paths (e.g.,
/items,/users). - Methods: The HTTP verbs (GET, POST, PUT, DELETE) for each resource.
- Integration: The backend service (our Lambda function) that the method invokes.
Let's look at a simple Lambda function that an API Gateway can call.
Basic Lambda for API Gateway
This Python Lambda function is designed to be triggered by API Gateway. It expects an event object containing details about the HTTP request and returns a dictionary with statusCode and body for the API response.
import json
def lambda_handler(event, context):
# Print the incoming event for debugging
print(f"Received event: {json.dumps(event)}")
# Extract query parameters if present
name = event.get('queryStringParameters', {}).get('name', 'World')
response_body = {
"message": f"Hello, {name} from Lambda!",
"input": event
}
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps(response_body)
}Request/Response Mapping
API Gateway can transform requests before sending them to Lambda and responses before sending them back to the client.
This is crucial for:
- Converting request bodies to a format your Lambda expects.
- Extracting path parameters or headers into the Lambda event object.
- Formatting Lambda's output into a standard HTTP response.
Accessing Path Parameters
Let's say your API has a path like /users/{userId}. API Gateway can map userId from the URL path directly into your Lambda's event object.
Your Lambda can then access it via event['pathParameters']['userId'].
API Authentication Options
API Gateway provides robust security options:
- IAM Roles/Policies: Control access using AWS Identity and Access Management.
- Lambda Authorizers: Use a Lambda function to validate custom tokens (e.g., JWTs) before invoking your main Lambda.
- Cognito User Pools: Integrate with Amazon Cognito for user authentication and authorization.
Custom Auth with Lambda Authorizers
A Lambda Authorizer is a separate Lambda function that API Gateway invokes before your main function.
It checks the incoming request's token (e.g., in a header), and if valid, returns an IAM policy allowing or denying access to your API resources. This is powerful for custom authentication logic.
Tips for API Gateway & Lambda
Keep these in mind for efficient APIs:
- Small Lambdas: Keep functions focused on a single task.
- Error Handling: Implement robust error handling in Lambda and configure API Gateway to return meaningful error messages.
- Caching: Use API Gateway's caching to reduce latency and Lambda invocations.
- Stages: Use API Gateway stages (e.g., dev, prod) for versioning and deployment management.
API Gateway Features
Which of the following are key features or benefits of using Amazon API Gateway to expose AWS Lambda functions?
Recap: API Gateway & Lambda
We've learned how Amazon API Gateway acts as the entry point for your serverless applications, routing HTTP requests to AWS Lambda functions.
You now understand its benefits, how to configure basic integrations, handle request/response mapping, and secure your APIs using various authentication methods, including Lambda Authorizers. This combination is fundamental for building modern, scalable backends!
คำถามที่พบบ่อย
บทเรียน “API Gateway สำหรับปลายทาง Lambda” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “API Gateway สำหรับปลายทาง Lambda” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “API Gateway สำหรับปลายทาง Lambda”
สร้าง RESTful API ที่ขยายระบบได้และปลอดภัยด้วยการผสาน AWS Lambda กับ Amazon API Gateway พร้อมจัดการการยืนยันตัวตนและการจับคู่คำขอ/การตอบกลับ คุณปฏิบัติ AWS for Backend Developers (EC2, S3, RDS, Lambda) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน AWS for Backend Developers (EC2, S3, RDS, Lambda) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “API Gateway สำหรับปลายทาง Lambda” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) นี้ได้ไหม
ได้ บทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเรียกใช้ Lambda แบบไม่พร้อมกัน
- เลเยอร์และตัวแปรสภาพแวดล้อมของ Lambda
- API Gateway สำหรับปลายทาง Lambda
- Step Functions สำหรับการประสานงาน Lambda