Peter
Peter

Reputation: 2414

CloudFormation condition functions value based on parameter

I have a parameter where I define the environment.

Parameters:
  Environment:
    Description: Environment. Example: qa, prod
    Type: String

I'm creating an RDS cluster and, in relation to the environment, I would like to set one or the other value for BackupRetentionPeriod

The logic would be: if is 'prod' the value should be int 35, if not int 7.

BackupRetentionPeriod: !Ref Environment = prod, 35, 7

I read the documentation, checked several examples but still, I cannot make it work referencing to a parameter, and setting one or other value inline.

Upvotes: 3

Views: 6035

Answers (2)

lexicore
lexicore

Reputation: 43709

If you for some reason don't like conditions, you can achieve this with Mappings. Something along the lines:

Parameters:
  EnvType:
    Description: >-
      Type of the environment (eu, tu, au, pu). 

      Please use the same environment for all components/stacks of your
      environment.
    Type: String
    Default: eu
    AllowedValues:
      - eu
      - tu
      - au
      - pu
Mappings:
  BackupRetentionPeriod:
    default:
      pu: 35
      eu: 7
      tu: 7
      au: 7

And then:

BackupRetentionPeriod: !FindInMap 
  - BackupRetentionPeriod
  - default
  - !Ref EnvType

Upvotes: 4

Marcin
Marcin

Reputation: 239000

You can use combination of If and Equals in your CloudFormation:

Parameters:

  Environment:
    Description: Environment. Example: qa, prod
    Type: String

Conditions:

  IsProd: 
    !Equals [!Ref Environment, 'prod']  

Resources:

    ....
    ....
    BackupRetentionPeriod: !If [IsProd, 35, 7]

You could also make it without separate Conditions section, but I think the CFN template is easier to read with it, so I included it.

Upvotes: 5

Related Questions