AWS SAM template file closely follows CloudFormation template format với primary differences:
Transform: AWS::Serverless-2016-10-31 (required)Transform: AWS::Serverless-2016-10-31
Hoặc với language extensions:
Transform:
- AWS::LanguageExtensions
- AWS::Serverless-2016-10-31
Lưu ý: AWS::LanguageExtensions phải đặt trước serverless transform.
Properties common to all serverless functions, APIs, tables. Các resources inherit properties từ Globals:
AWS::Serverless::FunctionAWS::Serverless::ApiAWS::Serverless::HttpApiAWS::Serverless::SimpleTableAWS::Serverless::StateMachineAWS::Serverless::CapacityProviderGlobals:
Function:
Runtime: python3.12
Timeout: 30
Environment:
Variables:
LOG_LEVEL: INFO
Api:
Cors:
AllowOrigin: "'*'"
AllowHeaders: "'*'"
Stack resources - combination of:
Resources:
MyFunction:
Type: AWS::Serverless::Function # SAM resource
Properties:
Handler: app.handler
Runtime: python3.12
MyTable:
Type: AWS::DynamoDB::Table # CloudFormation resource
Properties:
TableName: orders
| Section | Purpose |
|---|---|
| Description | Text describing template |
| Metadata | Additional template information |
| Parameters | Runtime values (prompts in sam deploy --guided) |
| Mappings | Key-value lookup table |
| Conditions | Control resource creation |
| Outputs | Values returned when viewing stack |
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Runtime: python3.12
Timeout: 30
Tracing: Active
Description: Order processing application
Metadata:
AWS::ServerlessRepo::Application:
Name: order-app
Description: Process orders
Parameters:
Stage:
Type: String
AllowedValues: [dev, staging, prod]
Default: dev
Mappings:
StageConfig:
dev:
MemorySize: 128
prod:
MemorySize: 512
Conditions:
IsProd: !Equals [!Ref Stage, prod]
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
CodeUri: src/
MemorySize: !FindInMap [StageConfig, !Ref Stage, MemorySize]
Environment:
Variables:
TABLE_NAME: !Ref MyTable
STAGE: !Ref Stage
Events:
Api:
Type: Api
Properties:
Path: /orders
Method: get
MyTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "orders-${Stage}"
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
Outputs:
ApiUrl:
Description: API Gateway endpoint URL
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/orders"
FunctionArn:
Description: Lambda Function ARN
Value: !GetAtt MyFunction.Arn
| Function | Use | Example |
|---|---|---|
!Ref | Reference parameter/resource | !Ref MyTable |
!Sub | String substitution | !Sub "orders-${Stage}" |
!GetAtt | Resource attribute | !GetAtt MyFunction.Arn |
!If | Conditional value | !If [IsProd, 512, 128] |
!ImportValue | Cross-stack reference | !ImportValue VpcId |
!FindInMap | Lookup in Mappings | !FindInMap [StageConfig, dev, MemorySize] |
!Join | Join strings | !Join ['-', [orders, !Ref Stage]] |
sam deploy --parameter-overrides (highest)samconfig.toml)# Build
sam build
# Deploy with prompts (first time)
sam deploy --guided
# Deploy with parameters
sam deploy --parameter-overrides Stage=prod
# Deploy from config
sam deploy --config-file samconfig.toml
Recommended logical order:
Transform (required)Globals (optional)Description (optional)Metadata (optional)Parameters (optional)Mappings (optional)Conditions (optional)Resources (required)Outputs (optional)Exam Tip:
AWS::Serverless-2016-10-31 (required)sam deploy --guided!Ref for IDs, !GetAtt for attributes, !Sub for string interpolation