Sub topic 7
Conditional resources and properties
This chapter assumes you can read the template sections from earlier chapters and use Ref and Fn::Sub. You should also have a template with a Parameters section.
How conditions work
A condition is a named true or false value. CloudFormation evaluates every condition at the start of a create or update, using the parameter values you supply. You declare conditions in the Conditions section and then use them in three places:
- On a resource, with the
Conditionkey. The resource is created only if the condition is true. - Inside a property, with
Fn::If. The condition picks which value the property gets. - On an output, with the
Conditionkey. The output appears only if the condition is true.
Condition functions
Use these functions to build a condition:
| Function | Meaning |
|---|---|
| `Fn::Equals` | True if two values match |
| `Fn::Not` | Inverts one condition |
| `Fn::And` | True if all listed conditions are true (2 to 10) |
| `Fn::Or` | True if any listed condition is true (2 to 10) |
To reuse a condition inside Fn::And, Fn::Or or Fn::Not, refer to it with !Condition Name.
A worked example
This template turns versioning on only in prod. It creates an alert topic only in prod, and only when an email address was given.
AWSTemplateFormatVersion: "2010-09-09"
Description: Conditional bucket settings and alert topic
Parameters:
EnvName:
Type: String
Default: dev
AllowedValues:
- dev
- prod
AlertEmail:
Type: String
Default: ""
Conditions:
IsProd: !Equals [!Ref EnvName, prod]
HasEmail: !Not [!Equals [!Ref AlertEmail, ""]]
CreateAlerts: !And [!Condition IsProd, !Condition HasEmail]
Resources:
AppBucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration: !If
- IsProd
- Status: Enabled
- !Ref AWS::NoValue
AlertTopic:
Type: AWS::SNS::Topic
Condition: CreateAlerts
Properties:
Subscription:
- Endpoint: !Ref AlertEmail
Protocol: email
Outputs:
AlertTopicArn:
Condition: CreateAlerts
Description: ARN of the alert topic
Value: !Ref AlertTopicAWS::NoValue is a pseudo parameter that removes a property entirely. With EnvName=dev, the bucket has no VersioningConfiguration at all, so it takes the service default. This differs from setting Status: Suspended, which explicitly writes a value.
Common errors
- Referencing a conditional resource without a guard. If
AlertTopicis not created,!Ref AlertTopicfails. Put the sameConditionon any output or resource that refers to it, or wrap the reference inFn::If. - Using
Fn::IfinConditions. Conditions are built from the four functions above.Fn::Ifis for properties and outputs. - Referencing resources in a condition. Conditions can use parameters, mappings and other conditions. They cannot use
Refon a resource. - Expecting conditions to re-evaluate by themselves. They are evaluated only during stack operations. A condition that flips from true to false on update deletes the resource.
Exercise
A template has the condition UseBig: !Equals [!Ref Size, large]. How do you set an instance's InstanceType to m5.large when the condition is true, and leave the property unset otherwise?
Show answer
Write InstanceType: !If [UseBig, m5.large, !Ref AWS::NoValue]. When UseBig is false, AWS::NoValue removes the property. This works only if the resource type has a default or does not require the property.