3

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.

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I was trying to avoid Conditions, thinking that maybe it was possible to perform some conditional logic inline... But, it seems it is not the case. As always, I go with your answer @Marcin - Btw, would you mind to recommend me some resources (links, books) to improve my CFN skills (and Cloud in general). Without commitment :D Again, thanks!
@Peter Inline should also be possible !If [ !Equals [!Ref Environment, 'prod'] , 35, 7]. I think aws docs and their solutions are good examples for templates. And practice obviously :-)
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.