How to use count with data resources in Terraform

hashicorp terraform snippets

tl;dr version:

"${element(data.github_team.pull.*.id, count.index)}"

While writing a little terraform module to manage github repositories, team permissions and branch protection rules, I ran into an issue where github_team_repository resources need the team ID, but github_branch_protection resources need the team slug.

This was annoying as I wanted the module to have a single variable which served both purposes. I ended up having a variable var.teams_pull which accepted a list of team slugs. Using the github_team data source I was able to derive each team’s ID.

See the team_id parameter on the github_team_repository.pull resource below to see how to access it.

locals {
  pull_count = "${length(var.teams_pull)}"

data "github_team" "pull" {
  count = "${local.pull_count}"
  slug  = "${element(var.teams_pull, count.index)}"

resource "github_team_repository" "pull" {
  count      =  "${local.pull_count}"
  team_id    = "${element(data.github_team.pull.*.id, count.index)}"
  repository = "${}"
  permission = "pull"