Random ID
The random_id resource generates cryptographically secure random identifiers. It provides the generated ID in multiple formats (base64, hexadecimal, and decimal) for use in other resources and configurations.
Use Cases
Section titled “Use Cases”As a lab author, you can use random_id resources to:
- Unique Resource Naming: Generate unique suffixes for container names, network names, and other resources to prevent conflicts
- Dynamic Configuration: Generate unique keys, tokens, or identifiers for application configuration files
Random ID resources ensure uniqueness and prevent resource conflicts in lab environments while maintaining idempotency.
HCL Syntax
Section titled “HCL Syntax”Basic Syntax
Section titled “Basic Syntax”resource "random_id" "name" { byte_length = 8}
Full Syntax
Section titled “Full Syntax”resource "random_id" "name" { byte_length = 16}
Fields
Section titled “Fields”Field | Required | Type | Description |
---|---|---|---|
byte_ | ✓ | int64 | Number of random bytes to generate (minimum: 1) |
Computed Attributes
Section titled “Computed Attributes”These attributes are set by the system after ID generation:
Field | Type | Description |
---|---|---|
base64 |
string | Generated ID in base64 encoding |
hex |
string | Generated ID in padded hexadecimal (twice the byte length) |
dec |
string | Generated ID in decimal format |
Validation Rules
Section titled “Validation Rules”- Byte length must be at least 1
- Generated values remain constant across multiple runs (idempotent)
- Random values are cryptographically secure
Examples
Section titled “Examples”Simple Random ID
Section titled “Simple Random ID”resource "random_id" "session" { byte_length = 8}
output "session_id" { value = resource.random_id.session.hex}
Database Identifier
Section titled “Database Identifier”resource "random_id" "db_suffix" { byte_length = 4}
resource "container" "database" { image { name = "postgres:15" }
environment = { POSTGRES_DB = "app_${resource.random_id.db_suffix.hex}" }}
Multiple Format Usage
Section titled “Multiple Format Usage”resource "random_id" "app_key" { byte_length = 32}
# Use in different formatsoutput "key_base64" { value = resource.random_id.app_key.base64 description = "Application key in base64 format"}
output "key_hex" { value = resource.random_id.app_key.hex description = "Application key in hex format"}
output "key_decimal" { value = resource.random_id.app_key.dec description = "Application key in decimal format"}
Container Environment Variables
Section titled “Container Environment Variables”resource "random_id" "container_suffix" { byte_length = 6}
resource "container" "web" { image { name = "nginx:alpine" }
# Use random ID in environment variables environment = { INSTANCE_ID = resource.random_id.container_suffix.hex CONTAINER_SUFFIX = resource.random_id.container_suffix.base64 }}
Secret Generation
Section titled “Secret Generation”resource "random_id" "api_secret" { byte_length = 24}
resource "template" "app_config" { source = <<-EOF api: secret_key: ${resource.random_id.api_secret.base64} session_timeout: 3600 EOF
destination = "./config/app.yaml"}
Network Subnet Generation
Section titled “Network Subnet Generation”resource "random_id" "network_id" { byte_length = 4}
resource "network" "app_network" { subnet = "10.${resource.random_id.network_id.dec % 254 + 1}.0.0/24"}
Best Practices
Section titled “Best Practices”- Appropriate Length: Use sufficient byte length for your use case (8-16 bytes for most identifiers)
- Format Selection: Choose the appropriate format for your target system
- Uniqueness: Random IDs are unique per resource instance, not globally unique
- Security: Use for non-sensitive identifiers; for passwords use random_password
- Naming: Use descriptive resource names to clarify the ID’s purpose
- Consistency: Use the same format consistently within a configuration