equinix / terraform-equinix-metal-anthos-on-vsphere

[Deprecated] Automated Anthos Installation via Terraform for Equinix Metal with vSphere
https://registry.terraform.io/modules/equinix/anthos-on-vsphere/metal/latest
Apache License 2.0
62 stars 41 forks source link

can't destroy after a failed deployment #83

Closed displague closed 4 years ago

displague commented 4 years ago

After a failed provision, the index on reserved IP blocks can reference an invalid index, preventing terraform destroy from succeeding.

$ terraform apply
...
Error: Device in non-active state "failed"

  on 04-esx-hosts.tf line 1, in resource "packet_device" "esxi_hosts":
   1: resource "packet_device" "esxi_hosts" {
$ terraform destroy
...
Error: Error in function call

  on 04-esx-hosts.tf line 15, in resource "packet_device" "esxi_hosts":
  15:     reservation_ids = [element(packet_reserved_ip_block.esx_ip_blocks.*.id, count.index)]
    |----------------
    | count.index is 0
    | packet_reserved_ip_block.esx_ip_blocks is empty tuple

Call to function "element" failed: cannot use element function with an empty
list.
displague commented 4 years ago

I dressed this up as follows:

diff --git a/04-esx-hosts.tf b/04-esx-hosts.tf
index 7007d3a..d90acec 100644
--- a/04-esx-hosts.tf
+++ b/04-esx-hosts.tf
@@ -12,7 +12,7 @@ resource "packet_device" "esxi_hosts" {
   ip_address {
     type            = "public_ipv4"
     cidr            = 29
-    reservation_ids = [element(packet_reserved_ip_block.esx_ip_blocks.*.id, count.index)]
+    reservation_ids = try([element(packet_reserved_ip_block.esx_ip_blocks.*.id, count.index)], null)
   }
   ip_address {
     type = "private_ipv4"

And was then able to terraform refresh.

I haven't thought through whether or not this is a suitable value under this condition. I was able to destroy the resources afterward.

Closing this, but we may want to revisit this solution if others report this problem.