无法创建从时间点快照提供新实例的CoudFormation RDS模板



我试图编写一个CloudFormation,从现有RDS数据库的时间点快照中提供一个新的RDS实例。

然而,我知道当你在CloudFormation模板中提供快照时,你不能指定数据库名称,因此它总是会将其恢复到原始数据库。

我在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

我该怎么解决这个问题?

您必须记住,当您想要从快照恢复时,有几个RDS属性(如MasterUsername(是不可自定义的。AWS文档中写道:

如果指定SourceDBInstanceIdentifier或DBSnapshotIdentifier属性,请不要指定此属性。该值是从源数据库实例或快照继承的。

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

因此,只需省略这些参数:

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 
...

最新更新