amazon-web-services - 无法创建从时间点快照配置新实例的 CloudFormation RDS 模板

标签 amazon-web-services aws-cloudformation amazon-rds aws-cloudformation-custom-resource

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

但是,我发现在 CloudFormation 模板中提供快照时无法指定数据库名称,因此它始终会将其恢复到原始数据库。

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

编辑 1

来 self 的 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 文档说:

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

所以只需省略这些参数,如下所示:

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

关于amazon-web-services - 无法创建从时间点快照配置新实例的 CloudFormation RDS 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61660524/

相关文章:

aws-cloudformation - 适用于 NAT 实例的 AWS CloudFormation - 在 LaunchConfig 中禁用 SourceDestinationCheck

postgresql - 在 AWS RDS 上更改 PostgreSQL 只读副本的密码是否安全?

mysql - 如何处理MySQL读写分离中的最终一致性问题

mysql - 对大型 MySQL 表的简单查询一开始需要很长时间,之后会快得多

amazon-web-services - 如何在 cdk 部署期间跳过 IAM 更改确认?

python - 如何通过 Python 访问 Amazon DynamoDB?

python - 在 python 中从实例本身获取 AWS EC2 Spot 实例启动时间

amazon-web-services - AWS Elastic MapReduce 和 AWS Kinesis Data Analytics 之间有什么区别?

amazon-web-services - 将 lambda 附加到现有 API GW cloudform

amazon-web-services - 如何通过cloudformation将多个电子邮件地址添加到SNS主题?