Serverless Backend with AWS Lambda & API Gateway · บทเรียน

การทดสอบและแก้ไขข้อบกพร่องภายในเครื่องด้วย SAM CLI

เรียกใช้และแก้ไขข้อบกพร่องของฟังก์ชันแบบ Serverless บนเครื่องของคุณเองก่อนนำไปใช้งาน ใช้ sam local เพื่อเรียกใช้ฟังก์ชัน จำลอง API Gateway และปรับแก้ได้อย่างรวดเร็ว

บทเรียน 4 จาก 413 ขั้นตอน

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

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

Why Test Locally?

Deploying to AWS just to test a change is slow. The SAM CLI emulates Lambda and API Gateway locally using Docker, so you can iterate in seconds.

Prerequisites

Local invocation needs:

  • Docker running
  • The SAM CLI installed
  • A valid template.yaml

Invoking a Single Function

Run one function with a test event payload.

sam local invoke HelloFunction --event events/event.json

Generating Sample Events

SAM can generate realistic event payloads for many AWS sources, saving you from writing them by hand.

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

Emulating the API

Start a local HTTP server that mimics API Gateway, routing requests to your functions.

sam local start-api
# then: curl http://localhost:3000/hello

Environment Variables Locally

Supply env vars via a JSON file so local runs match what the function expects.

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

Hot Reload During Development

SAM watches your source: when you save a change, the next request uses the new code without restarting the local API, keeping the feedback loop tight.

Step Debugging

Attach a debugger by passing a debug port. Your IDE connects to it so you can set breakpoints inside the running container.

sam local invoke HelloFunction -d 5858

Validating the Template

Catch template errors before deploying with a quick validation command.

sam validate --lint

Viewing Logs

After a deploy you can tail CloudWatch logs from the terminal, filtering by function.

sam logs -n HelloFunction --tail

Limitations of Local Emulation

Local mode is great for logic, but it cannot perfectly mirror IAM permissions, real network latency, or managed services like DynamoDB. Always run an integration test in a real AWS stage before production.

Quick Check

Test your SAM CLI knowledge.

Recap

You learned local development with SAM:

  • sam local invoke runs a single function
  • sam local start-api emulates API Gateway
  • Generate events, supply env vars, and attach a debugger
  • sam validate catches template errors early
  • Local mode has limits — verify in a real stage before prod
เริ่มต้นได้ฟรี

เรียนรู้ Serverless Backend with AWS Lambda & API Gateway ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

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

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

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

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

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

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

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

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

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

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

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

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

  1. บทนำสู่ AWS SAM
  2. การกำหนดทรัพยากรไร้เซิร์ฟเวอร์
  3. การนำแอปพลิเคชัน SAM ไปใช้งาน
  4. การทดสอบและแก้ไขข้อบกพร่องภายในเครื่องด้วย SAM CLI
← กลับไปที่ Serverless Backend with AWS Lambda & API Gateway