Random UUID
The random_uuid resource generates universally unique identifiers (UUIDs) following the RFC 4122 standard. It provides deterministic UUID generation that remains consistent across multiple runs.
Use Cases
Section titled “Use Cases”As a lab author, you can use random_uuid resources to:
- Unique Identifiers: Generate globally unique resource identifiers that never conflict across lab instances
- Database Keys: Create unique primary keys, foreign keys, or correlation IDs for database operations
Random UUID resources provide RFC 4122 compliant UUIDs with guaranteed uniqueness and consistent generation.
HCL Syntax
Section titled “HCL Syntax”Basic Syntax
Section titled “Basic Syntax”resource "random_uuid" "name" {}
Full Syntax
Section titled “Full Syntax”resource "random_uuid" "name" {}
Fields
Section titled “Fields”This resource takes no input parameters. UUIDs are generated automatically.
Computed Attributes
Section titled “Computed Attributes”These attributes are set by the system after UUID generation:
Field | Type | Description |
---|---|---|
value |
string | The generated UUID in standard format (e.g., "550e8400-e29b-41d4-a716-446655440000") |
Validation Rules
Section titled “Validation Rules”- UUIDs follow the RFC 4122 standard format
- Generated values remain constant across multiple runs (idempotent)
- UUIDs are globally unique with extremely low collision probability
- Standard format includes hyphens: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Examples
Section titled “Examples”Simple UUID Generation
Section titled “Simple UUID Generation”resource "random_uuid" "session_id" {}
output "session_id" { value = resource.random_uuid.session_id.value # e.g., "550e8400-e29b-41d4-a716-446655440000"}
Database Record ID
Section titled “Database Record ID”resource "random_uuid" "user_id" {}
resource "template" "user_record" { source = <<-EOF INSERT INTO users (id, name, email) VALUES ( '${resource.random_uuid.user_id.value}', -- e.g., 'a1b2c3d4-e5f6-4321-9876-123456789abc' 'John Doe', 'john@example.com' ); EOF
destination = "./init_user.sql"}
Container Naming
Section titled “Container Naming”resource "random_uuid" "container_id" {}
resource "container" "app" { image { name = "myapp:latest" }
environment = { INSTANCE_ID = resource.random_uuid.container_id.value # e.g., "550e8400-e29b-41d4-a716-446655440000" }}
API Correlation IDs
Section titled “API Correlation IDs”resource "random_uuid" "correlation_id" {}
resource "http" "api_request" { method = "POST" url = "https://api.example.com/data"
headers = { "Content-Type" = "application/json" "X-Correlation-ID" = resource.random_uuid.correlation_id.value # e.g., "a1b2c3d4-e5f6-4321-9876-123456789abc" }
payload = jsonencode({ request_id = resource.random_uuid.correlation_id.value # e.g., "a1b2c3d4-e5f6-4321-9876-123456789abc" data = "lab_data" })}
Multiple UUID Usage
Section titled “Multiple UUID Usage”resource "random_uuid" "lab_instance_id" {}resource "random_uuid" "transaction_id" {}resource "random_uuid" "session_token" {}
resource "template" "lab_config" { source = <<-EOF lab: instance_id: ${resource.random_uuid.lab_instance_id.value} # e.g., "123e4567-e89b-12d3-a456-426614174000" transaction_id: ${resource.random_uuid.transaction_id.value} # e.g., "987fcdeb-51a2-43d1-9b77-332168459012" session_token: ${resource.random_uuid.session_token.value} # e.g., "456789ab-cdef-1234-5678-90abcdef1234"
database: connection_id: ${resource.random_uuid.lab_instance_id.value}
logging: trace_id: ${resource.random_uuid.transaction_id.value} EOF
destination = "./lab-config.yaml"}
Message Queue Integration
Section titled “Message Queue Integration”resource "random_uuid" "queue_id" {}resource "random_uuid" "consumer_id" {}
resource "container" "message_producer" { image { name = "rabbitmq:management" }
environment = { RABBITMQ_DEFAULT_QUEUE = "lab-queue-${substr(resource.random_uuid.queue_id.value, 0, 8)}" # e.g., "lab-queue-abc12345" CONSUMER_ID = resource.random_uuid.consumer_id.value # e.g., "def67890-1234-5678-9abc-def123456789" }}
Cloud Resource Naming
Section titled “Cloud Resource Naming”resource "random_uuid" "resource_suffix" {}
resource "terraform" "aws_resources" { source = "./terraform"
variables = { # Use UUID for globally unique resource names bucket_name = "lab-bucket-${substr(resource.random_uuid.resource_suffix.value, 0, 12)}" # e.g., "lab-bucket-550e8400e29b" vpc_name = "lab-vpc-${substr(resource.random_uuid.resource_suffix.value, 24, 8)}" # e.g., "lab-vpc-44665544" cluster_id = resource.random_uuid.resource_suffix.value # e.g., "550e8400-e29b-41d4-a716-446655440000" }}
Best Practices
Section titled “Best Practices”- Global Uniqueness: Use UUIDs when you need guaranteed global uniqueness
- Substring Usage: Extract portions of UUIDs for shorter identifiers when full UUID isn’t needed
- Consistent Format: Use standard UUID format (with hyphens) for maximum compatibility
- Resource Naming: Combine UUIDs with descriptive prefixes for readable resource names
- Database Design: Use UUIDs as primary keys for distributed systems
- Logging: Include UUIDs in logs for correlation and tracing
- Documentation: Document UUID usage and purpose in your lab configuration