Testes e depuração locais com o SAM CLI
Execute e depure suas funções sem servidor em sua própria máquina antes de implantá-las. Use sam local para invocar funções, emular o API Gateway e iterar rapidamente.
Testes e depuração locais com o SAM CLI é uma aula grátis de Serverless Backend with AWS Lambda & API Gateway 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 Backend with AWS Lambda & API Gateway, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Serverless Backend with AWS Lambda & API Gateway inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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.jsonGenerating 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.jsonEmulating 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/helloEnvironment Variables Locally
Supply env vars via a JSON file so local runs match what the function expects.
sam local invoke HelloFunction \
--env-vars env.jsonHot 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 5858Validating the Template
Catch template errors before deploying with a quick validation command.
sam validate --lintViewing Logs
After a deploy you can tail CloudWatch logs from the terminal, filtering by function.
sam logs -n HelloFunction --tailLimitations 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 invokeruns a single functionsam local start-apiemulates API Gateway- Generate events, supply env vars, and attach a debugger
sam validatecatches template errors early- Local mode has limits — verify in a real stage before prod
Perguntas Frequentes
A aula “Testes e depuração locais com o SAM CLI” é grátis?
Sim — o texto completo de “Testes e depuração locais com o 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 Backend with AWS Lambda & API Gateway, atualize para CoddyKit PRO. O curso de Serverless Backend with AWS Lambda & API Gateway inclui 4 aulas no total.
O que vou aprender em “Testes e depuração locais com o SAM CLI”?
Execute e depure suas funções sem servidor em sua própria máquina antes de implantá-las. Use sam local para invocar funções, emular o API Gateway e iterar rapidamente. Você pratica Serverless Backend with AWS Lambda & API Gateway 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 Backend with AWS Lambda & API Gateway?
Nenhuma experiência prévia é necessária. Serverless Backend with AWS Lambda & API Gateway 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 e depuração locais com o 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 Backend with AWS Lambda & API Gateway?
Sim. Cada aula de Serverless Backend with AWS Lambda & API Gateway 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
- Introdução ao AWS SAM
- Definição de recursos sem servidor
- Implantação de aplicações SAM
- Testes e depuração locais com o SAM CLI