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/

相关文章:

amazon-web-services - AWS Glue Crawler 覆盖自定义表属性

amazon-web-services - 更新堆栈中的 CloudFormation 等待条件

aws-cloudformation - CloudFormation 循环依赖

postgresql - 在 Postgres 中创建指向同一数据库的 db_link

amazon-web-services - 使用 cloudformation 创建 FIFO SQS 队列

amazon-web-services - 我检测到一些针对我的 Web 应用程序的黑客攻击。现在怎么办?

r - 如何使用 IAM 角色身份验证从 EC2 上的 RStudio 访问 S3 数据?

amazon-web-services - VPC 中的 AWS Lambda 无法通过 NAT 访问互联网

mysql - 与 Amazon RDS MySQL & Play Framework 1.2.5(c3p0 默认设置)的连接偶尔挂起

php - Amazon RDS 和 Zend Framework 2