Skip to main content
Welcome to the lesson on AWS CloudFormation Conditions. This page explains what Conditions are, how they work, and how to use them to make templates dynamic and environment-aware. Conditions let a template decide, at stack create or update time, whether to create resources, set property values, or include outputs. They evaluate to true or false using intrinsic functions and can be driven by parameters, mappings, or other runtime inputs.

Where Conditions live

Conditions are defined in the top-level Conditions section of a CloudFormation template. Use them in two primary ways:
  • Attach a Condition to a resource or output using the Condition attribute so the entire resource/output is included only when the condition evaluates to true.
  • Use the Fn::If intrinsic function (!If in 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 named EnvironmentType 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:
When 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:
How !If works:
  • First item: condition name (IsProduction).
  • Second item: value returned when condition is true.
  • Third item: value returned when condition is false.
If you need to remove a property entirely when the condition is false, return the pseudo parameter 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 true
  • Fn::Or / !Or — true if any condition is true
  • Fn::Not / !Not — inverse of a condition
  • Fn::Equals / !Equals — compares two values
Example: require IsProduction and a UseS3 parameter to both be true before creating a resource:

Common patterns and best practices

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.
  • !If cannot remove a property unless one branch returns AWS::NoValue. If you need to omit the whole resource, use the Condition attribute 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.

Quick reference snippets

Conditional resource:
Conditional property with AWS::NoValue:
Combined condition example:
Conditions are a powerful way to make your CloudFormation templates reusable and environment-aware. Start by adding simple parameter-driven Conditions, then combine them with intrinsic functions as your template logic grows.

Watch Video