mcpField
A
aws-cloudformation-rds

aws-cloudformation-rds

github.com/giuseppe-trisciuoglio云与 DevOps36565 次安装7 周前更新
代码仓库

关于 aws-cloudformation-rds

面向使用 AWS CloudFormation 管理 Amazon RDS 的开发与运维人员,涵盖数据库实例、Aurora 集群、高可用性、网络安全及凭据管理配置。

主要功能

  1. 01RDS 实例与 Aurora 集群
  2. 02多可用区和只读副本
  3. 03子网组与参数组
  4. 04Secrets Manager 凭据集成
  5. 05跨栈引用与变更集

适用场景

  1. 01使用 CloudFormation 创建 RDS 数据库
  2. 02为生产数据库配置多可用区
  3. 03管理数据库凭据与网络访问
  4. 04向应用栈导出数据库连接信息

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill aws-cloudformation-rds

安装后在 Claude Code 中输入/aws-cloudformation-rds确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
constraints.md
database-components.md
examples.md
high-availability.md
operational-practices.md
reference.md
security-secrets.md
template-structure.md

详细介绍

AWS CloudFormation RDS 数据库

概述

使用 AWS CloudFormation 模板创建可用于生产环境的 Amazon RDS 基础设施。涵盖 RDS 实例(MySQL、PostgreSQL、Aurora)、数据库集群、多可用区部署、参数组、子网组、安全组和跨堆栈引用。

适用场景

  • 创建 RDS 实例(MySQL、PostgreSQL、Aurora)或带有只读副本的数据库集群
  • 设置多可用区部署,或配置参数组和子网组
  • 与 Secrets Manager 集成,或实现跨堆栈引用

快速参考

组件CloudFormation 类型用途
数据库实例AWS::RDS::DBInstance单个数据库实例
数据库集群AWS::RDS::DBClusterAurora 集群
数据库子网组AWS::RDS::DBSubnetGroupVPC 部署
参数组AWS::RDS::DBParameterGroup数据库配置
安全组AWS::EC2::SecurityGroup网络访问控制
Secrets ManagerAWS::SecretsManager::Secret凭证存储

操作说明

步骤 1 — 定义数据库参数

使用 AWS 专用参数类型进行验证。

Parameters:
  DBInstanceClass:
    Type: AWS::RDS::DBInstance::InstanceType
    Default: db.t3.micro
    AllowedValues: [db.t3.micro, db.t3.small, db.t3.medium]

  Engine:
    Type: String
    Default: mysql
    AllowedValues: [mysql, postgres, aurora-mysql, aurora-postgresql]

  MasterUsername:
    Type: String
    Default: admin
    AllowedPattern: "^[a-zA-Z][a-zA-Z0-9]*$"
    MinLength: 1
    MaxLength: 16

  MasterUserPassword:
    Type: String
    NoEcho: true
    MinLength: 8
    MaxLength: 41

有关高级参数模式、映射、条件和跨堆栈引用,请参阅 template-structure.md。

步骤 2 — 创建数据库子网组

在包含不同 AZ 子网的 VPC 中部署时必须创建。

DBSubnetGroup:
  Type: AWS::RDS::DBSubnetGroup
  Properties:
    DBSubnetGroupDescription: Subnet group for RDS
    SubnetIds:
      - !Ref PrivateSubnet1
      - !Ref PrivateSubnet2

有关参数组、选项组和引擎专用配置,请参阅 database-components.md。

步骤 3 — 配置安全组

将访问限制为仅应用层。

DBSecurityGroup:
  Type: AWS::EC2::SecurityGroup
  Properties:
    GroupDescription: Security group for RDS
    VpcId: !Ref VpcId
    SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: 3306
        ToPort: 3306
        SourceSecurityGroupId: !Ref AppSecurityGroup

有关 VPC 安全组、加密、Secrets Manager 集成和 IAM 身份验证,请参阅 security-secrets.md。

步骤 4 — 启动 RDS 实例

