Managing a GitHub organization for any sized company can be complicated. This is especially true when you have many teams and projects. Leveraging Terraform and its GitHub provider can make management of a GitHub organization a breeze.
In order to start using the Terraform GitHub provider, you need to first set up the required provider and configure authentication. There are multiple ways to authenticate against GitHub. We’ll cover authentication with a Personal Access Token in this tutorial.
terraform {
required_providers {
github = {
source = "integrations/github"
version = "~> 4.0"
}
}
}
Create a new Personal Access Token. Make sure to configure your desired scopes.
provider "github" {
token = "<personal-access-token>"
owner = "<organization>"
}
Create a new repository
resource "github_repository" "sre_scripts" {
name = "sre-scripts"
description = "Internal scripts for SRE"
}
Create a new team
resource "github_team" "sre" {
name = "SRE"
description = "Site Reliability Engineering"
privacy = "closed"
}
Add members to your new team
resource "github_team_membership" "sre_team_membership" {
team_id = github_team.sre.id
username = "<username>"
role = "member"
}
Grant your team access to your repository
resource "github_team_repository" "sre_team_repo" {
team_id = github_team.sre.id
repository = github_repository.sre_scripts.name
permission = "push"
}
Apply your new changes against your GitHub organization
$ terraform init
$ terraform validate
$ terraform plan
$ terraform apply
This is a very basic example of how to manage your GitHub organization with Terraform.
Give Terrateam a try for free to manage your IaC lifecycle.