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

求满足RDS实例快照重建及动态配置的CloudFormation模板

CloudFormation RDS 实例模板(支持删除留快照+复用快照创建)

以下是满足需求的CloudFormation YAML模板,核心通过DeletionPolicy: Snapshot实现删除栈时保留快照,同时通过可选参数支持首次无快照创建、后续复用快照恢复:

AWSTemplateFormatVersion: '2010-09-09'
Description: RDS实例模板,支持删除时生成快照,且可复用快照创建实例

Parameters:
  DBSnapshotIdentifier:
    Type: String
    Description: 用于恢复RDS实例的快照ID,首次创建时留空
    Default: ""
  DBInstanceIdentifier:
    Type: String
    Description: RDS实例ID
    Default: my-rds-instance
  DBAllocatedStorage:
    Type: Number
    Description: 存储容量(GB)
    Default: 20
  DBInstanceClass:
    Type: String
    Description: RDS实例规格
    Default: db.t3.micro
  Engine:
    Type: String
    Description: 数据库引擎
    Default: mysql
  MasterUsername:
    Type: String
    Description: 数据库管理员用户名
    Default: admin
  MasterUserPassword:
    Type: String
    Description: 数据库管理员密码
    NoEcho: true

Conditions:
  UseSnapshot: !Not [!Equals [!Ref DBSnapshotIdentifier, ""]]

Resources:
  MyRDSInstance:
    Type: AWS::RDS::DBInstance
    DeletionPolicy: Snapshot
    Properties:
      DBInstanceIdentifier: !Ref DBInstanceIdentifier
      DBInstanceClass: !Ref DBInstanceClass
      Engine: !Ref Engine
      MasterUsername: !If [UseSnapshot, !Ref "AWS::NoValue", !Ref MasterUsername]
      MasterUserPassword: !If [UseSnapshot, !Ref "AWS::NoValue", !Ref MasterUserPassword]
      DBAllocatedStorage: !If [UseSnapshot, !Ref "AWS::NoValue", !Ref DBAllocatedStorage]
      DBSnapshotIdentifier: !If [UseSnapshot, !Ref DBSnapshotIdentifier, !Ref "AWS::NoValue"]

使用说明

  • 首次创建:直接部署模板,保持DBSnapshotIdentifier参数默认空值,CloudFormation会创建全新的RDS实例。
  • 删除栈时:CloudFormation自动为该RDS实例生成快照,可在RDS控制台的“快照”页面找到对应快照ID。
  • 复用快照创建:再次部署模板时,将获取到的快照ID填入DBSnapshotIdentifier参数,CloudFormation会基于该快照恢复RDS实例,此时MasterUsername、MasterUserPassword、DBAllocatedStorage等参数会被忽略(快照已包含这些配置)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 09:02:59