Exploring Terraform configuration resources

Quite frequently, you will require some random data to be generated. This could be default password for a database or a random hostname for your servers. Terraform has random provider that solves this problem.

Of course, completely random values are harmful for Terraform. That's why, the random_id resource generates random string only on creation and then value is kept during updates (unless you change the configuration of this resource). Imagine that we want to pass random hostname to the previously configured template_file user data. We could do it as follows:

resource "random_id" "hostname" { byte_length = 4 } data "template_file" "user_data" { template = "${file("${path.module}/user_data.sh.tpl")}" ...

Get Getting Started with Terraform now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.