Serverless Backend with AWS Lambda & API Gateway · Lezione

Test e debugging locali con SAM CLI

Esegua ed esegua il debug delle funzioni serverless sul proprio computer prima del deployment. Usi sam local per invocare le funzioni, emulare API Gateway e iterare rapidamente.

Lezione 4 di 413 passaggi

Test e debugging locali con SAM CLI è una lezione Serverless Backend with AWS Lambda & API Gateway gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Serverless Backend with AWS Lambda & API Gateway, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Serverless Backend with AWS Lambda & API Gateway include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.json

Generating 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.json

Emulating 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/hello

Environment Variables Locally

Supply env vars via a JSON file so local runs match what the function expects.

sam local invoke HelloFunction \
  --env-vars env.json

Hot 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 5858

Validating the Template

Catch template errors before deploying with a quick validation command.

sam validate --lint

Viewing Logs

After a deploy you can tail CloudWatch logs from the terminal, filtering by function.

sam logs -n HelloFunction --tail

Limitations 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 invoke runs a single function
  • sam local start-api emulates API Gateway
  • Generate events, supply env vars, and attach a debugger
  • sam validate catches template errors early
  • Local mode has limits — verify in a real stage before prod
Gratis per iniziare

Impara Serverless Backend with AWS Lambda & API Gateway con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Test e debugging locali con SAM CLI» è gratuita?

Sì — il testo completo di «Test e debugging locali con SAM CLI» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Serverless Backend with AWS Lambda & API Gateway, passa a CoddyKit PRO. Il corso Serverless Backend with AWS Lambda & API Gateway include 4 lezioni in totale.

Cosa imparerò in «Test e debugging locali con SAM CLI»?

Esegua ed esegua il debug delle funzioni serverless sul proprio computer prima del deployment. Usi sam local per invocare le funzioni, emulare API Gateway e iterare rapidamente. Eserciti Serverless Backend with AWS Lambda & API Gateway con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Serverless Backend with AWS Lambda & API Gateway?

Non è richiesta alcuna esperienza precedente. Serverless Backend with AWS Lambda & API Gateway su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Test e debugging locali con SAM CLI»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Serverless Backend with AWS Lambda & API Gateway?

Sì. Ogni lezione Serverless Backend with AWS Lambda & API Gateway include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Introduzione ad AWS SAM
  2. Definizione delle risorse serverless
  3. Distribuzione delle applicazioni SAM
  4. Test e debugging locali con SAM CLI
← Torna a Serverless Backend with AWS Lambda & API Gateway