Can I set a property in CloudFormation only if the entity doesn't exist yet?

Viewed 287

I have a CloudFormation via Amplify that defines a Cognito User Pool. I want to set future environments that are deployed from this template to have UsernameConfiguration.CaseSensitive: False. If I just add that property to my CloudFormation template, the update fails with the following error:

Reason: Updates are not allowed for property - UsernameConfiguration. (Service: AWSCognitoIdentityProvider; Status Code: 400; Error Code: InvalidParameterException; Request ID: null; Proxy: null)

Is there a way to condition this property on whether the pool already exists? I'd like to have any new environments that are created use the new config, but still allow the old environments to deploy.

Here's the snippet of the CloudFormation template that Amplify uses to create the User Pool.

    # BEGIN USER POOL RESOURCES
    UserPool:
        # Created upon user selection
        # Depends on SNS Role for Arn if MFA is enabled
        Type: AWS::Cognito::UserPool
        UpdateReplacePolicy: Retain
        Properties:
            UserPoolName:
                !If [
                    ShouldNotCreateEnvResources,
                    !Ref userPoolName,
                    !Join ["", [!Ref userPoolName, "-", !Ref env]],
                ]

            Schema:
                - Name: email
                  Required: true
                  Mutable: true

            LambdaConfig:
                PostAuthentication: !Ref functiontestcd9b6b5ePostAuthenticationArn

                PostConfirmation: !Ref functiontestcd9b6b5ePostConfirmationArn

                PreTokenGeneration: !Ref functiontestcd9b6b5ePreTokenGenerationArn

            AutoVerifiedAttributes: !Ref autoVerifiedAttributes

            EmailVerificationMessage: !Ref emailVerificationMessage
            EmailVerificationSubject: !Ref emailVerificationSubject

            Policies:
                PasswordPolicy:
                    MinimumLength: !Ref passwordPolicyMinLength
                    RequireLowercase: false
                    RequireNumbers: false
                    RequireSymbols: false
                    RequireUppercase: false

            UsernameAttributes: !Ref usernameAttributes

            MfaConfiguration: !Ref mfaConfiguration
            SmsVerificationMessage: !Ref smsVerificationMessage
            SmsAuthenticationMessage: !Ref smsAuthenticationMessage
            SmsConfiguration:
                SnsCallerArn: !GetAtt SNSRole.Arn
                ExternalId: testcd9b6b5e_role_external_id
            UsernameConfiguration:
                CaseSensitive: False

1 Answers

Is there a way to condition this property on whether the pool already exists?

Sadly its not possible because CFN does not have functions to check if a pool exists or not. If you require such functionality you have to implement it yourself using custom resource.

Related