Where Conditions live
Conditions are defined in the top-levelConditions section of a CloudFormation template. Use them in two primary ways:
- Attach a Condition to a resource or output using the
Conditionattribute so the entire resource/output is included only when the condition evaluates to true. - Use the
Fn::Ifintrinsic function (!Ifin YAML shorthand) inside a property to select between two values depending on the Condition.
Basic Condition example
Define a condition that checks whether a parameter namedEnvironmentType equals "Production":
IsProduction evaluates to true when the EnvironmentType parameter equals Production.
Conditional resource creation
Attach the Condition to a resource so it’s created only when the condition is true:IsProduction is false, MyBucket and its associated physical resource are not created.
Conditional properties with !If
Use!If to choose between two values for a property:
!If works:
- First item: condition name (
IsProduction). - Second item: value returned when condition is true.
- Third item: value returned when condition is false.
AWS::NoValue from the false branch. Otherwise, !If will substitute one of the two values, and the property will remain present.
When using
!If, the two return values must match the data type expected by the property (for example, both mappings if a mapping is expected). The exception is using the pseudo value AWS::NoValue to remove a property entirely. Conditions are evaluated at stack creation or update time based on parameter values and template logic.Combining Conditions
Use the other intrinsic functions to build more complex logic:Fn::And/!And— true if all conditions are trueFn::Or/!Or— true if any condition is trueFn::Not/!Not— inverse of a conditionFn::Equals/!Equals— compares two values
IsProduction and a UseS3 parameter to both be true before creating a resource:
Common patterns and best practices
| Pattern | Use case | Example |
|---|---|---|
| Resource-level condition | Include or exclude entire resources/outputs | Condition: IsProduction on a resource |
| Property-level condition | Change or remove specific properties | !If with AWS::NoValue to omit a property |
| Combined conditions | Complex environment logic | !And, !Or, !Not with parameter checks |
| Reusable condition names | Use clear, descriptive names | IsProduction, EnableFeatureX |
Things to watch out for
Conditions cannot change resource logical IDs or resource types. Also ensure both branches returned by
!If match the expected data type for the property — otherwise template validation or runtime errors can occur. Use AWS::NoValue when you intend to omit a property entirely.- Conditions are evaluated during stack creation or update — they are not dynamic at runtime after the stack is active.
!Ifcannot remove a property unless one branch returnsAWS::NoValue. If you need to omit the whole resource, use theConditionattribute on the resource instead.- You may reference parameters, mappings, and pseudo parameters inside Conditions, but avoid referencing attributes of resources that may not exist due to other conditions.