Post AV5561u4kXSy3ekIKm by duponin@udongein.xyz
 (DIR) More posts by duponin@udongein.xyz
 (DIR) Post #AV53B1DQXDTNSq33XE by duponin@udongein.xyz
       2023-04-27T14:46:46.858250Z
       
       0 likes, 1 repeats
       
       any Terraform users to help me? I want to create a module¹ to generate several VM in a cluster and their DNS recordsusing a count for the VM, it works however, I don’t know how I can use VM’s ID to bind with a networkhere’s a snippet of my terraform moduleresource "hcloud_server" "storage" {  count       = var.vault_count  name        = "storage-${count.index}"  image       = "debian-11"  server_type = "cx11"  keep_disk   = false  location    = "nbg1"  ssh_keys    = var.admin_ssh_keys  public_net {    ipv4_enabled = true    ipv6_enabled = true  }  labels = {    "kind" = "storage"  }}resource "hcloud_server_network" "storage-vm-network" {  count      = var.vault_count  server_id  = hcloud_server."storage${count.id}".id  network_id = hcloud_network.storage-net.id  ip         = "10.0.1.101"}obviously, hcloud_server."storage${count.id}".id is invalidShould I use an output?help wanted :cirno_help: :cat_screamhelp: 1: if this the best solution, I dunno
       
 (DIR) Post #AV53m1HJGTdKNBVY4e by VD15@pl.valkyrie.world
       2023-04-27T14:53:37.296385Z
       
       0 likes, 0 repeats
       
       @duponin I think count.index is what you’re looking for?
       
 (DIR) Post #AV53tsUboSddRSTXuq by VD15@pl.valkyrie.world
       2023-04-27T14:55:07.786215Z
       
       0 likes, 0 repeats
       
       @duponin You should be able to index the server resource like an array:server_id = hcloud_server.storage[count.index].id
       
 (DIR) Post #AV53xc6WgdDzlwzaaW by duponin@udongein.xyz
       2023-04-27T14:55:23.642902Z
       
       0 likes, 0 repeats
       
       @VD15 not really, according to linter
       
 (DIR) Post #AV549qsFbBn33T9PDE by duponin@udongein.xyz
       2023-04-27T14:57:49.653396Z
       
       0 likes, 0 repeats
       
       @VD15 it works but I end in "Reference to undeclared resource"
       
 (DIR) Post #AV55234qRaJkXWsuKO by VD15@pl.valkyrie.world
       2023-04-27T15:07:48.523071Z
       
       0 likes, 0 repeats
       
       @duponin could you post the error?
       
 (DIR) Post #AV5561u4kXSy3ekIKm by duponin@udongein.xyz
       2023-04-27T15:08:16.509031Z
       
       0 likes, 0 repeats
       
       @VD15 $ terraform plan╷│ Error: Reference to undeclared resource│ │   on modules/storage/vault.tf line 21, in resource "hcloud_server_network" "storage-vm-network":│   21:   network_id = hcloud_network.storage-net.id│ │ A managed resource "hcloud_network" "storage-net" has not been declared in module.storage.╵
       
 (DIR) Post #AV55Ga8CLXMH6qNo92 by VD15@pl.valkyrie.world
       2023-04-27T15:10:26.905842Z
       
       0 likes, 0 repeats
       
       @duponin Are you creating the network you’re referencing in that module file or elsewhere?
       
 (DIR) Post #AV55Nmb0OxPHnLI6y0 by duponin@udongein.xyz
       2023-04-27T15:11:30.141124Z
       
       0 likes, 0 repeats
       
       @VD15 the network is created at the project root (not in that module)
       
 (DIR) Post #AV5600fCXwyYQfTvXs by VD15@pl.valkyrie.world
       2023-04-27T15:18:38.426089Z
       
       0 likes, 0 repeats
       
       @duponin gotcha. In that case, you’ll need to add a variable to your module’s variables file, reference that variable inside your module and populate it wherever you’re calling the module from:module’s variables.tf:variable "network_id" {  description = "Network ID for the storage VM"}Module file:resource "hcloud_server_network" "storage-vm-network" {  ...  network_id = var.network_id}wherever you’re calling the module from:module "my_storage_servers" {  ...  network_id = hcloud_network.storage-net.id # Or whatever you're calling it :)}
       
 (DIR) Post #AV56uGPBCsfVT2oNpQ by duponin@udongein.xyz
       2023-04-27T15:28:31.064687Z
       
       1 likes, 0 repeats
       
       @VD15 it works!thanksI'll figure out for the rest
       
 (DIR) Post #AV56wgFS5s0yCJPjUW by VD15@pl.valkyrie.world
       2023-04-27T15:29:16.613197Z
       
       1 likes, 0 repeats
       
       @duponin np
       
 (DIR) Post #AV56x7y32AjKARUEFc by bascht@social.yakshed.org
       2023-04-27T15:21:55Z
       
       0 likes, 0 repeats
       
       @duponin Just from a quick peek it looks like you want to have hcloud_server.storage.*.id[count.index] in there.
       
 (DIR) Post #AV56x90ZAIL3OXfkoa by duponin@udongein.xyz
       2023-04-27T15:29:08.120221Z
       
       0 likes, 0 repeats
       
       @bascht I got help, but thank you anywayhttps://udongein.xyz/notice/AV55zuOzs5QAqHrEky