l1lvink
l1lvink

Reputation: 13

Combine two sets of IPs to form a single output Terraform

I have two Terraform resources creating several EC2s each (one Linux EC2s, the other Windows), and they each have outputs for their IPs (this one for Linux, Windows has target_windows instead of target):

output "target_ips" {
  value = aws_instance.target.*.private_ip
}

I want to combine the two outputs into a single output for easier access later own the line. Anyone know how to do this?

Upvotes: 1

Views: 1264

Answers (1)

Martin Atkins
Martin Atkins

Reputation: 74239

Terraform's setunion function can combine two sets into a new set containing all of the values from both sets:

output "target_ips" {
  value = setunion(
    aws_instance.target.*.private_ip,
    aws_instance.target_windows.*.private_ip,
  )
}

In this case there can't be common values in both of these inputs by definition, because the IP addresses are unique per instance, but for completeness I'll note that due to the rules of sets the setunion result will only include each unique value once, even if it appears in both of the inputs.

Upvotes: 3

Related Questions