Testes Locais com SAM CLI
Aprenda a invocar, emular e depurar localmente seu aplicativo sem servidor com o AWS SAM CLI antes de implantá-lo na nuvem.
Testes Locais com SAM CLI é uma aula grátis de Serverless AWS Lambda Development no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Serverless AWS Lambda Development, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Serverless AWS Lambda Development inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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.
Perguntas Frequentes
A aula “Testes Locais com SAM CLI” é grátis?
Sim — o texto completo de “Testes Locais com SAM CLI” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Serverless AWS Lambda Development, atualize para CoddyKit PRO. O curso de Serverless AWS Lambda Development inclui 4 aulas no total.
O que vou aprender em “Testes Locais com SAM CLI”?
Aprenda a invocar, emular e depurar localmente seu aplicativo sem servidor com o AWS SAM CLI antes de implantá-lo na nuvem. Você pratica Serverless AWS Lambda Development com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Serverless AWS Lambda Development?
Nenhuma experiência prévia é necessária. Serverless AWS Lambda Development no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Testes Locais com SAM CLI”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Serverless AWS Lambda Development?
Sim. Cada aula de Serverless AWS Lambda Development inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Modelo de aplicativo sem servidor da AWS (SAM)
- Introdução ao Serverless Framework
- CI/CD para aplicativos sem servidor
- Testes Locais com SAM CLI