How to generate UUIDs in Terraform

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

Terraform by HashiCorp is an open-source tool used for building, changing, and versioning infrastructure safely and efficiently. It can manage existing and popular service providers as well as custom in-house solutions. A frequently encountered requirement across various infrastructure projects is the generation of unique identifiers (UUIDs) for different resources. This tutorial will guide you through generating UUIDs in Terraform with multiple code examples, from basic to advanced usage.

Understanding UUIDs

UUID stands for Universal Unique Identifier, which is a 128-bit number used to uniquely identify information in computer systems. The main reason for using UUIDs in Terraform is to ensure that names of resources like cloud instances, security groups, or any other resources are unique and do not clash, especially when you are creating multiple instances of them.

Basic UUID Generation

To start generating UUIDs in Terraform, you would typically use Terraform’s built-in uuid() function.

resource "random_uuid" "example" {
  keepers = {
    id = uuid()
  }
}

This simple code snippet demonstrates how to generate a UUID and use it as a keeper in a random resource. Each time you apply your Terraform configuration, a new, unique UUID will be generated.

UUIDs as Resource Names

Now, let’s see how you can use the generated UUIDs as part of resource names to ensure uniqueness across your Terraform-managed infrastructure.

resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  tags = {
    Name = "Instance-${random_uuid.example.id}"
  }
}

In this example, an AWS EC2 instance is tagged with a unique name using the generated UUID. This ensures that each instance has a unique identifier, eliminating naming collisions.

Advanced Usage: UUIDs with Custom Data

Terraform allows for more complex UUID generation scenarios where you might want to incorporate custom data or specific strings into your UUIDs. For such requirements, you might need to use additional Terraform functions alongside uuid() to construct your desired identifiers.

resource "null_resource" "advanced_example" {
  triggers = {
    id = "custom-${uuid()}-${timestamp()}"
  }
}

This example demonstrates generating a more complex identifier that includes a custom string, the generated UUID, and a timestamp. This technique is useful for creating detailed, unique IDs for logging, auditing, or tracking resources over time.

UUIDs and State Management

While UUIDs offer uniqueness and can help avoid naming collisions, their use in Terraform comes with considerations for state management. As UUIDs generated by Terraform are deterministic and stored in the state file, it’s crucial to understand how Terraform tracks and manages these identifiers to prevent accidental resource duplication or overwrites.

Conclusion

Generating UUIDs in Terraform is a straightforward process that can greatly enhance the management and uniqueness of resources in your infrastructure. By utilizing the uuid() function and understanding its implications on resource naming and state management, you can effectively use UUIDs to avoid naming collisions and maintain an orderly, identifiable infrastructure. With the concepts and examples provided in this tutorial, you are well-equipped to incorporate UUIDs into your Terraform projects.