Using template files
Note
The code for this section is under the chap05/remote-state
directory in the GitHub repo of this book.
Terraform provides a built-in function called templatefile
(read more about it here: https://developer.hashicorp.com/terraform/language/functions/templatefile).
This function takes a file that uses Terraform expressions and evaluates them. We can use variables, resource attributes, and other expressions in the file, and Terraform evaluates those expressions and replaces them with the values in the files. Let’s see this in practice. First, we define a template file. By convention, template files use the *.
tftpl
extension:
chap05/compute-instance/startup.tftpl
#! /bin/bash apt update apt -y install apache2 cat <<EOF > /var/www/html/index.html <html><body><p>Hello World!</p> <p>The CloudSQL connection name is: ${connection_name}</body></html>
During runtime, Terraform evaluates the data...