Manage GitHub with Terraform

By Josh Pollara on Apr 4, 2022
Manage GitHub with Terraform

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

terraform {
required_providers {
github = {
source = "integrations/github"
version = "~> 4.0"
}
}
}

Personal Access Token

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

Provider Configuration

provider "github" {
token = "<personal-access-token>"
owner = "<organization>"
}

Repository

Create a new repository

resource "github_repository" "sre_scripts" {
name = "sre-scripts"
description = "Internal scripts for SRE"
}

Teams

Create a new team

resource "github_team" "sre" {
name = "SRE"
description = "Site Reliability Engineering"
privacy = "closed"
}

Members

Add members to your new team

resource "github_team_membership" "sre_team_membership" {
team_id = github_team.sre.id
username = "<username>"
role = "member"
}

Repository

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

Apply your new changes against your GitHub organization

$ terraform init
$ terraform validate
$ terraform plan
$ terraform apply

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.

We use cookies and similar technologies to provide certain features, enhance the user experience and deliver content that is relevant to your interests. Depending on their purpose, analysis and marketing cookies may be used in addition to technically necessary cookies. By clicking on "Agree and continue", you declare your consent to the use of the aforementioned cookies. Here you can make detailed settings or revoke your consent (in part if necessary) with effect for the future. For further information, please refer to our Privacy Policy .