0Pricing
Serverless AWS Lambda Development · บทเรียน

การเรียกใช้ Lambda ด้วย API Gateway

กำหนดค่า API Gateway ให้ทำหน้าที่เป็นจุดปลายทาง HTTP สำหรับฟังก์ชัน Lambda เพื่อรองรับคำขอเว็บและสร้าง API แบบ RESTful

การเรียกใช้ Lambda ด้วย API Gateway เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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., 200 for 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:

  1. Create a new REST API in API Gateway.
  2. Define a Resource (e.g., /hello or /hello/{name} for path parameters).
  3. Add a Method (e.g., GET) to your resource.
  4. Configure the method to use Lambda Proxy Integration, pointing to your Lambda function.
  5. 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!

คำถามที่พบบ่อย

บทเรียน “การเรียกใช้ Lambda ด้วย API Gateway” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเรียกใช้ Lambda ด้วย API Gateway” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเรียกใช้ Lambda ด้วย API Gateway”

กำหนดค่า API Gateway ให้ทำหน้าที่เป็นจุดปลายทาง HTTP สำหรับฟังก์ชัน Lambda เพื่อรองรับคำขอเว็บและสร้าง API แบบ RESTful คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless AWS Lambda Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless AWS Lambda Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การเรียกใช้ Lambda ด้วย API Gateway” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Serverless AWS Lambda Development นี้ได้ไหม

ได้ บทเรียน Serverless AWS Lambda Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. พื้นฐานสถาปัตยกรรมที่ขับเคลื่อนด้วยเหตุการณ์
  2. การเรียกใช้ Lambda ด้วย API Gateway
  3. ตัวกระตุ้นเหตุการณ์ S3 และ SQS
  4. การเรียกใช้ตามกำหนดเวลาด้วย EventBridge
← กลับไปที่ Serverless AWS Lambda Development