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

การทดสอบภายในเครื่องด้วย SAM CLI

เรียนรู้การเรียกใช้ จำลอง และแก้ไขข้อบกพร่องของแอปพลิเคชันแบบไร้เซิร์ฟเวอร์ภายในเครื่องด้วย AWS SAM CLI ก่อนนำไปใช้งานบนคลาวด์

การทดสอบภายในเครื่องด้วย SAM CLI เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless AWS Lambda Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Test Locally?

Deploying to test every change is slow. The SAM CLI emulates Lambda and API Gateway on your machine for a fast feedback loop.

What You Need

Local emulation runs functions inside Docker containers that mirror the Lambda runtime, so you need Docker installed alongside the SAM CLI.

Invoking a Single Function

Use sam local invoke to run one function once with a test event, just like a single Lambda execution.

sam local invoke OrdersFunction \
  --event events/order.json

A Test Event File

The event file is the JSON payload your handler receives. Keep a folder of sample events for different scenarios.

{
  "body": "{\"item\":\"book\",\"qty\":2}",
  "httpMethod": "POST"
}

Generating Sample Events

SAM can scaffold realistic events for common sources so you do not handcraft them.

sam local generate-event s3 put > events/s3.json
sam local generate-event apigateway aws-proxy > events/api.json

Running a Local API

sam local start-api stands up a local HTTP server that routes requests to your functions, mimicking API Gateway.

sam local start-api --port 3000

Calling the Local API

Once running, hit it with any HTTP client. The response comes from your real handler code.

curl -X POST http://localhost:3000/orders \
  -d '{"item":"book","qty":2}'

Environment Variables Locally

Pass a JSON file of environment variable overrides so local runs point at test resources instead of production.

sam local invoke OrdersFunction \
  --env-vars env.json

Step-Through Debugging

Start with -d to pause for a debugger to attach on a port, letting you set breakpoints in your function code.

sam local invoke OrdersFunction -d 5858

Limits of Local Emulation

Local mode cannot perfectly reproduce IAM, networking, or cold starts. Treat it as a fast first check, not a replacement for a cloud staging test.

Validate the Template

Before deploying, run sam validate to catch template errors early.

sam validate --lint

Quick Check

Test your local-testing knowledge.

Recap

You learned to invoke functions, generate events, run a local API, inject env vars, and attach a debugger with the SAM CLI, plus its emulation limits.

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

บทเรียน “การทดสอบภายในเครื่องด้วย SAM CLI” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบภายในเครื่องด้วย SAM CLI”

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

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

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

บทเรียน “การทดสอบภายในเครื่องด้วย SAM CLI” ใช้เวลานานแค่ไหน

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

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

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

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

  1. โมเดลแอปพลิเคชันแบบไร้เซิร์ฟเวอร์ของ AWS (SAM)
  2. บทนำสู่เฟรมเวิร์กแบบไร้เซิร์ฟเวอร์
  3. CI/CD สำหรับแอปแบบไร้เซิร์ฟเวอร์
  4. การทดสอบภายในเครื่องด้วย SAM CLI
← กลับไปที่ Serverless AWS Lambda Development