Understanding Variables - Your First Programming Concept
Lesson 3
Ready to learn your first real programming concept? Variables are like labeled boxes that store information you can use later. If you’ve ever used a calculator and saved a number for later use, you’ve already used the idea of variables!
Think of variables like this:
- Your phone’s contacts list stores names and phone numbers
- Variables store values (like names, numbers, or yes/no answers) with easy-to-remember names
- Instead of remembering “ami-0c02fb55956c7d316”, you can use a variable called
server_image
Why learn variables before advanced Terraform? Because variables are the foundation of all programming. Once you understand them, everything else becomes much easier to learn. Plus, they make your cloud infrastructure flexible and reusable!
Table of contents
- Understanding Variables - Your First Programming Concept
- The Five Basic Types of Information You Can Store
- 1. Strings - Text Information
- 2. Numbers - Numerical Values
- 3. Bool - True/False Answers
- 4. Objects - Information Packages
- 5. Lists - Collections of Similar Items
- Quick Reference - The Basic Rules
- Hands-On Practice - Master Each Variable Type
- 1. String Practice - Your Name
- 2. String Practice - Server Name
- 3. Number Practice - Maximum Instances
- 4. Boolean Practice - Yes or No Decision
- 5. Object Practice - Server Configuration Package
- 6. List Practice - Multiple Server IDs
- 7. List Practice - Network Ports
- 8. Advanced Challenge - Multiple Server Configurations
- What You’ve Accomplished
The Five Basic Types of Information You Can Store
Just like your phone can store different types of information (text messages, photos, numbers), variables can store different types of data. Terraform recognizes five main types - think of them as different kinds of containers:
Why do types matter? It’s like the difference between a photo album and a phone book - they store different kinds of information in different ways. Terraform needs to know what type of information you’re storing so it can handle it correctly.
1. Strings - Text Information
What it is: Any text - words, sentences, IDs, or even numbers treated as text.
How to recognize it: Always wrapped in quotes " "
Think of it like: Text messages on your phone
Examples:
name = "Sarah"
server_region = "us-east-1"
password = "MySecret123"
user_id = "12345" # This is text, not a number!
Real-world use: Server names, AWS regions, passwords, resource IDs
Practice Strings
2. Numbers - Numerical Values
What it is: Actual numbers you can do math with.
How to recognize it: NO quotes around them
Think of it like: The number of contacts in your phone
Examples:
server_count = 3
port_number = 80
max_users = 1000
Common mistake: server_count = "3"
(with quotes) makes it text, not a number!
Real-world use: Port numbers, instance counts, memory sizes
Practice Numbers
3. Bool - True/False Answers
What it is: Yes/no questions with only two possible answers: true
or false
How to recognize it: Only the words true
or false
(no quotes!)
Think of it like: Whether your phone is on silent mode (yes or no)
Examples:
monitoring_enabled = true
auto_backup = false
publicly_accessible = true
Common mistake: auto_backup = "false"
(with quotes) makes it text, not a true/false value!
Real-world use: Feature toggles, security settings, monitoring options
Practice Booleans
4. Objects - Information Packages
What it is: A collection of related information, like a contact card with name, phone, and email
How to recognize it: Wrapped in curly brackets { }
with key = value pairs
Think of it like: A complete contact entry in your phone
Example:
server_config = {
name = "WebServer"
size = "large"
backup_enabled = true
port = 80
}
What each part means:
name = "WebServer"
→ The server’s name (string)size = "large"
→ The server size (string)backup_enabled = true
→ Whether backups are on (bool)port = 80
→ Which port to use (number)
Real-world use: Complete server configurations, user profiles, network settings
Practice Objects
5. Lists - Collections of Similar Items
What it is: Multiple values of the same type, like a shopping list
How to recognize it: Wrapped in square brackets [ ]
with items separated by commas
Think of it like: Your phone’s photo gallery (multiple photos of the same type)
Examples:
allowed_regions = ["us-east-1", "us-west-2", "eu-west-1"]
port_numbers = [80, 443, 22]
backup_enabled_list = [true, false, true]
Advanced example - List of objects:
servers = [
{
name = "WebServer1"
port = 80
},
{
name = "WebServer2"
port = 443
}
]
Real-world use: Multiple regions, port lists, multiple server configurations
Practice Lists
Quick Reference - The Basic Rules
Remember these simple rules:
- Strings = Always use quotes:
name = "WebServer"
- Numbers = Never use quotes:
port = 80
- Bools = Never use quotes:
enabled = true
- Objects = Use curly brackets:
{ key = value }
- Lists = Use square brackets:
["item1", "item2"]
Important: Once you tell Terraform a variable is a certain type, you must always give it that type of value. You can’t mix types!
Hands-On Practice - Master Each Variable Type
Time to practice! These exercises start simple and gradually get more challenging. Don’t worry if you don’t get them right the first time - that’s how you learn! Click Check Answer
for instant feedback, or expand the Solution
section if you need help.
Remember: You’re writing the assignment part only - the part that comes after the =
sign. For example, if we want a string variable called name
, you would type: name = "YourName"
1. String Practice - Your Name
Goal: Create a variable called name
that stores your name as text.
Remember: Strings need quotes around them!
Solution
The variable should be called name
and should be before the =
. After the =
should be your name, enclosed in quotes
(" "
).
2. String Practice - Server Name
Goal: Create a variable called ec2_name
that stores any server name you want.
Hint: Pick a creative name for your future cloud server!
Solution
The variable should be called ec2_name
and should be before the =
. After the =
should be any name for the EC2 instance you choose, enclosed in quotes
(" "
).
3. Number Practice - Maximum Instances
Goal: Create a variable called instance_max
that stores how many servers you want.
Remember: Numbers don’t use quotes!
Solution
The variable should be called instance_max
and should be before the =
. After the =
should any number you choose, not enclosed in quotes.
4. Boolean Practice - Yes or No Decision
Goal: Create a variable called terminate_instances
that answers: should servers be automatically deleted?
Remember: Only true
or false
(no quotes!)
Solution
The variable should be called terminate_instances
and should be before the =
. After the =
should either true
or false
, not enclosed in quotes.
5. Object Practice - Server Configuration Package
Goal: Create a variable called ec2_instance
that stores complete server information in one package.
Include these three pieces of information:
name
- A string with your server’s nameinstance_max
- A number for maximum instancesterminate_instances
- A boolean (true/false) for auto-deletion
Remember: Objects use curly brackets { }
and each line has key = value
Solution
The variable should be called ec2_instance
and should be before the =
. After the =
should an open curly bracket {
, then three new lines, like in the image below, and a last line of a closed curly bracket }
.
6. List Practice - Multiple Server IDs
Goal: Create a variable called instance_ids
that stores three server IDs as a list.
Remember: Lists use square brackets [ ]
and separate items with commas. Make up your own IDs!
Solution
The variable should be called instance_ids
and should be before the =
. After the =
should an open square bracket [
, then three string values, in quotes (" ")
, separated by commas. Lastly, your variable should end with a closed square bracket ]
.
7. List Practice - Network Ports
Goal: Create a variable called instance_ports
that stores these three port numbers: 80
, 22
, and 8080
.
Remember: Lists of numbers don’t use quotes around the numbers!
Solution
The variable should be called instance_ports
and should be before the =
. After the =
should an open square bracket [
, then three number values, not in quotes, separated by commas. Lastly, your variable should end with a closed square bracket ]
.
8. Advanced Challenge - Multiple Server Configurations
Goal: Create a variable called ec2_instances
that stores information for TWO different servers.
This combines everything: You need a list that contains two objects!
Each server object should have:
name
- A string with the server’s name (make them different!)instance_max
- A number for maximum instancesterminate_instances
- A boolean for auto-deletion
Think of it like: Two contact cards in your phone, each with complete information
Solution
The variable should be called ec2_instances
and should be before the =
. After the =
should an open square bracket [
followed by an open curly bracket {
(to denote your first object). Then, you should have three new lines, each with one of: name
, instance_max
, and terminate_instances
followed by an equal sign =
and a correct variable type. Next, you sould have a closed curly bracket }
(to denote the end of your first object) followed by a comma. You should then have another open curly bracket {
(to denote the starte of your second object), followed again by the three lines of name
, instance_max
, and terminate_instances
(with different values). Lastly, you should have a closed curly bracket }
and a closed square bracket ]
(one to close your second object and the other to close the list).
What You’ve Accomplished
✅ Learned the five fundamental data types - The building blocks of all programming
✅ Practiced with real examples - From simple strings to complex object lists
✅ Understood when to use each type - Strings for text, numbers for math, bools for yes/no
✅ Mastered the syntax rules - Quotes, brackets, and structure patterns
✅ Built confidence with hands-on practice - Interactive exercises with instant feedback
You now understand the foundation of all programming! Variables are used in every programming language, cloud platform, and automation tool. This knowledge will serve you throughout your entire tech career.
Next Steps
Option 1: Practice More (Recommended if you want extra confidence)
Want to solidify your understanding? Go back to the practice exercises and try different values. See if you can create your own examples!
Option 2: Apply Variables to Real Terraform
Ready to use variables in actual cloud infrastructure? Head to Terraform Variables where you’ll learn how to make your cloud code flexible and reusable!
Pro Tip: Variables are like learning to ride a bike - once you get it, you never forget it. You’re now ready for more advanced programming concepts!