0

I'm starting to use Terraform (0.12) with different types of servers, each associated to a different image. I'm trying to simply output all private IP (I don't know the number before runtime) but can't find a simple way to do it (I found a more complicated one).

I'm trying TF on scaleway provider and I have some servers declaration like this (simplified)

# INPUT
variable "srv1" {
  type        = number
  default     = 0
}
variable "srv2" {
  type        = number
  default     = 0
}

# IMAGES
data "scaleway_instance_image" "srv1" {
  name = "srv1"
}
data "scaleway_instance_image" "srv2" {
  name = "srv2"
}

# SERVERS
resource "scaleway_instance_server" "srv1" {
  name              = "srv1_${count.index}"
  image             = data.scaleway_instance_image.srv1.id
  count             = var.srv1
}
resource "scaleway_instance_server" "srv2" {
  name              = "srv2_${count.index}"
  image             = data.scaleway_instance_image.srv2.id
  count             = var.srv2
}

To output private IP from all servers, this is what I do

# OUTPUT
output "srv1_private_ips" {
  value = ["${scaleway_instance_server.srv1.*.private_ip}"]
}

output "srv2_private_ips" {
  value = ["${scaleway_instance_server.srv2.*.private_ip}"]
}

It works but I need to create an output section for each server type. I would like to have the option

  • to loop over a list of server types and output all IPs in one output. Didn't find how
  • to just output all scaleway_instance_server.*.*.private_ip but the double wildcard seems not possible

Any help appreciated.

daks
  • 673
  • 6
  • 23

1 Answers1

0

You have a bit of a misunderstanding of how "count" and the splat ("*") syntax work. Using count, you will create a single resource block for your servers, using count to define how many times that instance will be duplicated. Hence:

resource "scaleway_instance_server" "srv" {
  name              = "srv_${count.index}"
  image             = data.scaleway_instance_image.srv1.id
  count             = 2
}

Will create two resources: scaleway_instance_server.srv[0] and scaleway_instance_server.srv[1]. (Yes, they'll have the same image - that's a hint that this probably isn't the best way to manage this)

Outputting a list of the private_ip attribute from all of the servers becomes trivial:

output "srv_private_ips" {
  value = scaleway_instance_server.srv[*].private_ip
}

NOTE: I've never used the scaleway provider in the example you gave, so I'm just guessing that attribute exists

  • The documentation (https://www.terraform.io/docs/providers/scaleway/r/instance_server.html) and the source code (https://github.com/terraform-providers/terraform-provider-scaleway/blob/master/scaleway/resource_instance_server.go#L119) says - they do. – limakzi Apr 18 '20 at 08:50
  • I don't understand your answer: I need to have multiple servers from the same image, but I also need to have different types of servers with different images. So I still don't get how I can 'loop' over ALL my servers to output their IP. Your example `srv[*]` works in your example but not in mine (or I don't get it). – daks Apr 20 '20 at 09:03
  • Well, the hacky way to do it would be to set your output `value = flatten(scaleway_instance_server.srv1.*.private_ip, scaleway_instance_server.srv2.*.private_ip)`. (flatten() will combine the two lists for you) –  Apr 21 '20 at 16:58
  • @Kyle that may be a solution for my problem, thanks. I'll try that. – daks May 04 '20 at 07:42