SAM CLIによるローカルテスト
AWS SAM CLIを使って、クラウドにデプロイする前にサーバーレスアプリケーションをローカルで呼び出し、エミュレートし、デバッグする方法を学びます。
「SAM CLIによるローカルテスト」はCoddyKit上の無料Serverless AWS Lambda Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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.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.
よくある質問
「SAM CLIによるローカルテスト」レッスンは無料ですか?
はい。「SAM CLIによるローカルテスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Serverless AWS Lambda Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Serverless AWS Lambda Developmentコースには全4レッスンが含まれています。
「SAM CLIによるローカルテスト」で何を学びますか?
AWS SAM CLIを使って、クラウドにデプロイする前にサーバーレスアプリケーションをローカルで呼び出し、エミュレートし、デバッグする方法を学びます。 ブラウザで直接実行するハンズオンコードでServerless AWS Lambda Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Serverless AWS Lambda Developmentを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのServerless AWS Lambda Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「SAM CLIによるローカルテスト」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このServerless AWS Lambda Developmentレッスンでコードを書いて実行できますか?
はい。すべてのServerless AWS Lambda Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。