Variable
The variable resource defines input parameters that can be used throughout your lab configuration. Variables provide a way to parameterize your lab configuration, making it more flexible and reusable by allowing values to be customized without modifying the core configuration.
Use Cases
Section titled “Use Cases”As a lab author, you can use variables to create configurable lab experiences:
- Environment Customization: Define different resource allocations (CPU, memory) for different lab environments or user tiers
- User Personalization: Allow customization of usernames, workspace names, or personal preferences within labs
- Multi-Variant Labs: Create labs with different difficulty levels or feature sets controlled by variable values
- Resource Scaling: Configure container counts, instance sizes, or service configurations based on lab requirements
- Feature Flags: Enable or disable specific lab features, tools, or resources using boolean variables
- Dynamic Naming: Generate consistent naming patterns across resources using variable-based naming schemes
Variables make your lab configurations maintainable and adaptable to different scenarios and requirements.
HCL Syntax
Section titled “HCL Syntax”Basic Syntax
Section titled “Basic Syntax”variable "name" {  default = "value"}Full Syntax
Section titled “Full Syntax”variable "name" {  default     = "default_value"  description = "Description of what this variable controls"}Fields
Section titled “Fields”| Field | Required | Type | Description | 
|---|---|---|---|
| default | ✓ | any | Default value for the variable (can be string, number, boolean, list, or object) | 
| description | string | Human-readable description of the variable’s purpose | |
| depends_ | list(string) | List of resource dependencies (inherited from ResourceBase) | |
| disabled | bool | Whether this variable is disabled. Defaults to false. | 
Variable Types
Section titled “Variable Types”Variables can hold different data types:
| Type | Example | Usage | 
|---|---|---|
| string | default = "admin" | Text values like usernames, passwords, names | 
| number | default = 2048 | Numeric values like CPU, memory, ports, counts | 
| bool | default = false | Enable/disable flags for features or resources | 
| list | default = ["web", "api", "db"] | Arrays of values like service names, ports | 
| object | default = { cpu = 1024, memory = 512 } | Complex configurations with multiple fields | 
Variable Reference
Section titled “Variable Reference”Variables are referenced in other resources using the syntax:
variable.variable_nameExamples
Section titled “Examples”String Variable
Section titled “String Variable”variable "username" {  default     = "admin"  description = "Default username for system access"}
resource "container" "web" {  image {    name = "nginx:latest"  }
  environment = {    USER = variable.username  }}Numeric Variable
Section titled “Numeric Variable”variable "cpu_resources" {  default     = 2048  description = "CPU resources allocation in millicores"}
resource "container" "app" {  image {    name = "myapp:latest"  }
  resources {    cpu = variable.cpu_resources  }}Boolean Variable
Section titled “Boolean Variable”variable "debug_enabled" {  default     = false  description = "Enable debug mode for applications"}
resource "container" "service" {  image {    name = "myservice:latest"  }
  disabled = !variable.debug_enabled
  environment = {    DEBUG = variable.debug_enabled ? "true" : "false"  }}List Variable
Section titled “List Variable”variable "service_names" {  default     = ["web", "api", "database"]  description = "List of services to deploy"}
resource "container" "services" {  count = length(variable.service_names)
  image {    name = "${variable.service_names[count.index]}:latest"  }
  container_name = variable.service_names[count.index]}Object Variable
Section titled “Object Variable”variable "database_config" {  default = {    username = "dbuser"    password = "dbpass"    database = "myapp"    port     = 5432  }  description = "Database connection configuration"}
resource "container" "postgres" {  image {    name = "postgres:13"  }
  environment = {    POSTGRES_USER     = variable.database_config.username    POSTGRES_PASSWORD = variable.database_config.password    POSTGRES_DB       = variable.database_config.database  }
  port {    local  = variable.database_config.port    remote = 5432  }}Multiple Variables Example
Section titled “Multiple Variables Example”variable "environment" {  default     = "development"  description = "Environment name (development, staging, production)"}
variable "replica_count" {  default     = 1  description = "Number of application replicas to run"}
variable "enable_monitoring" {  default     = false  description = "Enable monitoring and logging components"}
resource "container" "app" {  count = variable.replica_count
  image {    name = "myapp:latest"  }
  container_name = format("app-%s-%02d", variable.environment, count.index + 1)
  environment = {    ENV = variable.environment  }}
resource "container" "monitor" {  image {    name = "prometheus:latest"  }
  disabled = !variable.enable_monitoring}Best Practices
Section titled “Best Practices”- Descriptive Names: Use clear, descriptive variable names that indicate their purpose
- Provide Descriptions: Always include descriptions to document what each variable controls
- Reasonable Defaults: Set sensible default values that work for most use cases
- Type Consistency: Use consistent types for similar variables across your configuration
- Validation: Consider the range of values users might provide when setting defaults
- Documentation: Document variable usage patterns in your lab instructions or README
Common Patterns
Section titled “Common Patterns”Resource Scaling
Section titled “Resource Scaling”variable "worker_count" {  default     = 3  description = "Number of worker containers to create"}
resource "container" "worker" {  count = variable.worker_count
  image {    name = "worker:latest"  }
  container_name = format("worker-%02d", count.index + 1)}Feature Toggles
Section titled “Feature Toggles”variable "features" {  default = {    monitoring = true    debugging  = false    caching    = true  }  description = "Feature flags for lab components"}
resource "container" "cache" {  disabled = !variable.features.caching
  image {    name = "redis:alpine"  }}Environment-Specific Configuration
Section titled “Environment-Specific Configuration”variable "config" {  default = {    dev = {      cpu    = 512      memory = 256      debug  = true    }    prod = {      cpu    = 2048      memory = 1024      debug  = false    }  }  description = "Environment-specific configurations"}
variable "environment" {  default = "dev"  description = "Target environment"}
resource "container" "app" {  image {    name = "myapp:latest"  }
  resources {    cpu    = variable.config[variable.environment].cpu    memory = variable.config[variable.environment].memory  }
  environment = {    DEBUG = variable.config[variable.environment].debug ? "1" : "0"  }}