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")}" vars { packages = "${var.extra_packages}"...