rk123
rk123

Reputation: 83

Fn::If condition based on environment for Cloudformation Parameters

Based on the environment, I am trying to set the URL for a variable: It staging my URL should be https://staging.DNHostedZoneName , if prod - it should just be https://DNSHostedZoneName:

Here's my condition:

Conditions:
  IsEnvProd: Fn::Equals [ !Ref Env, 'prod' ]
  IsEnvStage: Fn::Equals [ !Ref Env, 'stage' ]

Here's where its been evaluated:

          Environment:
            - Name: NODE_ENV
              Value: !Ref NodeEnv
            - Fn::If:
              - IsEnvStage
              - Name: CORE_URL
                Value:
                  Fn::Join:
                    - ""
                    - - "https://"
                      - "staging"
                      - "."
                      - !Ref DnsHostedZoneName
              - Name: NCVCORE_URL
                Value:
                  Fn::Join:
                    - ""
                    - - "https://"

                      - !Ref DnsHostedZoneName

I am getting the following error:

 Template format error: Conditions can only be boolean operations on parameters and other conditions

Upvotes: 1

Views: 11945

Answers (3)

marcin2x4
marcin2x4

Reputation: 1439

You can try to orchestrate creation of specific resources using AWS::NoValue

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html

Below is taken from variables creation for LambdaFunction

Conditions:
   IsProd: !Equals [!Ref Env, "production"]

Environment:
   Variables:
     USER: !If [IsProd, !GetAtt ...., Ref: AWS::NoValue]

Upvotes: 1

krunal shimpi
krunal shimpi

Reputation: 1

Usually the conditions defined are used as an attribute to an aws resource and you specify the name of the condition as a value. You can try https://krunal4amity.github.io - it is an online cloudformation template generator. It takes away a lot of such dreadful work.

Upvotes: 1

George Rushby
George Rushby

Reputation: 1345

Without the full template, it is difficult to try to recreate the issue, but here your snippets refactored with a possible error removed.

Adjusted the conditionals to use all shorthand.

Conditions:
  IsEnvProd:  !Equals [!Ref "Env", "prod"]
  IsEnvStage: !Equals [!Ref "Env", "stage"]

There was an additional space in the YAML so that has been removed, and reformatted.

Environment:
- Name: "NODE_ENV"
  Value: !Ref "NodeEnv"
- !If
  - "IsEnvStage"
  - Name: "CORE_URL"
    Value: !Sub "https://staging.${DnsHostedZoneName}"
  - Name: "NCVCORE_URL"
    Value: !Sub "https://${DnsHostedZoneName}"

Upvotes: 4

Related Questions