Sub topic 12
Deploying repeatably with AWS SAM
This chapter assumes you can write a Python handler, read basic YAML, and have the AWS CLI configured with credentials. It also assumes you know what an IAM role is. You will define a function, its permissions, its trigger and its table in one template, then deploy it the same way every time.
Why a template instead of console clicks
Creating resources by hand works once. It is hard to repeat, review or roll back. Infrastructure as code (IaC) describes resources in a file you keep in version control. The AWS Serverless Application Model (SAM) is a shorthand on top of CloudFormation. A SAM template is a CloudFormation template with a Transform line. At deploy time, CloudFormation expands the short resource types into full ones: the function, an IAM role, an API, event source permissions and so on.
The template
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
OrdersFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
CodeUri: src/
MemorySize: 256
Timeout: 10
Environment:
Variables:
TABLE_NAME: !Ref OrdersTable
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref OrdersTable
Events:
CreateOrder:
Type: HttpApi
Properties:
Path: /orders
Method: post
OrdersTable:
Type: AWS::Serverless::SimpleTableEach part maps to an earlier chapter:
| Template field | What it sets |
|---|---|
| `Handler`, `Runtime`, `CodeUri` | Which file and function run, and where the code lives (`src/app.py`) |
| `MemorySize`, `Timeout` | Memory in MB and the maximum run time in seconds |
| `Environment` | Variables the handler reads. `!Ref OrdersTable` passes the table name. |
| `Policies` | A SAM policy template that generates a least-privilege execution role limited to this table |
| `Events` | The trigger. `HttpApi` creates an HTTP API route and the permission for it to invoke the function. |
Referencing the table with !Ref has a second effect. CloudFormation sees the dependency and creates the table before the function.
Build and deploy
Put the handler from the earlier chapter in src/app.py, and the template in template.yaml. Then run:
sam build
sam deploy --guidedsam build resolves dependencies and copies your code into a .aws-sam/build folder. sam deploy --guided asks for a stack name, region and whether to let SAM create IAM roles. It saves your answers in samconfig.toml. Later deployments need only sam build && sam deploy.
The deployment flow is easier to see as a diagram.
What happens during a deploy
sam deploy uploads the build artifacts to an S3 bucket, then asks CloudFormation to create or update the stack. CloudFormation computes a change set, which is the list of resources to add, modify or replace, and applies it. If any step fails, CloudFormation rolls the stack back to its previous state. That rollback is what makes a deploy repeatable. You end up with either the new version or the old one, not a mix.
Common errors
- Wrong
CodeUriorHandler. The function deploys, then fails with an import error at invocation.Handler: app.handlermeans fileapp.py, functionhandler. - Skipping
sam build. SAM then packages your source folder as is, and dependencies fromrequirements.txtare missing. - Missing capability. A template that creates IAM roles needs
CAPABILITY_IAM.--guidedsets this for you. In scripts, pass--capabilities CAPABILITY_IAM. - Editing resources in the console. Changes made outside the template are overwritten, or cause drift, the next time you deploy.
- Stack stuck in
ROLLBACK_COMPLETE. A failed first deploy leaves a stack you must delete before you can retry.
Testing the deployed API
After deployment, the stack outputs include the API endpoint. You can also run the function locally with sam local invoke (this needs Docker) or remove everything with sam delete.
Exercise
You changed the function's Timeout in the AWS console to 30 seconds, but template.yaml still says 10. What happens at the next sam deploy, and how do you avoid it?
Show answer
CloudFormation does not know about the console change, so the deploy sets the timeout back to the template's value of 10 (or, if the property is unchanged in the template, it may leave the drift in place). To avoid this, make configuration changes in template.yaml, review the change set, and deploy, so the template stays the single source of truth.