Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cloudformation parameter dependency

I'm trying to do the following:

"Parameters": {
    "InterfaceMode" : {
        "Description": "Configure instance to run in onearm or inline mode",
        "Type": "String",
        "Default": "onearm",
        "AllowedValues": [ "onearm", "inline" ], 
    }
    "InlineSubnetId" : {
        "Description": "Name of a subnet assigned to the VPC to use for second interface in inline mode.", 
        "Type": "AWS::EC2::Subnet::Id",
        "Default": "None"
    },

Now if the user selects onearm, only one interface is needed and the InlineSubnetId is not needed. Usually the user would leave the "InlineSubnetId" drop down empty, but this doesn't work with cloudformation validation since it requires a value for AWS types. I can't just use a string type as I want the user to select from AWS-supplied SubnetIds.

How to get around this?

  1. Is there a way to bypass validation, allowing an AWS type chosen to be empty?
  2. Any way to add another option like "None" to the Subnet::Id list?
  3. Is there a way to hide the inlineSubnetId parameter only if the mode is inline?
  4. How about a second page of parameters, that depends on the output of the first page of paramters?

Thanks for the help.

like image 241
Tony Lin Avatar asked Oct 21 '16 19:10

Tony Lin


People also ask

How do you reference parameters in CloudFormation?

Referencing a parameter within a template You use the Ref intrinsic function to reference a parameter, and AWS CloudFormation uses the parameter's value to provision the stack. You can reference parameters from the Resources and Outputs sections of the same template.

What are parameters which are predefined by CloudFormation called?

Pseudo parameters are parameters that are predefined by AWS CloudFormation. You don't declare them in your template. Use them the same way as you would a parameter, as the argument for the Ref function.

What is circular dependency in CloudFormation?

A circular dependency, as the name implies, means that two resources are dependent on each other or that a resource is dependent on itself: Resource A is dependent on Resource B, and Resource B is dependent on Resource A.

How do I specify AWS-specific parameters in CloudFormation templates?

To specify parameters with AWS-specific types, a template user must enter existing AWS values that are in their AWS account. AWS CloudFormation validates these input values against existing values in the account. For example, with the AWS::EC2::VPC::Id parameter type, a user must enter an existing VPC ID...

What is myusername in AWS CloudFormation?

For example, users could specify "MyUserName" . An integer or float. AWS CloudFormation validates the parameter value as a number; however, when you use the parameter elsewhere in your template (for example, by using the Ref intrinsic function), the parameter value becomes a string. For example, users could specify "8888" .

How does AWS CloudFormation validate the input value of a parameter?

For example, if you use the AWS::EC2::KeyPair::KeyName parameter type, AWS CloudFormation validates the input value against users' existing key pair names before it creates any resources, such as Amazon EC2 instances. If a user uses the AWS Management Console, AWS CloudFormation prepopulates AWS-specific parameter types with valid values.

What is parallelization in AWS CloudFormation?

AWS CloudFormation creates, updates, and deletes resources in parallel to the extent possible. It automatically determines which resources in a template can be parallelized and which have dependencies that require other operations to finish first. You can use DependsOn to explicitly specify dependencies,...


2 Answers

Unfortunately, if you want a Parameter to be optional, you can not use any of the AWS-specific parameter types (i.e. AWS::*). None of your hoped-for workarounds will work, either. I'd recommend a type String with an AllowedPattern set to something like ^(subnet-[0-9a-fA-F]{8})?$, but this will not meet your requirement of making the drop-down prepopulated with existing subnet values.

like image 156
mfisherca Avatar answered Sep 16 '22 15:09

mfisherca


I've been looking for this type of conditional parameter as well and as far as I can tell it doesn't exist. The closest you can come is an AWS Condition.

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html

This will allow you to define conditions that evaluate to true or false based on input parameters that you can then use along with Fn:If statements to inject different values into resources.

So for your above scenario you may want to default the value to a known subnet id, but use Conditions to ignore this value if not needed. Unfortunately this falls a bit short if you are trying to use the same CloudFormation template across different VPCs as the default subnet wouldn't exist.

like image 42
bcampolo Avatar answered Sep 20 '22 15:09

bcampolo