Back to: AWS-Basics-Advanced
Lesson 1
Topic: Terraform Variables + Comparisons + Conditions
You will learn:
-
Types of variables
-
Small definitions
-
For each variable → comparison examples
-
For each variable → conditional (if-else) examples
✅ 1. Terraform Variable Types (Simple Definitions)
| Variable Type | Small Definition |
|---|---|
| string | Text value like "dev", "prod" |
| number | Numeric value like 10, 3.14, 100 |
| bool | True/False values |
| list | Ordered collection ["dev", "stage"] |
| map | Key-value pairs { size = "large" } |
| object | Structured group of attributes |
| set | Unique list with no duplicates |
| tuple | List with fixed types in order |
Now let’s learn them one by one with comparison operators and conditions.
🔥 2. STRING VARIABLE
Define string variable
String Comparisons
| Comparison | Example |
|---|---|
| Equal | var.env == "prod" |
| Not Equal | var.env != "prod" |
Example local variable:
String Conditions (if-else)
Use ternary operator:
🔥 3. NUMBER VARIABLE
Define:
Number Comparisons
| Comparison | Example |
|---|---|
| Greater than | var.cpu > 2 |
| Less than | var.cpu < 4 |
| >= | var.cpu >= 4 |
| <= | var.cpu <= 8 |
| Equal | var.cpu == 4 |
Number Conditions (if-else)
Example: choose instance based on CPU:
Example: limit CPU:
🔥 4. BOOLEAN VARIABLE
Define:
Boolean Comparisons
You can compare with true / false
OR simply:
Boolean Conditions
Enable resource only when boolean is true:
Another example:
🔥 5. LIST VARIABLE
Define:
List Comparisons
Check if item exists in list:
List Conditions (if-else)
Pick first region if more than one:
Pick instance size based on count:
🔥 6. MAP VARIABLE
Define:
Map Comparisons
Check if key exists:
Map Conditions
Select instance based on environment:
🔥 7. OBJECT VARIABLE
Define:
Object Comparisons
Compare fields:
Object Conditions
Set pricing tier dynamically:
🔥 8. SET VARIABLE
Define:
Set Comparisons
Check membership:
Set Conditions
Choose deployment type:
🔥 9. TUPLE VARIABLE
Define:
Tuple Comparisons
Tuple Conditions
1. Difference Between map and object in Terraform
✅ MAP — Key–Value Pair (Flexible)
Definition (Simple):
A map is a collection of key–value pairs where all values must have the same type.
Example:
✔ All values are string
✔ Keys must be strings, but number of keys is flexible
✔ Useful for lookup tables
Lookup Example:
✅ OBJECT — Structured Data Type (Strict)
Definition (Simple):
An object is like a record or struct —
You define multiple attributes, each with specific types.
Example:
✔ Each attribute has different types
✔ Structure is strict — user must follow exact keys & types
✔ Useful for complex configurations
Access Example:
🔥 MAP vs OBJECT — Quick Differences Table
| Feature | MAP | OBJECT |
|---|---|---|
| Structure | Flexible | Strict |
| Keys | Unlimited | Fixed & predefined |
| Value types | All same type | Each attribute has its own type |
| Use case | Lookup tables | Complex structured config |
| Example | { env="prod" } |
{ cpu=2, region="us-east-1" } |
🎯 2. Difference Between variable and locals in Terraform
Let’s learn like an instructor:
✅ VARIABLES — Input from Outside
Definition (Simple):
variable is used to take input from users, CLI, tfvars file, environment variables, etc.
They make Terraform dynamic and configurable.
Example:
Users can override:
Use Case:
-
Input values
-
Environment selection
-
Customization from outside
✅ LOCALS — Internal Helper Values
Definition (Simple):
locals are internal computation helpers used only within the module.
They cannot be passed from outside.
They are like temporary variables inside your Terraform code.
Example:
Use Case:
-
Calculated values
-
If–else logic
-
Repeated expressions
-
Clean code formatting
🔥 VARIABLES vs LOCALS — Quick Difference Table
| Feature | VARIABLES | LOCALS |
|---|---|---|
| Who sets the value? | User / CLI / tfvars | Terraform module itself |
| Can user override? | Yes | No |
| Purpose | External configuration | Internal calculations |
| Example | var.env |
local.instance_type |
| Can depend on? | Inputs, defaults | Variables or other locals |
| Ideal usage | Inputs | Derived values |
🎉 Final Instructor Summary
✔ map
Good for lookup tables. All values same type. Keys flexible.
✔ object
Good for structured configs. Keys fixed. Values can be diff types.
✔ variable
Input from outside. Overrides allowed.
✔ locals
Internal computed values. No override allowed.
🎉 FINAL OUTPUT: Everything Together
You now know:
✔ all Terraform variable types
✔ definition of each
✔ comparison operators for each
✔ if-else conditions for each