0
votes

I want to create read replicas if the user needs it, the choice is a parameter of the cloudformation template. The template executes through when the choice is made to create the replica, but when No is selected to not to create the replica, it throws below error. Please check and let me know what am I missing.

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

After the block for primary and replica instances, there are blocks for creating alarms for both primary and replicas. The condition has been put for remaining replicas alarms code as well. The error it throws is

An error occurred (ValidationError) when calling the CreateStack operation: Template format error: Unresolved resource dependencies [DatabaseReplicaInstance] in the Resources block of the template
1
Is anything else referring to DatabaseReplicaInstance in the template? - John Rotenstein
Yes, the cloudwatch alarms which are being created for the replica instance. Have added the condition for them as well. Is the error due to it being referred elsewhere? If so, how to go about it in this case. - nmakb

1 Answers

0
votes

The error is due to the fact that, if the CreateReadReplica condition evaluates to False, the DatabaseReplicaInstance resource is not being created.

This means that the CloudWatch alarms that refer to DatabaseReplicaInstance will generate an error.

You can resolve this by adding the same Condition: CreateReadReplica setting to the alarms, so that they are only created if the condition is True.