Pengujian Lokal dengan SAM CLI
Pelajari cara memanggil, meniru, dan men-debug aplikasi tanpa server secara lokal dengan AWS SAM CLI sebelum menerapkannya ke cloud.
Pengujian Lokal dengan SAM CLI adalah pelajaran Serverless AWS Lambda Development gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Serverless AWS Lambda Development, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Serverless AWS Lambda Development mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.jsonA 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.jsonRunning 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 3000Calling 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.jsonStep-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 5858Limits 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 --lintQuick 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.
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Pengujian Lokal dengan SAM CLI” gratis?
Ya — teks lengkap “Pengujian Lokal dengan SAM CLI” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Serverless AWS Lambda Development, upgrade ke CoddyKit PRO. Kursus Serverless AWS Lambda Development mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Pengujian Lokal dengan SAM CLI”?
Pelajari cara memanggil, meniru, dan men-debug aplikasi tanpa server secara lokal dengan AWS SAM CLI sebelum menerapkannya ke cloud. Kamu berlatih Serverless AWS Lambda Development dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Serverless AWS Lambda Development?
Tidak diperlukan pengalaman sebelumnya. Serverless AWS Lambda Development di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.
Berapa lama pelajaran “Pengujian Lokal dengan SAM CLI” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Serverless AWS Lambda Development ini?
Ya. Setiap pelajaran Serverless AWS Lambda Development menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- AWS Serverless Application Model (SAM)
- Pengantar Serverless Framework
- CI/CD untuk Aplikasi Tanpa Server
- Pengujian Lokal dengan SAM CLI