learn-terraform

Course outlines for learning terraform.

View the Project on GitHub robertdebock/learn-terraform

Templates

expected time requirements
45 minutes A computer with Terraform installed, terraform knowledge.

Goal: Use Templates to customize your usage of Terraform.

Explanation

Sometimes, you want Terraform to save a file with details from a Terraform run, such as an IP address. Terraform offers many functions one of which is the templatefile function.

Howto

These templates can be used in (for example) remote_file or local_file.

Here is an example to store an IP address of hosts managed by Terraform:

inventory.tf:

resource "local_file" "inventory" {
    content  = templatefile("templates/inventory.tpl", { ip = data.azurerm_public_ip.ipaddress.ip_address })
    filename = "${path.module}/inventory"
}

templates/inventory.tpl:

${ip}

Demo

Have a look here.

Assignment

You can start using this Terraform configuration. (Remember: az login, terraform init)

Host ${ip}
  User ${username}

A GCP example to get you started.

Questions:

  1. How can you pin the version of local_file?
  2. Do you know a use case for using templates?
  3. Will the local_file be remove when running terraform destroy?

Conclusion

You can use generated content or input variables to create file, both locally and remote.

Solution