Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 341
Peter Avatar asked May 13 '26 06:05

Peter


2 Answers

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.

like image 167
Marcin Avatar answered May 14 '26 23:05

Marcin


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
like image 24
lexicore Avatar answered May 15 '26 00:05

lexicore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!