You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何拆分大型AWS SAM模板为小型可管理文件?

拆分大型AWS SAM模板的最佳方法与实践

1. 原生YAML文件引用(对应Serverless Framework的${file}方式)

AWS SAM完全支持通过!Include指令实现和Serverless Framework文件引用一致的拆分逻辑,适合将单个资源类型(如函数、API配置)拆分到独立文件。

主模板示例(template.yaml)

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 大规模无服务器应用主模板

Resources:
  # 引用外部业务模块的函数定义
  !Include ./services/authentication/functions.yaml
  !Include ./services/posts/functions.yaml

子模板示例(./services/authentication/functions.yaml)

AuthGetExampleFunction:
  Type: AWS::Serverless::Function
  Properties:
    PackageType: Image
    ImageUri: latest
    ImageConfig:
      Command: ["example_service/app.lambda_handler"]
      EntryPoint: ["/lambda-entrypoint.sh"]
    Events:
      GetExampleApi:
        Type: Api
        Properties:
          Path: /example/get
          Method: get
          Cors: true
          Authorizer: !Ref AuthAuthorizer

!Include会直接将外部YAML内容嵌入主模板对应位置,和你之前用Serverless Framework的方式逻辑完全匹配。

2. 嵌套栈(Nested Stacks)实现高内聚模块化

如果应用按业务域划分清晰(如认证、帖子、支付模块),嵌套栈是更适合的方案——每个模块作为独立CloudFormation栈,支持单独部署、权限隔离和资源边界划分。

实现步骤

  1. 为每个业务模块创建独立SAM子模板,比如authentication-stack.yaml
  2. 在主模板中通过AWS::CloudFormation::Stack引入子栈:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 主应用栈 - 管理所有嵌套子栈

Parameters:
  Environment:
    Type: String
    Default: prod

Resources:
  AuthenticationStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: ./services/authentication/authentication-stack.yaml
      Parameters:
        Environment: !Ref Environment

  PostsStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: ./services/posts/posts-stack.yaml
      Parameters:
        Environment: !Ref Environment
        # 引用认证栈输出的资源
        AuthRoleArn: !GetAtt AuthenticationStack.Outputs.AuthRoleArn
  1. 子栈通过Outputs暴露共享资源,供主栈或其他子栈调用:
# authentication-stack.yaml 末尾添加输出
Outputs:
  AuthRoleArn:
    Value: !GetAtt AuthExecutionRole.Arn
    Export:
      Name: !Sub "${Environment}-AuthRoleArn"

核心优势

  • 子栈独立部署,更新某模块不会影响其他业务域
  • 资源权限可按栈隔离,适合团队分工开发
  • 支持跨栈资源引用,保证模块间依赖清晰

3. 多应用组合管理(类似Serverless Compose)

如果需要管理多个独立的SAM应用(而非单个应用内的模块),可以通过以下两种方式实现类似Serverless Compose的效果:

方式1:SAM CLI批量部署脚本

编写Shell脚本批量部署各模块,控制部署顺序和参数:

#!/bin/bash
# 部署认证模块
sam deploy --template-file ./services/authentication/template.yaml \
  --stack-name prod-auth-service \
  --parameter-overrides Environment=prod \
  --no-fail-on-empty-changeset

# 部署帖子模块(依赖认证模块输出)
sam deploy --template-file ./services/posts/template.yaml \
  --stack-name prod-posts-service \
  --parameter-overrides Environment=prod AuthRoleArn=$(aws cloudformation describe-stacks --stack-name prod-auth-service --query "Stacks[0].Outputs[?OutputKey=='AuthRoleArn'].OutputValue" --output text) \
  --no-fail-on-empty-changeset

方式2:AWS CDK整合SAM模板

用CDK代码统一管理多个SAM模板的部署依赖和顺序,支持更灵活的编排:

import * as cdk from 'aws-cdk-lib';
import { CfnInclude } from 'aws-cdk-lib/cloudformation-include';

const app = new cdk.App();
const mainStack = new cdk.Stack(app, 'ProdMainStack');

// 导入认证模块SAM模板
const authTemplate = new CfnInclude(mainStack, 'AuthModule', {
  templateFile: './services/authentication/template.yaml',
  parameters: {
    Environment: 'prod'
  }
});

// 导入帖子模块SAM模板,依赖认证模块输出
new CfnInclude(mainStack, 'PostsModule', {
  templateFile: './services/posts/template.yaml',
  parameters: {
    Environment: 'prod',
    AuthRoleArn: authTemplate.getOutput('AuthRoleArn')
  }
});

4. 保持整体结构的最佳实践

  • 按业务域拆分:将同一业务逻辑的资源(如认证函数、用户表、授权器)放在同一子模板/子栈,避免跨域拆分
  • 统一命名规范:资源、栈、参数使用统一前缀(如Prod-Auth-Function、Staging-Posts-Table),便于识别归属
  • 抽离共享配置:把环境变量、VPC配置、日志级别等通用配置放在单独的config.yaml,通过!Include或参数传递给各模块
  • 文档化依赖:在每个子模板头部添加注释,说明模块依赖的外部资源和输出内容,降低协作成本
  • 定期验证模板:用sam validate命令验证拆分后的模板有效性,提前发现语法错误

内容的提问来源于stack exchange,提问作者Chapi Menge

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 20:49:57