Skip to content
Star
478
Star
478

Lambda (SAM)

This guide shows you how to work with AWS Lambda using the Serverless Application Model (SAM) and Python. This example demonstrates how to build and run a local API Gateway and Lambda service for direct SDK invocation.

  1. Setup Environment: Run the setup script to install tools and dependencies.

    Terminal window
    scripts/setup.sh
  2. Start Infrastructure: Launch both SAM local services (API Gateway and Lambda) in separate terminals.

    • API Gateway: to use HTTP requests or cURL:

      Terminal window
      sam local start-api
    • Lambda SDK: to use the AWS SDK (boto3) or AWS CLI:

      Terminal window
      sam local start-lambda
  3. Run the Example:

    Terminal window
    python main.py

Use the preconfigured VS Code debug profiles:

  1. Python Script (main.py):

    • Ensure the local SAM services are running (sam local start-api and sam local start-lambda).
    • Run the Python: Main debug configuration.
  2. Lambda Function:

    • Run the SAM: Debug get_secret debug configuration.
  • All tests: Run the automated script:
    Terminal window
    scripts/run_tests.sh
  • Individually: Use the VS Code Testing tab to run or debug specific test cases.
  • Logs: Check the console output in the terminals where sam local is running to verify invocation details and log messages.
  • AWS CLI Output: Verify that the generated output.json contains the expected HTTP response body and status code.
  1. Stop the active SAM local services in your terminals.
  2. Remove the virtual environment and uninstalled tools:
    Terminal window
    rm -rf .venv
    mise uninstall -a