Hi there!
Today, we will learn how to use a random integer generator in terraform module.
Situation -
You are writing a module which takes a url as variable but needs to add a random userId (or any other thing) to the url. How do you achieve this in your terraform module?
Pre-requisites
This post is NOT covering basics of terraform and module writing so some understanding of terraform is expected.
It is assumed that you have terraform setup on your system and have an idea about how to write module.
Steps
- Example module
Consider following datadog synthetics module
resource "datadog_synthetics_test" "test_template" {
name = var.name
request_definition {
method = var.request_method
url = var.start_url
}
// rest of the module
}
The above is a sample for creating a synthetic monitoring test on datadog.
Assume you want to append a parameter userId
with random 6 digit number to the url.
- Add random_integer resource
Add a resource for random integer
resource "random_integer" "random_user_id" {
max = 999999
min = 100000
keepers = {
test_name = var.name
}
The documentation for random integer.
The above resource generates a 6-digit integer between 100000 and 999999. The keepers
field make sure that a new random number is generated each time a new var.name
is passed to module.
- Link random_integer resource to the example module
update the url
field to
url = "${var.start_url}/${random_integer.random_user_id.result}"
- Checking
Use your module as usual and verify.
That's all!
Top comments (0)