Table of Contents :
-
Introduction
-
Types of Terraform Variables with Real-World Examples
-
Defining and Using Variables in Terraform
-
Methods to Assign Variable Values
-
Real-World Scenario Using Variables
-
Essential Terraform Commands for Variables
-
Practical DevOps Example Using Variables
-
Key Takeaways
1. Introduction to Terraform Variables
- Why Use Variables?
Variables make Terraform configurations more flexible and maintainable by allowing us to define reusable and modular code that adapts to different environments.
2. Types of Variables in Terraform with Real-Time Examples
String Variables
-
Use Case: Storing environment-specific values like region, instance type, or environment name.
-
Example: Deploying an AWS instance in different regions for different environments.
-
Code & Commands:
variable "region" { description = "AWS region for deployment" type = string default = "us-west-2" }
Number Variables
-
Use Case: Defining the instance count, disk size, or other numeric values.
-
Example: Setting the number of instances dynamically based on requirements.
-
Code & Commands:
variable "instance_count" { description = "Number of instances to deploy" type = number default = 2 }
Boolean Variables
-
Use Case: Toggling optional features, such as enabling monitoring or backups.
-
Example: Enabling monitoring for production but disabling for development.
-
Code & Commands:
variable "enable_monitoring" { description = "Enable monitoring on instances" type = bool default = true }
-
To reference: Use
${var.enable_monitoring}
to apply conditional logic. -
Command:
terraform apply -var="enable_monitoring=false"
for environments where monitoring is not needed.
-
List Variables
-
Use Case: Defining a set of values, such as availability zones or IP addresses.
-
Example: Deploying across multiple availability zones.
-
Code & Commands:
variable "availability_zones" { description = "List of availability zones" type = list(string) default = ["us-west-2a", "us-west-2b"] }
-
To reference: Access specific values using
${var.availability_zones[0]}
. -
Command:
terraform apply -var="availability_zones=["us-east-1a", "us-east-1b"]"
.
-
Map Variables
-
Use Case: Storing environment-specific configurations, like instance types for dev/prod.
-
Example: Assigning different instance types for different environments.
-
Code & Commands:
variable "instance_type" { description = "Map of instance types" type = map(string) default = { dev = "t2.micro" prod = "m5.large" } }
-
To reference: Use
${var.instance_type["dev"]}
to access specific values. -
Command:
terraform apply -var="instance_type={ dev="t3.micro", prod="m5.xlarge" }"
for custom overrides.
-
3. Defining and Using Variables
-
Defining Variables: Add a
variables.tf
file to organize all variables. -
Referencing Variables: Variables are referenced with
${var.variable_name}
syntax. -
Command: Run
terraform apply
with variable flags, such asterraform apply -var-file="prod.tfvars"
to deploy production infrastructure.
4. Methods to Assign Variable Values
-
Default Values: Use
default
in variable declaration. -
Environment Variables: Set Terraform variables as environment variables (e.g.,
export TF_VAR_region="us-west-1"
). -
Command-Line Flags: Use
terraform apply -var="variable_name=value"
. -
Variable Definition Files: Store multiple values in
.tfvars
files (e.g.,prod.tfvars
for production).
5. Real-World Scenario with Variables
-
Multi-Region Setup: Using variables to configure instances across regions.
-
Command: Create a variable file (e.g.,
multi-region.tfvars
) for each region, allowing automated deployment across regions using a single command.
6. Essential Commands for Working with Variables
-
terraform plan
: Validates variable usage and checks for any missing values. -
terraform apply
: Applies the configuration using provided variables. -
terraform validate
: Checks the configuration syntax. -
Real-World Use: Easily switch between development and production by referencing a single
.tfvars
file specific to each environment.
7. Practical DevOps Example Using Variables
8. Key Takeaways
-
Variables provide flexibility and reusability in Terraform, allowing for efficient, scalable DevOps workflows.
-
From multi-environment deployments to on-demand instance scaling, variables in Terraform offer a powerful way to adapt configurations dynamically.
Stay tuned for more hands-on tasks and in-depth learning!
Thanks for joining me on Day 64! Let’s keep learning and growing together!
Happy Learning!
#90DaysOfDevOps