Random Number
The random_number resource generates random integers within a specified range. It provides deterministic random number generation that remains consistent across multiple runs.
Use Cases
Section titled “Use Cases”As a lab author, you can use random_number resources to:
- Port Assignment: Generate random port numbers for services to avoid conflicts in multi-instance lab environments
- Load Balancing: Create random weights or priorities for load balancing scenarios and traffic distribution
- Network Configuration: Create random VLAN IDs, network segments, or routing priorities for network labs
Random number resources provide controlled randomness for lab configuration while ensuring reproducible results.
HCL Syntax
Section titled “HCL Syntax”Basic Syntax
Section titled “Basic Syntax”resource "random_number" "name" { minimum = 1000 maximum = 2000}
Full Syntax
Section titled “Full Syntax”resource "random_number" "name" { minimum = 10000 maximum = 65535}
Fields
Section titled “Fields”Field | Required | Type | Description |
---|---|---|---|
minimum | ✓ | int | The minimum number to generate (inclusive) |
maximum | ✓ | int | The maximum number to generate (inclusive) |
Computed Attributes
Section titled “Computed Attributes”These attributes are set by the system after number generation:
Field | Type | Description |
---|---|---|
value |
int | The generated random number |
Validation Rules
Section titled “Validation Rules”- Minimum value must be less than or equal to maximum value
- Both minimum and maximum must be valid integers
- Generated values remain constant across multiple runs (idempotent)
- Random values are deterministically generated based on resource configuration
Examples
Section titled “Examples”Random Port Number
Section titled “Random Port Number”resource "random_number" "port" { minimum = 10000 maximum = 20000}
resource "container" "web" { image { name = "nginx:alpine" }
port { local = resource.random_number.port.value # e.g., 15847 host = resource.random_number.port.value # e.g., 15847 }}
output "assigned_port" { value = resource.random_number.port.value # e.g., 15847}
Load Balancer Weight
Section titled “Load Balancer Weight”resource "random_number" "weight" { minimum = 1 maximum = 100}
resource "template" "nginx_config" { source = <<-EOF upstream backend { server backend1:8080 weight=${resource.random_number.weight.value}; # e.g., weight=73 server backend2:8080 weight=${100 - resource.random_number.weight.value}; # e.g., weight=27 } EOF
destination = "./nginx.conf"}
Multiple Random Values
Section titled “Multiple Random Values”resource "random_number" "cpu_shares" { minimum = 1 maximum = 4}
resource "random_number" "memory_mb" { minimum = 512 maximum = 2048}
resource "container" "app" { image { name = "myapp:latest" }
resources { cpu = resource.random_number.cpu_shares.value * 250 # e.g., 750 (if cpu_shares.value = 3) memory = resource.random_number.memory_mb.value # e.g., 1536 (if memory_mb.value = 1536) }}
Network Configuration
Section titled “Network Configuration”resource "random_number" "subnet_octet" { minimum = 1 maximum = 254}
resource "network" "lab_network" { subnet = "10.${resource.random_number.subnet_octet.value}.0.0/24" # e.g., "10.142.0.0/24"}
# Use random number in template for network configurationresource "template" "network_config" { source = <<-EOF network: subnet: 10.${resource.random_number.subnet_octet.value}.0.0/24 vlan_id: ${resource.random_number.subnet_octet.value + 100} EOF
destination = "./network.yaml"}
Scaling Simulation
Section titled “Scaling Simulation”resource "random_number" "instance_count" { minimum = 2 maximum = 8}
resource "container" "worker" { count = resource.random_number.instance_count.value # e.g., 5 containers created
image { name = "worker:latest" }
environment = { WORKER_ID = tostring(count.index + 1) # e.g., "1", "2", "3", "4", "5" TOTAL_WORKERS = tostring(resource.random_number.instance_count.value) # e.g., "5" }}
output "worker_count" { value = resource.random_number.instance_count.value # e.g., 5 description = "Number of worker instances created"}
Best Practices
Section titled “Best Practices”- Range Selection: Choose appropriate minimum and maximum values for your use case
- Collision Avoidance: Use sufficiently wide ranges to minimize collision probability
- Resource Naming: Use descriptive names to clarify the random number’s purpose
- Validation: Ensure generated values work with target systems and configurations
- Documentation: Document the purpose and expected range of random values
- Determinism: Remember that values are consistent across runs for the same configuration
- Testing: Test with various generated values to ensure system robustness