使用cloudformation条件创建副本实例失败

时间:2019-05-19 23:14:50

标签: amazon-web-services amazon-cloudformation amazon-rds

如果用户需要,我想创建只读副本,该选择是cloudformation模板的参数。当选择创建副本时,模板将一直执行,但是如果选择不创建副本,则该模板将抛出错误。请检查并让我知道我在想什么。

....
    DatabaseReadReplicaRequired:
        Type: String
        Default: 'No'
        AllowedValues:
          - 'Yes'
          - 'No'
        Description: The database cluster needs a read replica
....
Conditions:
  CreateReadReplica: !Equals [ !Ref DatabaseReadReplicaRequired, "Yes" ]

.....
    DatabasePrimaryInstance:
         ....

    DatabaseReplicaInstance:
        Metadata:
          cfn_nag:
            rules_to_suppress:
              - id: F22
        Type: AWS::RDS::DBInstance
        Condition: CreateReadReplica
        Properties:
            Engine: aurora-postgresql
            EngineVersion: '10.6'
            DBClusterIdentifier:
                Ref: DatabaseCluster
            DBInstanceClass:
                Ref: DatabaseInstanceType
            DBSubnetGroupName:
                Ref: DatabaseSubnetGroup
            PubliclyAccessible: False
            EnablePerformanceInsights: True
            DBParameterGroupName: aurora-postgresql-10dot6

在主实例和副本实例的块之后,有一些块用于为主实例和副本创建警报。还为其余副本警报代码设置了条件。它引发的错误是

An error occurred (ValidationError) when calling the CreateStack operation: Template format error: Unresolved resource dependencies [DatabaseReplicaInstance] in the Resources block of the template

1 个答案:

答案 0 :(得分:0)

该错误是由于以下事实造成的:如果CreateReadReplica条件的值为False,则不会创建DatabaseReplicaInstance资源。

这意味着引用DatabaseReplicaInstance的CloudWatch警报将生成错误。

您可以通过向警报添加相同的Condition: CreateReadReplica设置来解决此问题,以便仅在条件为True时创建警报。