使用 SAM CLI 进行本地测试
学习在部署到云端之前,如何使用 AWS SAM CLI 在本地调用、模拟和调试无服务器应用。
使用 SAM CLI 进行本地测试 是 CoddyKit 上的免费 Serverless AWS Lambda Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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 进行本地测试」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Serverless AWS Lambda Development 课程的其余内容,请升级到 CoddyKit PRO。 Serverless AWS Lambda Development 课程共包含 4 节课。
「使用 SAM CLI 进行本地测试」这节课中我会学到什么?
学习在部署到云端之前,如何使用 AWS SAM CLI 在本地调用、模拟和调试无服务器应用。 你通过在浏览器中直接运行的动手代码来练习 Serverless AWS Lambda Development,全天候 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 反馈 — 无需本地设置。
此课程中的所有课时
- AWS 无服务器应用模型(SAM)
- 无服务器框架入门
- 无服务器应用的持续集成/持续交付
- 使用 SAM CLI 进行本地测试