Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS: How to specify a boolean parameter in a CloudFormation template

I'm trying to specify a boolean parameter in a CloudFormation template so I can conditionally create resources based on a parameter passed in.

Looking at the documentation here and here it would appear there is a discernible lack of a boolean data type.

What is best practice for specifying a boolean? possibly Number with 0 or 1 or String with AllowedValues 'true' and 'false'?

like image 605
Willem van Ketwich Avatar asked Jan 11 '17 02:01

Willem van Ketwich


People also ask

How do you reference a parameter 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 is pseudo parameter in AWS CloudFormation?

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 Boolean in AWS?

Use the BOOLEAN data type to store true and false values in a single-byte column.

How do I change parameters in CloudFormation stack?

In the AWS CloudFormation console , from the list of stacks, select the running stack that you want to update. In the stack details pane, choose Update. If you haven't modified the stack template, select Use current template, and then choose Next.


1 Answers

The Quick Start templates are a good, semi-official reference point of how complex templates can/should be created, and they implement Boolean values for Conditional resources exactly as you described, using a String with AllowedValues true and false. Here's a specific example:

"EnableBanner": {
    "AllowedValues": [
        "true",
        "false"
    ],
    "Default": "false",
    "Description": "To include a banner to be displayed when connecting via SSH to the bastion, set this parameter to true",
    "Type": "String"
}

A similar example can be found in the Conditionally use an existing resource example from the CloudFormation documentation, where the AllowedValues are default or NONE (the default).

To conditionally create a resource based on such a boolean parameter, you add a Condition statement containing a Fn::Equals intrinsic function matching true, then add a Condition key to the resource.

Here's a complete, minimal example template:

Launch Stack

Parameters:
  CreateResource:
    Description: Whether I should create a resource.
    Default: false
    Type: String
    AllowedValues: [true, false]
Conditions:
  ShouldCreateResource:
    !Equals [true, !Ref CreateResource]
Resources:
  Resource:
    Type: AWS::CloudFormation::WaitConditionHandle
    Condition: ShouldCreateResource
like image 134
wjordan Avatar answered Oct 23 '22 17:10

wjordan