SAM templates and local tests
SAM and the serverless transform
SAM is a template specification and CLI for AWS serverless applications. A SAM template extends CloudFormation using shorthand types such as AWS::Serverless::Function. The transform expands those declarations for deployment; generated resources may include IAM roles. A template is not an application process. It describes the function and how AWS should configure it.
A quote calculator template
This static JSON template declares one Python 3.12 Lambda with a five-second timeout and no event trigger. The relative CodeUri points to src/app.py and Handler selects app.handler. No deployed function is needed to test the arithmetic. Runtime availability and tool versions must be verified before an authorized deployment.
{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Resources": { "Quote": { "Type": "AWS::Serverless::Function", "Properties": { "Runtime": "python3.12", "Handler": "app.handler", "CodeUri": "src/", "Timeout": 5, "MemorySize": 128 } } }}Test the handler with synthetic input
The handler calculates a fictional quote in integer cents. For {"units": 3}, it returns {"total_cents": 750}; zero, booleans and nonintegers are rejected. The pure Python test invokes the handler directly. It does not verify Lambda permissions, execution environment or delivery semantics.
def handler(event, context): units = event.get("units") if type(units) is not int or not 1 <= units <= 100: return {"error": "units must be an integer from 1 to 100"} return {"total_cents": units * 250}Build, validate, invoke and deploy
In a prepared sandbox, sam validate checks a template and sam build prepares artifacts. sam local invoke Quote -e event.json invokes the function through a local emulation environment with the required container runtime and image. Local emulation may download images and application code could call real APIs if not isolated. A local pass cannot establish cloud IAM, service quotas or event integration behavior. sam deploy changes AWS resources; review generated roles, artifacts, scope and costs first. None of these CLI commands were executed here.
Practice
Run the included Python tests locally using synthetic inputs only. Add a case for 100 units and an invalid string. Explain why these tests are useful but not sufficient for a real deployment.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
SAM templates and local tests — knowledge check
Review infrastructure artifacts and evidence without deploying resources.
What does the SAM Transform enable?
What is the quote for 3 units in the sample handler?
Which input should be rejected by the handler?
What does a successful direct Python test establish?
Which step is explicitly a real cloud mutation?