TrademarkTrademark
Features
Documentation

Terraform Random Provider

Use the Random Provider to generate values
Ryan FeeOctober 2, 2024
Terraform Random Provider
Key takeaways
  • The Terraform random provider generates random values for use cases like unique resource names, random passwords or API keys, and selecting random list elements.
  • It supports seven resources: random_id, random_integer, random_string, random_password, random_shuffle, random_pet, and random_uuid.
  • Random values are generated during the plan phase and stay consistent across applies unless changed via the keepers argument.
  • Keepers reference a Terraform attribute that, when changed, regenerates the random value, making the resource behave predictably.

The "random" Terraform provider generates random values inside your Terraform or OpenTofu configurations. It's handy when you need a unique or arbitrary value but don't want to hardcode it. A few things people use it for:

  • Creating unique names for resources
  • Generating random passwords or API keys
  • Selecting random elements from a list

There are seven resources that the random provider supports in the Terraform configuration:

  • random_id: Generates a random identifier
  • random_integer: Produces a random integer within a specified range
  • random_string: Creates a random string of characters
  • random_password: Generates a random password
  • random_shuffle: Randomly shuffles a list of strings
  • random_pet: Randomly generate names
  • random_uuid: Randomly generates a UUID

The random values are generated during the plan phase and remain consistent across applies, unless explicitly set to regenerate through the keepers argument which is discussed later in the blog.

Set the Provider

Before doing anything, you always need to declare the provider to ensure you can pull the random resources into the Terraform run:

Examples

Once the Terraform provider is set, you can start testing out the various provider resources as seen below:

random_id

The main use case for the random_id resource is to create random resource IDs, such as appending an ID to a name. This will allow you to keep a consistent naming convention:

random_integer

The main use case for the random_integer resource is to create a random integer for testing scenarios and simulations.

random_string

The main use case for the random_string resource is to generate random strings for input. This helps to create unique values for testing, especially useful for non-sensitive values. Sensitive values would be more appropriate for the random_password resource below.

random_password

The main use case for random_password is to generate random passwords or strings to improve security. Rather than relying on a human, this resource will create strong unique passwords based on the set of rules that are set:

random_shuffle

The main use case for random_shuffle is to randomly shuffle a list of inputs. A use case for this is distributing resources across different AZs for high availability:

random_pet

The main use case for this is to provide an easy way to generate unique, memorable names for resources without having to come up with them manually.

random_uuid

Nothing else is needed besides the resource when using random_UUID, as it calls go-uuid to generate the UUID:

Keepers

By design, the values generated as part of the random provider will remain the same during every new Terraform plan and apply, unless told otherwise by a "keeper" in the configuration file. Keepers reference an attribute in the Terraform code, that if changed, will change the random value. The goal is to make the value of the random resource predictable in how it behaves. For example, you might want the name of an ec2 instance to remain the same unless the AMI, instance type, or environment type changes:

Summary

The random Terraform provider is useful whenever you need generated values for testing and simulation, with the rules defined in code instead of left to a person. Once you've tried the basic resources, spend some time on the keepers argument. It's the piece that decides when a random value stays put and when it regenerates, and that behavior is easy to get wrong if you skip it.

About the author
Ryan Feedirector of platform engineering at Scalr
Ryan Fee is the director of platform engineering at Scalr, with over 15 years of experience improving infrastructure experiences at companies large and small.