1

我正在尝试编写一个 CloudFormation 来从现有 RDS 数据库的时间点快照配置一个新的 RDS 实例。

但是,我知道在 CloudFormation 模板中提供快照时不能指定 db-name,因此它将始终将其还原到原始数据库。

我在 aws 博客上也有同样的文章,不过我正在寻找是否有任何开箱即用的解决方案。

编辑 1

我的 Cloud Formation 中的 RDS 片段

Resources:
  MyDB:
    Type: AWS::RDS::DBInstance
    Properties:
      DBName: Fn::If ["UseDbSnapshot", !Ref AWS:NoValue, !Ref MyDBName]
      DBSecurityGroups:
      - !Ref MyDbSecurityByEC2SecurityGroup
      - !Ref MyDbSecurityByCIDRIPGroup
      AllocatedStorage: 20
      DBInstanceClass: db.m1.small
      Engine: MySQL
      MasterUsername: root
      MasterUserPassword: password
      DBSnapshotIdentifier: Fn::If ["UseDbSnapshot", !Ref DBSnapshotIdentifier, !Ref AWS::NoValue]
    DeletionPolicy: Snapshot

我可以尝试什么来解决这个问题?

4

1 回答 1

0

You have to keep in mind, that a couple of RDS properties (such as MasterUsername) are not customizable when you want to restore from snapshot. The AWS documentation says:

If you specify the SourceDBInstanceIdentifier or DBSnapshotIdentifier property, don't specify this property. The value is inherited from the source DB instance or snapshot.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-rds-database-instance.html

So just omit those parameters like this:

Parameters:
  DBSnapshotIdentifier:
    Description: 'Optional identifier for the DB cluster snapshot from which you want to restore'
    Type: String
    Default: ''

Conditions:
  HasDBSnapshotIdentifier: !Not [!Equals [!Ref DBSnapshotIdentifier, '']]


Resources:
  DBInstance:
    Type: AWS::RDS::DBInstance
    Properties:
      ...
      DBSnapshotIdentifier: !If [HasDBSnapshotIdentifier, !Ref DBSnapshotIdentifier, !Ref 'AWS::NoValue']
      KmsKeyId: !If [HasDBSnapshotIdentifier, !Ref 'AWS::NoValue', !If [HasEncryption, !Ref KmsKey, !Ref 'AWS::NoValue']]
      MasterUsername: !If [HasDBSnapshotIdentifier, !Ref 'AWS::NoValue', !Ref MasterUsername]
      MasterUserPassword: !If [HasDBSnapshotIdentifier, !Ref 'AWS::NoValue', !Ref MasterPassword]
      StorageEncrypted: !If [HasDBSnapshotIdentifier, !Ref 'AWS::NoValue', !If 
      ...
于 2020-05-08T19:30:05.103 回答