Blog post

Manage GitHub with Terraform

2022-04-04

6 minute read

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.

Provider Setup and Authentication

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.

Required provider

1terraform {
2  required_providers {
3    github = {
4      source  = "integrations/github"
5      version = "~> 4.0"
6    }
7  }
8}
9

Personal Access Token

Create a new Personal Access Token. Make sure to configure your desired scopes.

Provider Configuration

1provider "github" {
2  token = "<personal-access-token>"
3  owner = "<organization>"
4}
5

Repository

Create a new repository

1resource "github_repository" "sre_scripts" {
2  name        = "sre-scripts"
3  description = "Internal scripts for SRE"
4}
5

Teams

Create a new team

1resource "github_team" "sre" {
2  name        = "SRE"
3  description = "Site Reliability Engineering"
4  privacy     = "closed"
5}
6

Members

Add members to your new team

1resource "github_team_membership" "sre_team_membership" {
2  team_id  = github_team.sre.id
3  username = "<username>"
4  role     = "member"
5}
6

Repository

Grant your team access to your repository

1resource "github_team_repository" "sre_team_repo" {
2  team_id    = github_team.sre.id
3  repository = github_repository.sre_scripts.name
4  permission = "push"
5}
6

Apply

Apply your new changes against your GitHub organization

1$ terraform init
2$ terraform validate
3$ terraform plan
4$ terraform apply
5

That's it

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.

Ready, Steady, Go!

Try Terrateam

The complete Terraform CI/CD platform on GitHub