دمج Lambda مع API Gateway
هيّئ API Gateway لتشغيل دوال Lambda، وأنشئ نقطة نهاية HTTP عديمة الخوادم متكاملة
دمج Lambda مع API Gateway درس مجاني في Serverless Backend with AWS Lambda & API Gateway على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Serverless Backend with AWS Lambda & API Gateway، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Serverless Backend with AWS Lambda & API Gateway 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Connect Lambda to API Gateway
Welcome! In this lesson, you'll learn how to link your AWS Lambda functions to API Gateway. This creates a powerful serverless HTTP endpoint, letting users trigger your code via web requests.
Think of it as giving your Lambda function a web address!
API Gateway: Your Web Front Door
Remember, API Gateway acts as the front door for your serverless applications. It handles incoming HTTP requests and routes them to the correct backend service, like a Lambda function.
- Manages traffic efficiently
- Handles authentication and authorization
- Routes requests to specific functions
The Lambda Proxy Integration
The most common and flexible way to connect API Gateway to Lambda is using Lambda Proxy Integration. This method passes the entire incoming HTTP request to your Lambda function.
Your Lambda then processes the request and returns a specific JSON response that API Gateway understands.
Request & Response Format
With proxy integration, API Gateway sends the full HTTP request (headers, body, query parameters) to your Lambda as an event object.
Your Lambda must return a JSON object with specific keys: statusCode, headers, and body. This allows API Gateway to craft the final HTTP response for the client.
Our First Proxy Lambda
Let's write a simple Python Lambda function that expects a proxy event. It will return a 'Hello from Lambda!' message and echo back part of the input.
Remember, the event dictionary contains all request details.
import json
def lambda_handler(event, context):
# The 'event' contains details like
# path, httpMethod, headers, body, queryStringParameters
print(f"Received event: {json.dumps(event)}")
# Prepare the response body
response_body = {
"message": "Hello from Lambda! Your path was:",
"path": event.get("path", "N/A")
}
# Return the response in the required proxy format
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps(response_body)
}Setting Up API Gateway
Now, let's set up the API Gateway part. We'll create an HTTP API and define a route.
HTTP APIs are simpler and more cost-effective for basic serverless endpoints compared to REST APIs. We covered the differences in a previous lesson.
Defining a Route & Integration
In API Gateway, you'll create a route, for example, GET /hello. This route specifies the HTTP method and path for your endpoint.
Then, you'll add an integration to this route, pointing it to your Lambda function. This is where API Gateway knows which Lambda to invoke!
Granting Invoke Permissions
For API Gateway to successfully trigger your Lambda function, it needs permission. This is managed by an IAM Role associated with API Gateway.
Specifically, API Gateway requires the lambda:InvokeFunction permission on your Lambda resource. AWS often sets this up automatically when integrating via the console.
Deploy & Test Your Endpoint
After configuring the route and integration, you need to deploy your API Gateway changes. This makes your endpoint publicly accessible.
You'll get an invoke URL, which you can use with tools like a web browser or curl to test your new serverless API!
Integration Check
You've learned how Lambda Proxy Integration works. Which of the following are REQUIRED in the JSON response from a Lambda function for API Gateway Proxy Integration?
Recap: Lambda & API Gateway
Great job! You've learned how to integrate Lambda functions with API Gateway:
- API Gateway acts as the web front door for your serverless functions.
- Lambda Proxy Integration is the standard method, passing full HTTP requests to Lambda.
- Lambda returns a specific JSON format (
statusCode,headers,body) for API Gateway to process. - IAM permissions are crucial for API Gateway to invoke your Lambda function securely.
- After deployment, your Lambda function is accessible via a public URL!
الأسئلة الشائعة
هل درس «دمج Lambda مع API Gateway» مجاني؟
نعم — نص درس «دمج Lambda مع API Gateway» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Serverless Backend with AWS Lambda & API Gateway، انتقل إلى CoddyKit PRO. تتضمن دورة Serverless Backend with AWS Lambda & API Gateway 4 دروس في المجموع.
ماذا ستتعلم في «دمج Lambda مع API Gateway»؟
هيّئ API Gateway لتشغيل دوال Lambda، وأنشئ نقطة نهاية HTTP عديمة الخوادم متكاملة تتمرن على Serverless Backend with AWS Lambda & API Gateway مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Serverless Backend with AWS Lambda & API Gateway؟
لا تُشترط خبرة سابقة. Serverless Backend with AWS Lambda & API Gateway على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «دمج Lambda مع API Gateway»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Serverless Backend with AWS Lambda & API Gateway هذا؟
نعم. كل درس في Serverless Backend with AWS Lambda & API Gateway يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مقدمة إلى API Gateway
- واجهة HTTP API مقارنةً بواجهة REST API
- دمج Lambda مع API Gateway
- تعيين الطلبات والاستجابات في API Gateway