Course contentsShow
← Cloud and Infrastructure
Lesson 868 of 2,98916. AWS CloudFormationFree lesson

Condition Functions

Create conditional logic with Fn::If, Fn::Equals, Fn::And, Fn::Or, and Fn::Not functions.

What You'll Learn

You'll learn how to build conditional logic into your CloudFormation templates using built-in functions that control whether resources are created or how they're configured based on runtime conditions.

What Are Condition Functions?

Condition functions let you make "if-then" decisions inside your CloudFormation templates. Think of them like traffic lights for your infrastructure: they evaluate conditions and determine which resources get deployed or how they're configured.

These functions work with the Conditions section of your template, allowing you to write logic based on parameter values, environment types, or other criteria.

The Five Core Condition Functions

Fn::If

Makes decisions based on a condition name. Returns one value if true, another if false.

!If [UseProductionConfig, t3.large, t3.micro]

Fn::Equals

Compares two values for equality. Forms the foundation of most conditions.

!Equals [!Ref Environment, "production"]

Fn::And

Returns true only if all nested conditions are true (like "&&" in programming).

!And [!Equals [!Ref Env, "prod"], !Equals [!Ref Region, "us-east-1"]]

Fn::Or

Returns true if any nested condition is true (like "||" in programming).

!Or [!Equals [!Ref Env, "dev"], !Equals [!Ref Env, "test"]]

Fn::Not

Inverts a condition—turns true to false and vice versa.

!Not [!Equals [!Ref CreateResource, "false"]]

How They Work Together

You typically define conditions using Fn::Equals, Fn::And, Fn::Or, or Fn::Not, then reference those conditions with Fn::If in your resource properties or use them directly in the Condition property of resources.

Key Takeaway: Condition functions transform static templates into smart, adaptable infrastructure code that makes decisions at deployment time based on your environment and requirements.