Github self-service with Terraform

Share on: linkedin copy

It is late Friday evening; I am sitting in Schiphol Airport and my brain is still processing information from my visit at our Amsterdam office. One thing I noticed, was the similarity between our teams use and use cases of Terraform. I am thinking to myself, this specific use case might inspire you and why not share it? Before this turns into one of those food blogs, where you get the authors life story before getting to the recipe let’s move on.

At BESTSELLER, we are using Github as our main version control system. For us it is a great collaboration tool making it easy to collaborate between teams and projects. In our Github we structure repository permissions and ownership by allocating people and repositories to teams. But to make the collaboration as efficient and easy as possible we need to keep of track of which people belong to which teams and which repositories should be assigned to a specific team…. To be honest this was a horrible task for us in "Engineering" (which is our enabling(something else) team) .

That is why we wanted to enable our fellow developers to maintain this information by themselves. That empowers the teams to self manage, and saves us from repetitive, time wasting work.

As we already manage a bunch of our stuff with Terraform, we thought that it would be pretty neat to keep going in that direction and use Terraform for our small self-service. Aaaand "Github-teams" were born!

We had a few requirements that needed to be fulfilled:

  • As we are using SSO with Azure AD, we needed to assign a Github license to specific Azure users.
  • Assigning users to a github team (which in reality is mapping an Azure AD Group to a Github team).
  • Create new teams or modify existing teams, based on Azure groups.

To visualize it, the flow in its simplest form looks like:

Configuration check
As seen above, we use our Azure AD as the source of truth and then map the groups to Github.


Now that we have the concepts and requirements nailed, let's take a look at how we actually do it. Our Terraform consists of 3 base modules and a folder with all the teams.

1|-- github-teams/ # one file for each team to contributing a bit easier.
2    |--
3    |--
4|-- modules/
5    |-- azure # base module maintaining groups and members in Azure AD
6    |-- github # base module maintaining groups and group mappings between Github and Azure AD
7    |-- team # base module combining Azure and Github based on the input from the github-teams folder. # All github members. (if you are not on a team you can still be a part of the organisation)

If we start with the base modules, we have created them generic enough that they can be called for each team without having to write the same code multiple times.

 2# taking care of groups and users in azure
 4data "azuread_users" "users" {
 5  user_principal_names = var.members
 8# manage azure group and members
 9resource "azuread_group" "group" {
10  display_name     =
11  description      = var.description == "" ? "Grants access to the GitHub Team '${var.team_name}'" : var.description
12  members          = data.azuread_users.users.object_ids
13  security_enabled = true
 3# create/manage a github team
 4resource "github_team" "team" {
 5  name                      =
 6  description               = var.description
 7  privacy                   = "closed"
 8  parent_team_id            = var.parent != null ? var.parent : null
 9  create_default_maintainer = var.create_default_maintainer
12# map with Azure AD group
13resource "github_team_sync_group_mapping" "mapping" {
14  team_slug                 =
16  group {
17    group_id          = var.group_id
18    group_name        = var.group_name
19    group_description = var.description
20  }
 2# we invoke the Azure and Github module for each team and make sure they map 1-1.
 4module "azure" {
 5  source = "../azure"
 7  name      = local.azure_group_name
 8  team_name =
 9  members   = var.members
12module "github" {
13  source = "../github"
15  name                      =
16  description               = var.description
17  group_id                  =
18  group_name                =
19  parent                    = var.parent != null ? var.parent : null
20  create_default_maintainer = var.create_default_maintainer

Beside the base modules the most important part is the github-teams folder where we have a file for each team. The only information needed is the Team Name and the members. Easy right?

 1# example for "Team A" in the github-teams folder.
 3module "team_a" {
 4  source = "../modules/team"
 6  name = "Team A"
 7  members = [
 8    "",
 9    "",
10  ]

Using the self-service

So if you are a developer in BESTSELLER and you need to add a new colleague, it is as easy as making a pull request with two changes:

  1. Adding the person to the members file
  2. Assigning the person to a team.

Wait for review and approval and the CI will take care of the rest!

About the author

Peter Brøndum

My name is Peter Brøndum, I work as a Tech Lead and Scrum Master in a platform engineering team at BESTSELLER. Our main priority is building a development highway, with paved roads, lights and signs, so our colleagues can deliver value even faster. Besides working at BESTSELLER, I — amongst other things, am automating my own home, and yes, that is, of course, running on Kubernetes as well.