random_integer (Resource)
July 3, 2025 ยท View on GitHub
The resource random_integer generates random values from a given range, described by the min and max attributes of a given resource.
This resource can be used in conjunction with resources that have the create_before_destroy lifecycle flag set, to avoid conflicts with unique names during the brief period where both the old and new resources exist concurrently.
Example Usage
# The following example shows how to generate a random priority
# between 1 and 50000 for a aws_alb_listener_rule resource:
resource "random_integer" "priority" {
min = 1
max = 50000
keepers = {
# Generate a new integer each time we switch to a new listener ARN
listener_arn = var.listener_arn
}
}
resource "aws_alb_listener_rule" "main" {
listener_arn = random_integer.priority.keepers.listener_arn
priority = random_integer.priority.result
action {
type = "forward"
target_group_arn = var.target_group_arn
}
# ... (other aws_alb_listener_rule arguments) ...
}
Schema
Required
max(Number) The maximum inclusive value of the range.min(Number) The minimum inclusive value of the range.
Optional
keepers(Map of String) Arbitrary map of values that, when changed, will trigger recreation of resource. See the main provider documentation for more information.seed(String) A custom seed to always produce the same value.
Read-Only
id(String) The string representation of the integer result.result(Number) The random integer result.
Import
Import is supported using the following syntax:
The terraform import command can be used, for example:
# Random integers can be imported using the result, min, and max, with an
# optional seed. This can be used to replace a config value with a value
# interpolated from the random provider without experiencing diffs.
# Example (values are separated by a ,):
terraform import random_integer.priority 15390,1,50000