Terraform modules


Figure 1127. Example: Creating a JSON host meta data file Slide presentation
# main.tf
resource "hcloud_server" "myServer" { ... }

resource "local_file" "hostdata" {
  content = templatefile("Tpl/hostdata.json", {
    ip4      = hcloud_server.mySrv.ipv4_address
    ip6      = hcloud_server.mySrv.ipv6_address
    location = hcloud_server.mySrv.location
  })
  filename   = "Gen/hostdata.json"
}
{ "network": {
     "ipv4": "${ip4}",
     "ipv6": "${ip6}"
  },
  "location": "${location}"}
{
"network": {
  "ipv4": "157.180.78.16",
  "ipv6": "2a01:4f9:c013:be69::1"
  },
  "location": "hel1"
}

Figure 1128. Parent module / sub module layout Slide presentation
├── CreateHostByModule
│   ├── main.tf
│   ├── variables.tf
│   └── Gen
|        └── hostdata.json ◀━━┓
└── Modules                    ┃
    └── HostMetaData           ┃
        ├── main.tf            ┃
        ├── outputs.tf         ┃
        ├── providers.tf       ┃
        ├── Tpl                ┃
        │   └── hostdata.json ━┛
        └── variables.tf

Figure 1129. Parent module implementation Slide presentation
# main.tf
...
module "createHostAmongMetaData" {
  source       = "../Modules/HostMetaData"
  hcloud_token = var.hcloud_token
  server       = hcloud_server.helloServer.id
}

Figure 1130. Sub module implementation, mind ${path.module} ! Slide presentation
# variable.tf

variable "server" {
  description = "The server's id"
  type        = string
}
variable "hcloud_token" {
  type      = string
  nullable  = false
  sensitive = true
}
data "hcloud_server" "server" {
  id = var.server
}

resource "local_file" "hostdata" {
  content    = templatefile("${path.module}/Tpl/hostdata.json", {
    name     = data.hcloud_server.server.name
    ip4      = data.hcloud_server.server.ipv4_address
    ip6      = data.hcloud_server.server.ipv6_address
    location = data.hcloud_server.server.location
  })
  filename        = "gen/${data.hcloud_server.server.name}.json"
  file_permission = "0644"
}

Figure 1131. Sub module, JSON template file Tpl/hostdata.json and result Slide presentation
{
  "network": {
    "ipv4": "${ip4}",
    "ipv6": "${ip6}"
  },
  "location": "${location}"
}
{
  "network": {
    "ipv4": "157.180.78.16",
    "ipv6": "2a01:4f9:c013:be69::1"
  },
  "location": "hel1"
}

Figure 1132. Parent module vs. sub module context Slide presentation
resource "local_file" "hostdata" {
  content = templatefile(
     "${path.module}/Tpl/hostdata.json",{
    ip4 =
      hcloud_server.newServer.ipv4_address
    ip6 =
      hcloud_server.newServer.ipv6_address    
    location = 
      hcloud_server.newServer.location
  })
  filename        = "Gen/${var.name}.json" 
}
├── CreateHostByModule
│   ├── main.tf
│   ├── variables.tf
│   └── Gen
|        └── hostdata.json ◀━━┓
└── Modules                    ┃    
    └── HostMetaData           ┃
        ├── main.tf            ┃
        ├── outputs.tf         ┃
        ├── providers.tf       ┃
        ├── Tpl                ┃
        │   └── hostdata.json ━┛
        └── variables.tf

exercise No. 24

Generating host meta data

Q:

Implement the module based host creation including meta data generation as being outlined in Figure 1132, “Parent module vs. sub module context ”.

exercise No. 25

A module for ssh host key handling

Q:

Create a SshKnownHosts module for generating ssh and scp wrapper scripts among with known_hosts as in Solving the ~/.ssh/known_hosts quirk . This time re factor ssh, scp and known_hosts generation into a Modules/SshKnownHosts module to be used as:

...
resource "hcloud_server" "mySrv" {
  name         = "hello"
  ...
}

module "createSshKnownHosts" {
  source = "../Modules/SshKnownHosts"
  loginUserName        = hcloud_ssh_key.loginUser.name
  serverNameOrIp       = hcloud_server.mySrv.ipv4_address
  serverHostPublicKey  = tls_private_key.host.public_key_openssh
}

Your overall project layout may look like:

.
├── KnownHostsByModule
│   ├── bin
│   │   ├── scp ━━┓
│   │   └── ssh━┓ ┃ depend
│   ├── gen     ▼ ▼
│   │   └── known_hosts
│   ├── main.tf
│   ├── network.tf
│   ├── outputs.tf
│   ├── providers.tf
│   ├── tpl
│   │   └── userData.yml
│   └── variables.tf
└── Modules
    └── SshKnownHosts
        ├── main.tf
        ├── tpl
        │   ├── scp.sh
        │   └── ssh.sh
        └── variables.tf

Red colour indicate generated resources: As in Solving the ~/.ssh/known_hosts quirk both ssh and scp use the generated known_hosts file containing the server's public key.