hetznercloud / terraform-provider-hcloud

Terraform Hetzner Cloud provider
https://registry.terraform.io/providers/hetznercloud/hcloud/latest
Mozilla Public License 2.0
495 stars 72 forks source link

Unable to get outputs to work #925

Closed TLINDEN closed 4 months ago

TLINDEN commented 4 months ago

Howdy,

I followed the tutorial on https://community.hetzner.com/tutorials/howto-hcloud-terraform and I have a server up and running:

tf show 
# hcloud_server.web:
resource "hcloud_server" "web" {
    allow_deprecated_images    = false
    backup_window              = null
    backups                    = false
    datacenter                 = "hel1-dc2"
    delete_protection          = false
    firewall_ids               = []
    id                         = "46979328"
    ignore_remote_firewall_ids = false
    image                      = "ubuntu-22.04"
    ipv4_address               = "135.181.27.58"
    ipv6_address               = "2a01:4f9:c010:c51d::1"
    ipv6_network               = "2a01:4f9:c010:c51d::/64"
    keep_disk                  = false
    location                   = "hel1"
    name                       = "munculus"
    placement_group_id         = 0
    primary_disk_size          = 20
    rebuild_protection         = false
    server_type                = "cx11"
    shutdown_before_deletion   = false
    status                     = "running"
}

However, output isn't working. When I add:

output "web_servers_status" {
  value = {
    for server in hcloud_server.web:
    server.name => server.status
  }
}

I get these errors:

│ Error: Unsupported attribute
│ 
│   on outputs.tf line 4, in output "web_servers_status":
│    4:     server.name => server.status
│ 
│ Can't access attributes on a primitive-typed value (string).
╵
╷
│ Error: Attempt to get attribute from null value
│ 
│   on outputs.tf line 4, in output "web_servers_status":
│    4:     server.name => server.status
│ 
│ This value is null, so it does not have any attributes.

Here's the web server definition:

resource "hcloud_server" "web" {
  name        = "munculus"
  image       = "ubuntu-22.04"
  server_type = "cx11"
}

Versions used:

Terraform v1.8.2
on linux_amd64
+ provider registry.terraform.io/hetznercloud/hcloud v1.47.0
apricote commented 4 months ago

Hey @TLINDEN,

the for .. in .. expression you currently have only works when hcloud_server.web was a list of servers with count or for_each.

To fix your output, try this instead:

output "web_servers_status" {
  value = server.status
}

Hope this answered your question. I will close the issue but feel free to respond if you still have problems.

TLINDEN commented 4 months ago

Many thanks for the fast reply!

Ok, I understand, that since I only have 1 server defined, I can't loop. But I can't access the one server either. With your variant config of the output I get:

│ Error: Reference to undeclared resource
│ 
│   on outputs.tf line 2, in output "web_servers_status":
│    2:   value = server.status
│ 
│ A managed resource "server" "status" has not been declared in the root module.

So I changed it to "web":

output "web_servers_status" {
  value = web.status
}

but the response is the same:

╷
│ Error: Reference to undeclared resource
│ 
│   on outputs.tf line 2, in output "web_servers_status":
│    2:   value = web.status
│ 
│ A managed resource "web" "status" has not been declared in the root module.
TLINDEN commented 4 months ago

Ok, I need to do it like so:

output "web_servers_status" {
    description = "Status"
    value = hcloud_server.web.status
}
apricote commented 4 months ago

:facepalm: Sorry, too early for writing code. Yes, you need to reference the full path including the resource type and the name, aka hcloud_server.web.status. Sorry for that confusion.