使用子网组、安全组和相关设置配置实例。

DBInstance:
  Type: AWS::RDS::DBInstance
  Properties:
    DBInstanceIdentifier: !Sub "${AWS::StackName}-mysql"
    DBInstanceClass: !Ref DBInstanceClass
    Engine: !Ref Engine
    MasterUsername: !Ref MasterUsername
    MasterUserPassword: !Ref MasterUserPassword
    AllocatedStorage: 20
    StorageType: gp3
    DBSubnetGroupName: !Ref DBSubnetGroup
    VPCSecurityGroups: [!Ref DBSecurityGroup]
    StorageEncrypted: true
    MultiAZ: true
    BackupRetentionPeriod: 7
    DeletionProtection: false

有关 MySQL、PostgreSQL、Aurora 集群配置和参数组,请参阅 database-components.md。

步骤 5 — 启用高可用性

为生产环境配置多可用区部署。

Conditions:
  IsProduction: !Equals [!Ref Environment, production]

Resources:
  DBInstance:
    Type: AWS::RDS::DBInstance
    Properties:
      MultiAZ: !If [IsProduction, true, false]
      BackupRetentionPeriod: !If [IsProduction, 35, 7]
      DeletionProtection: !If [IsProduction, true, false]
      EnablePerformanceInsights: !If [IsProduction, true, false]

有关多可用区部署、只读副本、Aurora 自动扩缩、增强型监控和灾难恢复,请参阅 high-availability.md。

步骤 6 — 定义输出

导出连接详情,供应用堆栈使用。

Outputs:
  DBInstanceEndpoint:
    Description: Database endpoint address
    Value: !GetAtt DBInstance.Endpoint.Address
    Export:
      Name: !Sub ${AWS::StackName}-DBEndpoint

  DBInstancePort:
    Description: Database port
    Value: !GetAtt DBInstance.Endpoint.Port
    Export:
      Name: !Sub ${AWS::StackName}-DBPort

  DBConnectionString:
    Description: Connection string
    Value: !Sub jdbc:mysql://${DBInstance.Endpoint.Address}:${DBInstance.Endpoint.Port}/${DBName}

有关跨堆栈引用模式和导入/导出策略,请参阅 template-structure.md。

验证步骤

部署前务必进行验证,尤其是在部署到生产环境之前。

# Validate the template syntax
aws cloudformation validate-template --template-body file://template.yaml

# Review the change set before applying updates
aws cloudformation create-change-set \
  --stack-name my-rds-stack \
  --template-body file://template.yaml \
  --change-set-type UPDATE

aws cloudformation describe-change-set --change-set-name <arn>

# Execute the change set if the preview looks correct
aws cloudformation execute-change-set --change-set-name <arn>

最佳实践

类别实践实现方式
安全静态加密使用 KMS 密钥设置 StorageEncrypted: true
安全凭证管理使用 Secrets Manager 集成
安全网络隔离使用私有子网和严格的 SG 规则
安全IAM 身份验证启用 IAMDatabaseAuthentication
高可用性多可用区部署在生产环境中设置 MultiAZ: true
高可用性删除保护在生产环境中设置 DeletionProtection: true
高可用性备份保留期生产环境保留 35 天,开发环境保留 7 天
高可用性只读副本用于读取密集型工作负载
成本存储类型使用 gp3 提高成本效益
成本实例规格根据工作负载选择合适规格
成本无服务器对于负载变化较大的情况,可考虑 Aurora Serverless
运维变更集应用更新前务必检查
运维漂移检测启用以确保符合模板
运维监控配置 CloudWatch 警报

有关堆栈策略、终止保护和备份策略的详细指导,请参阅 operational-practices.md。

示例

包含 MultiAZ、加密和 Secrets Manager 集成的完整生产就绪型 RDS 实例:

AWSTemplateFormatVersion: '2010-09-09'
Description: Production RDS Instance

