Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the allowed Values of a parameter depend on the selected value of another parameter in AWS Cloudformarion

I am very new to aws Cloud and CloudFormation. I am trying to write a CloudFormation Template where a user can choose the DB Engine through a parameter. Depending on the choice made by the user I would like the instance type to change accordingly. For example: Say part of my template looks like this:

Parameters:
Engine:
    Default: sqlserver-ex
    Type: String
    AllowedValues:
      - sqlserver-se
      - sqlserver-ex
      - sqlserver-ee
DatabaseInstanceType:
    Default: db.t3.small
    AllowedValues:
      - db.t2.micro
      - db.t2.small
      - db.t2.medium
      - db.t2.large
      - db.t2.xlarge

When the user chooses the sqlserver-ex I only want the user to be able to choose an InstanceType like

db.t2.micro
dbt2.medium
db.t3.small
db.t3.xlarge
db.t2.micro
dbt2.medium
db.t3.small
db.t3.xlarge

Is this posible? Is it also posible to use that technique multiple times in a template?

I have searched for a solution for this for a few days with no luck and decided to ask the more advanced developers. I hope that someone can clear this up for me. Thanks in advance.

like image 220
Marian Avatar asked Oct 19 '25 20:10

Marian


2 Answers

CloudFormation Template Constraint Rules enable cross-parameter validation:

https://www.cloudar.be/awsblog/undocumented-feature-using-template-constraint-rules-in-cloudformation

https://aws.amazon.com/blogs/mt/how-to-perform-cross-parameter-validation-using-aws-cloudformation-rules-and-assertions/

https://docs.aws.amazon.com/servicecatalog/latest/adminguide/reference-template_constraint_rules.html

like image 82
Pat Myron Avatar answered Oct 21 '25 10:10

Pat Myron


Update

If the cross-parameter validation solution (provided by @Pat Myron https://stackoverflow.com/a/61874910/842075) works then of course it's better to use that solution.

Original answer

Yes, possible but with tricks. Basically the trick is to replace two parameters with one parameter:

Parameters:
  EngineAndInstanceType:
    Default: sqlserver-ex/db.t3.small
    Type: String
    AllowedValues:
      - sqlserver-se/db.t2.micro
      - sqlserver-se/db.t2.small
      - sqlserver-se/db.t2.medium

      - sqlserver-ex/db.t3.micro
      - sqlserver-ex/db.t3.small
      - sqlserver-ex/db.t3.medium

      - sqlserver-ee/db.t2.large
      - sqlserver-ee/db.t2.xlarge

this of course will disable you from simply !Referencing engine and instance type later in template. You will have to use combination of !Select and !Split functions to be able to use one parameter in two different places:

Resources:
  MyDB:
    Type: 'AWS::RDS::DBInstance'
    Properties:
      Engine: !Select [0, !Split ["/", !Ref EngineAndInstanceType]]
      DBInstanceClass: !Select [1, !Split ["/", !Ref EngineAndInstanceType]]
like image 35
Molecular Man Avatar answered Oct 21 '25 11:10

Molecular Man



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!