Files
nexus/wiki/entities/SAM-Serverless-Application-Model.md

87 lines
2.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "SAM Serverless Application Model"
type: entity
tags:
- AWS
- Serverless
- IaC
- CloudFormation
sources:
- public-cloud-learning-sessions-opentext-serverless-computing-20240903-160139-mee
last_updated: 2026-04-14
---
## Aliases
- SAM
- AWS SAM
- Serverless Application Model
## Definition
AWS SAMServerless Application Model是 AWS 官方的开源 IaC 工具,基于 AWS CloudFormation 构建专门简化无服务器应用Lambda、API Gateway、Step Functions 等的定义、部署和管理。SAM 提供简化的 YAML 语法,降低 CloudFormation 模板的复杂度,同时支持本地开发和测试。
## Core Properties
| 属性 | 值 |
|------|-----|
| 基础 | AWS CloudFormation |
| 配置格式 | YAML简化语法 |
| CLI | AWS SAM CLI支持本地调用和测试 |
| 本地测试 | SAM Local — 本地启动 API Gateway + Lambda |
| 部署 | `sam deploy``sam build``sam package` |
| 应用发布 | AWS Serverless Application Repository应用市场 |
## SAM vs CloudFormation
| 特性 | SAM | CloudFormation |
|------|-----|----------------|
| 语法 | 简化 YAML | JSON/YAML |
| 资源类型 | 仅 Serverless 资源 | 全部 AWS 资源 |
| 本地测试 | SAM Local | 不支持 |
| 打包上传 | `sam package` | `aws cloudformation package` |
| 模板继承 | `!Sub``!Ref` | 原生支持 |
## Typical SAM Template
```yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
Events:
ApiEvent:
Type: Api
Properties:
Path: /hello
Method: get
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
DefinitionBody:
# OpenAPI spec
```
## SAM CLI 常用命令
| 命令 | 作用 |
|------|------|
| `sam init` | 初始化新 SAM 项目 |
| `sam build` | 构建应用处理依赖、Layer |
| `sam local invoke` | 本地调用 Lambda 函数 |
| `sam local start-api` | 本地启动完整 APIAPI Gateway + Lambda |
| `sam deploy` | 交互式部署到 AWS |
| `sam package` | 打包模板和代码到 S3 |
| `sam validate` | 验证模板语法 |
## Connections
- [[SAM-Serverless-Application-Model]] ← builds_on ← [[CloudFormation]]
- [[SAM-Serverless-Application-Model]] ← deploys ← [[AWS-Lambda]]
- [[SAM-Serverless-Application-Model]] ← deploys ← [[Amazon-API-Gateway]]
- [[SAM-Serverless-Application-Model]] ← deploys ← [[AWS-Step-Functions]]
- [[SAM-Serverless-Application-Model]] ← is_tool_of ← [[Serverless-Computing]]