Skip to content

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.

resource "random_id" "name" {
byte_length = 8
}
resource "random_id" "name" {
byte_length = 16
}
FieldTypeRequiredDescription
byte_lengthintNumber of random bytes to generate (minimum: 1)

These attributes are set by the system after ID generation:

FieldTypeDescription
base64stringGenerated ID in base64 encoding
hexstringGenerated ID in padded hexadecimal (twice the byte length)
decstringGenerated ID in decimal format
  • Byte length must be at least 1
  • Generated values remain constant across multiple runs (idempotent)
  • Random values are cryptographically secure
resource "random_id" "session" {
byte_length = 8
}
output "session_id" {
value = resource.random_id.session.hex
}
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}"
}
}
resource "random_id" "app_key" {
byte_length = 32
}
# Use in different formats
output "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"
}
resource "random_id" "container_suffix" {
byte_length = 6
}
resource "container" "web" {
image {
name = "nginx:alpine"
}
# Use random ID to ensure unique container names
meta {
name = "web-${resource.random_id.container_suffix.hex}"
}
}
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"
}
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"
}
  1. Appropriate Length: Use sufficient byte length for your use case (8-16 bytes for most identifiers)
  2. Format Selection: Choose the appropriate format for your target system
  3. Uniqueness: Random IDs are unique per resource instance, not globally unique
  4. Security: Use for non-sensitive identifiers; for passwords use random_password
  5. Naming: Use descriptive resource names to clarify the ID’s purpose
  6. Consistency: Use the same format consistently within a configuration
  1. Unique Naming: Generating unique suffixes for resource names
  2. Session IDs: Creating session identifiers for applications
  3. Database Names: Generating unique database or schema names
  4. Cache Keys: Creating cache key prefixes or suffixes
  5. API Keys: Generating non-sensitive API identifiers
  6. Container Names: Ensuring unique container names in multi-instance deployments
  7. Network Isolation: Creating unique network identifiers