Parameters:
  VpcId:
    Type: AWS::EC2::VPC::Identifier
  SubnetIds:
    Type: List<AWS::EC2::Subnet::Identifier>
  AppSecurityGroupId:
    Type: AWS::EC2::SecurityGroup::Id
  Environment:
    Type: String
    AllowedValues: [dev, staging, production]
  MasterUsername:
    Type: String
    Default: dbadmin

Conditions:
  IsProduction: !Equals [!Ref Environment, production]

Resources:
  DBSubnetGroup:
    Type: AWS::RDS::DBSubnetGroup
    Properties:
      DBSubnetGroupDescription: !Sub "${AWS::StackName} subnet group"
      SubnetIds: !Ref SubnetIds

  DBSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: !Sub "${AWS::StackName} RDS security group"
      VpcId: !Ref VpcId
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 3306
          ToPort: 3306
          SourceSecurityGroupId: !Ref AppSecurityGroupId

  DBInstance:
    Type: AWS::RDS::DBInstance
    DeletionPolicy: Snapshot
    UpdateReplacePolicy: Snapshot
    Properties:
      DBInstanceIdentifier: !Sub "${AWS::StackName}-mysql"
      DBInstanceClass: db.t3.medium
      Engine: mysql
      EngineVersion: '8.0'
      MasterUsername: !Ref MasterUsername
      MasterUserPassword: !Ref MasterUserPassword
      AllocatedStorage: 50
      StorageType: gp3
      StorageEncrypted: true
      KmsKeyId: !Ref KmsKeyId
      DBSubnetGroupName: !Ref DBSubnetGroup
      VPCSecurityGroups: [!Ref DBSecurityGroup]
      MultiAZ: !If [IsProduction, true, false]
      BackupRetentionPeriod: !If [IsProduction, 35, 7]
      DeletionProtection: !If [IsProduction, true, false]
      EnablePerformanceInsights: !If [IsProduction, true, false]
      PerformanceInsightsRetentionPeriod: !If [IsProduction, 731, 7]

  KmsKeyId:
    Type: AWS::KMS::Key
    Condition: IsProduction
    Properties:
      Description: KMS key for RDS encryption
      EnableKeyRotation: true
      KeyPolicy:
        Version: '2012-10-17'
        Statement:
          - Sid: Enable IAM User Permissions
            Effect: Allow
            Principal:
              AWS: !Sub arn:aws:iam::${AWS::AccountId}:root
            Action: kms:*
            Resource: '*'

Outputs:
  DBEndpoint:
    Description: Database endpoint
    Value: !GetAtt DBInstance.Endpoint.Address
    Export:
      Name: !Sub ${AWS::StackName}-DBEndpoint
  DBPort:
    Description: Database port
    Value: !GetAtt DBInstance.Endpoint.Port
    Export:
      Name: !Sub ${AWS::StackName}-DBPort

请参阅 examples.md,了解更多示例,包括 Aurora 集群、只读副本和多区域配置。

参考资料

核心配置
运维指南
其他资源

约束和警告

资源限制
  • 最大存储容量因引擎而异(MySQL/PostgreSQL 最高为 64 TB)
  • 每个 CloudFormation 堆栈最多包含 500 个资源
  • 参数组限制因账户和区域而异
成本考量
  • Multi-AZ 部署的成本约为单可用区部署的两倍
  • 预置 IOPS(io1)会显著增加成本
  • 超出免费额度的备份存储会产生月度费用
  • 即使删除实例,手动快照仍会产生存储费用
安全约束
  • 创建后无法检索主密码
  • 启用静态加密后无法将其禁用
  • RDS 实例必须位于 VPC 中(不建议公开访问)
  • 安全组规则必须限制对应用层的访问
运维约束
  • 某些修改(引擎版本、存储类型)需要替换实例,并会造成停机
  • 维护时段可能导致短暂的服务中断
  • 只读副本可能落后于主实例数秒至数分钟
  • 并非所有数据库引擎都可在所有区域使用

请参阅 constraints.md,了解完整的约束、故障排除指南和性能考量。

评论

支持 Markdown