Terraform loops


Figure 1042. Using count Slide presentation
Defining 10 server 10 corresponding A-records
resource "hcloud_server" "server" {
  count     = 10
  name      = "www-${count.index}"
  user_data = local_file.user_data[count.index].content
  ...
}
resource "dns_a_record_set" "dnsRecordSet" {
  count     = 10
  zone      = "g03.sdi.hdm-stuttgart.cloud."
  name      = hcloud_server.server[count.index].name
  addresses = [hcloud_server.server[count.index].ipv4_address]
}

exercise No. 18

Creating a fixed number of servers

Q:

Write a Terraform configuration for deploying a configurable number of servers being defined by the following config.auto.tfvars:

dnsZone        = "g03.sdi.hdm-stuttgart.cloud"
serverBaseName = "test"
serverCount    = 3

terraform apply shall create the following:

  • Three DNS entries:

    • test-1.g03.sdi.hdm-stuttgart.cloud

    • test-2.g03.sdi.hdm-stuttgart.cloud

    • test-3.g03.sdi.hdm-stuttgart.cloud

  • Three corresponding servers each endowed with its own unique ssh public host key.

  • Three corresponding sub directories test-1, test-2 and test-3 each containing its own ssh and related known_hosts file.