hello kitty
hello kitty

Reputation: 21

Is there a way to pass attributes to data source in terraform?

I'm trying to tell data.github_ip_ranges to what name to use so I could create a list of CIDRs and my code look cleaner. I was trying to find answers, but no luck so far.

And I'm trying to see if there is a way of passing my variables to it...

variable "git_services" {
  default = ["hooks_ipv4", "dependabot_ipv4", "dependabot_ipv6", "git_ipv4", "hooks_ipv6"]
}

locals {
  github_ips = concat(data.github_ip_ranges.git.name) # name is my custom variable
}

Here is my original approach

locals {
  github_ips = concat(data.github_ip_ranges.git.hooks_ipv4, data.github_ip_ranges.git.hooks_ipv6, 
  data.github_ip_ranges.git.dependabot_ipv4, data.github_ip_ranges.git.dependabot_ipv6)

}

Please help if you could. Thank you!

Upvotes: 0

Views: 1055

Answers (1)

Jordan
Jordan

Reputation: 4472

I think I understand what you're trying to accomplish. You would do it like so:

variable "git_services" {
  default = ["hooks_ipv4", "dependabot_ipv4", "dependabot_ipv6", "git_ipv4", "hooks_ipv6"]
}

locals {
  github_ips = distinct(flatten([
    for service in var.git_services:
    data.github_ip_ranges.git[service]
  ]))
}

What this is doing is creating a list of lists, where each element in the first level is for a service, and each element in the second level is a CIDR bock for that service. The flatten function turns this list of lists into a flat list of CIDR blocks. Since the same CIDR might be used for multiple services, and we probably don't want duplicates if we're using this for something like security group rules, we use the distinct function to remove any duplicate CIDR blocks.

Upvotes: 1

Related Questions