0Pricing
Serverless AWS Lambda Development · Lezione

Test locali con SAM CLI

Impari a invocare, emulare e sottoporre a debug localmente la sua applicazione serverless con AWS SAM CLI prima della distribuzione nel cloud.

Test locali con SAM CLI è una lezione Serverless AWS Lambda Development 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 AWS Lambda Development, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Serverless AWS Lambda Development include 4 lezioni in totale.

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

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

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

Running 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 3000

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

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

Limits 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 --lint

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

Domande Frequenti

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

Sì — il testo completo di «Test 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 AWS Lambda Development, passa a CoddyKit PRO. Il corso Serverless AWS Lambda Development include 4 lezioni in totale.

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

Impari a invocare, emulare e sottoporre a debug localmente la sua applicazione serverless con AWS SAM CLI prima della distribuzione nel cloud. Eserciti Serverless AWS Lambda Development 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 AWS Lambda Development?

Non è richiesta alcuna esperienza precedente. Serverless AWS Lambda Development 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 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 AWS Lambda Development?

Sì. Ogni lezione Serverless AWS Lambda Development 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. AWS Serverless Application Model (SAM)
  2. Introduzione al Serverless Framework
  3. CI/CD per app serverless
  4. Test locali con SAM CLI
← Torna a Serverless AWS Lambda Development