0Pricing
AWS for Backend Developers (EC2, S3, RDS, Lambda) · 课时

用于 Lambda 端点的 API Gateway

通过将 AWS Lambda 与 Amazon API Gateway 集成,构建可扩展且安全的 RESTful API,并处理身份验证和请求/响应映射。

用于 Lambda 端点的 API Gateway 是 CoddyKit 上的免费 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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!

常见问题解答

「用于 Lambda 端点的 API Gateway」课时是免费的吗?

是的 — 「用于 Lambda 端点的 API Gateway」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程的其余内容,请升级到 CoddyKit PRO。 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程共包含 4 节课。

「用于 Lambda 端点的 API Gateway」这节课中我会学到什么?

通过将 AWS Lambda 与 Amazon API Gateway 集成,构建可扩展且安全的 RESTful API,并处理身份验证和请求/响应映射。 你通过在浏览器中直接运行的动手代码来练习 AWS for Backend Developers (EC2, S3, RDS, Lambda),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 AWS for Backend Developers (EC2, S3, RDS, Lambda) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「用于 Lambda 端点的 API Gateway」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课中编写并运行代码吗?

能。每节 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 异步调用 Lambda
  2. Lambda 层与环境变量
  3. 用于 Lambda 端点的 API Gateway
  4. 使用 Step Functions 编排 Lambda
← 返回 AWS for Backend Developers (EC2, S3, RDS, Lambda)