使用 API Gateway 调用 Lambda
配置 API Gateway 作为 Lambda 函数的 HTTP 端点,使其能够处理 Web 请求并构建 RESTful API
使用 API Gateway 调用 Lambda 是 CoddyKit 上的免费 Serverless AWS Lambda Development 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Serverless AWS Lambda Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Serverless AWS Lambda Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Your Lambda's Front Door
Imagine you have a fantastic serverless function, but how do people access it from a web browser or another application? That's where Amazon API Gateway comes in!
API Gateway acts as the "front door" for your applications. It's a fully managed service that helps developers create, publish, maintain, monitor, and secure APIs at any scale.
Why Pair API Gateway with Lambda?
Lambda functions are great for running code without managing servers. But by themselves, they don't have public HTTP endpoints accessible over the internet.
API Gateway provides these endpoints, transforming simple Lambda functions into powerful, accessible web services. This combination is a core pattern for building serverless RESTful APIs.
Turning Lambda into an HTTP Endpoint
When you integrate Lambda with API Gateway, API Gateway handles the incoming HTTP request and routes it to your Lambda function. After your Lambda processes the request and returns a response, API Gateway takes that response and sends it back to the client.
- Client Request: Browser or app sends HTTP request.
- API Gateway: Receives, authenticates, and routes request.
- Lambda Invocation: API Gateway triggers your Lambda.
- Lambda Response: Lambda processes and returns data.
- Client Response: API Gateway formats and sends response back.
Simplified Lambda Proxy Integration
One of the most common ways to integrate Lambda with API Gateway is using Lambda Proxy Integration.
With proxy integration, API Gateway passes the raw HTTP request data (like headers, body, query parameters) directly to your Lambda function. Your Lambda then returns a response in a specific format that API Gateway understands, which it then uses to create the HTTP response for the client.
Building an API-Ready Lambda
For API Gateway's proxy integration, your Lambda function needs to expect an event object containing HTTP request details and return an object with statusCode, headers, and body.
Let's look at a simple Python example:
import json
def lambda_handler(event, context):
# Log the incoming event for debugging
print(f"Received event: {event}")
# Extract path parameter if available
name = "World"
if event.get('pathParameters') and 'name' in event['pathParameters']:
name = event['pathParameters']['name']
# Construct the response body
response_message = f"Hello, {name} from CoddyKit Lambda!"
# API Gateway expects a specific response format
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json'
},
'body': json.dumps(response_message)
}Lambda Response Format
The Python Lambda function we saw returns a dictionary. API Gateway uses this dictionary to construct the HTTP response sent back to the client.
statusCode: The HTTP status code (e.g.,200for success).headers: A dictionary of HTTP response headers.body: The actual response content, which must be a string (often JSON stringified).
This strict format ensures API Gateway can correctly translate your Lambda's output into a standard HTTP response.
Setting Up the API Endpoint
To connect your Lambda function, you'd typically:
- Create a new REST API in API Gateway.
- Define a Resource (e.g.,
/helloor/hello/{name}for path parameters). - Add a Method (e.g.,
GET) to your resource. - Configure the method to use Lambda Proxy Integration, pointing to your Lambda function.
- Grant API Gateway permission to invoke your Lambda.
These steps create the bridge between your HTTP request and your serverless function.
Deploying Your API
After configuring your API Gateway resources and methods, you need to deploy the API to a stage.
A stage is a logical reference to a lifecycle state of your API (e.g., dev, test, prod). Deploying creates a publicly accessible URL for your API, like https://abcdefg.execute-api.us-east-1.amazonaws.com/dev/hello.
You can then use this URL to send HTTP requests to your Lambda function.
Invoking Your New API
Once deployed, you can test your API endpoint using tools like a web browser, curl, or Postman.
For example, if your API is deployed to /dev/hello/{name}, you could visit:
https://your-api-id.execute-api.region.amazonaws.com/dev/hello/Coddy
This would invoke your Lambda, which would then return a "Hello, Coddy from CoddyKit Lambda!" message.
API Gateway Integration Check
You've learned how API Gateway acts as a front door for Lambda. Let's test your understanding of how these services work together.
Recap: API Gateway & Lambda
In this lesson, we discovered how Amazon API Gateway transforms your Lambda functions into accessible HTTP endpoints. You learned:
- API Gateway acts as the "front door" for serverless applications.
- Lambda Proxy Integration simplifies passing requests and responses.
- The specific response format your Lambda needs to return.
- The conceptual steps to define, deploy, and test your API.
This powerful combination is fundamental for building serverless web APIs!
常见问题解答
「使用 API Gateway 调用 Lambda」课时是免费的吗?
是的 — 「使用 API Gateway 调用 Lambda」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Serverless AWS Lambda Development 课程的其余内容,请升级到 CoddyKit PRO。 Serverless AWS Lambda Development 课程共包含 4 节课。
「使用 API Gateway 调用 Lambda」这节课中我会学到什么?
配置 API Gateway 作为 Lambda 函数的 HTTP 端点,使其能够处理 Web 请求并构建 RESTful API 你通过在浏览器中直接运行的动手代码来练习 Serverless AWS Lambda Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Serverless AWS Lambda Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Serverless AWS Lambda Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 API Gateway 调用 Lambda」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Serverless AWS Lambda Development 课中编写并运行代码吗?
能。每节 Serverless AWS Lambda Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 事件驱动架构基础
- 使用 API Gateway 调用 Lambda
- S3 与 SQS 事件触发器
- 使用 EventBridge 定时调用