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 stay the same across applies, unless you tell them to regenerate with the keepers argument, which we'll get to later.

How Do You Set Up the Random Provider?

Before anything else, you need to declare the provider so you can pull the random resources into the Terraform run:

What Do the Random Provider Resources Look Like in Practice?

Once the provider is set, you can start trying out the resources 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. It's good for unique test values, as long as they're not sensitive. For sensitive values, use the random_password resource below.

random_password

The main use case for random_password is to generate random passwords or strings to improve security. Instead of relying on a human, this resource creates strong, unique passwords based on the rules you 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:

How Do Keepers Control When a Random Value Regenerates?

By design, the values from the random provider stay the same on every new Terraform plan and apply, unless a "keeper" in the config file says otherwise. A keeper references an attribute in your Terraform code, and if that attribute changes, the random value changes with it. The point is to make the random resource behave predictably. For example, you might want an ec2 instance name to stay the same unless the AMI, instance type, or environment type changes:

When Should You Use the Terraform Random Provider?

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.