shonky linux user
shonky linux user

Reputation: 6428

Cloudformation conditional nested stack Unresolved resource dependencies

I have a Cloudformation stack that conditionally invokes a nested stack to create a RDS instance, only if an existing database URL is not passed in as a parameter.

If I pass a value to the DBExistingEndpoint parameter in the stack, the condition CreateDB is set to false, and it will not invoke the nested RDS stack at all.

The issue is that in the AutoScaling launch config resource, there is a conditional dependency. I need to reference either the URL output from the nested stack, or the URL passed in as a parameter to place in a file in the newly launched instance.

    Parameters:
      DBExistingEndpoint:
        Type: String
        Description: Set to a URL of a RDS instance to use an existing DB, otherwise create one
        Default: ''
    ...
    
    Conditions:
      CreateDB:
        !Equals [!Ref DBExistingEndpoint, '']
    ...

    Resources:
      # Database created only if existing URL not passed in
      DB:
        Type: AWS::CloudFormation::Stack
        Condition: CreateDB
        Properties:
          TemplateURL: ...
      ...

      ClusterInstanceLaunchConfig:
        Type: AWS::AutoScaling::LaunchConfiguration
        Metadata:
          AWS::CloudFormation::Init:
            config:

              files:
                /etc/dbenv:
                  mode: "000640"
                  owner: root
                  group: root
                  content:
                    !Join
                      - "\n"
                      -
                        - !Sub ["DB_HOST=${DBEndpointAddress}", DBEndpointAddress: !If [CreateDB, !GetAtt DB.Outputs.RDSEndPointAddress, !Ref DBExistingEndpoint]]
                      ...

The issue is that if I pass in an existing endpoint URL, the DB resource is skipped (correctly), but the stack creation fails with Template format error: Unresolved resource dependencies [DB] in the Resources block of the template

Ideally the DB.output.RDSEndpointAddress reference in the ClusterInstanceLauchConfig resource should be ignored because the CreateDB condition in the !If is false

Does anybody know how to code around this limitation?

Upvotes: 0

Views: 561

Answers (1)

stijndepestel
stijndepestel

Reputation: 3564

You should try to set the conditional statement on a different level than it is now.

What will work for sure, is having the conditional statement on the level of the LaunchConfiguration itself, which would also mean quite a lot of duplication of the code. But maybe you could try to see the conditional on the level of content or files etc, to see if there's a middle ground somewhere, to keep duplication low, but avoid the error you're getting right now.

Upvotes: 0

Related Questions