
Create GitHub Repository through Terraform – Step-by-Step Guide
Sep 20
2 min read
0
21
0
Managing infrastructure and development workflows manually often leads to inconsistency and inefficiency. This is where Terraform, a powerful Infrastructure as Code (IaC) tool, comes in. While most people associate Terraform with cloud infrastructure like AWS, Azure, or GCP, it can also be used effectively with GitHub to automate repository creation and management.
In this guide, we’ll go step by step on how to create a GitHub repository using Terraform.
📌 Prerequisites
Before we begin, make sure you have:
A GitHub account.
Terraform installed on your system (Download link - https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli
A GitHub Personal Access Token with admin permissions.
⚙️ Step 1: Configure the GitHub Provider
Terraform works with different providers, and for this use case and blog, we’ll use the GitHub provider.
Create a provider.tf file:
terraform {
required_providers {
github = {
source = "integrations/github"
version = "~> 5.0"
}
}
}
provider "github" {
token = var.github_token
owner = var.github_owner
}
Note - Here, we’re authenticating Terraform with GitHub using a token and specifying the repository owner.
⚙️ Step 2: Define Variables
In variables.tf, define variables for flexibility:
variable "github_token" {
type = string
description = "GitHub Personal Access Token"
}
variable "github_owner" {
type = string
description = "GitHub username or organization name"
}
variable "repo_name" {
type = string
description = "Name of the repository to be created"
default = "terraform-github-demo"
}
⚙️ Step 3: Write Repository Resource Code
In main.tf, add the GitHub repository resource:
resource "github_repository" "example" {
name = var.repo_name
description = "Repository created using Terraform"
visibility = "public"
auto_init = true
}
This tells Terraform to create a new GitHub repository with the defined name, description, and visibility.
⚙️ Step 4: Initialize and Apply
Now, run the following commands from your terminal:
# terraform init
# terraform plan
# terraform apply
Terraform will authenticate with GitHub, create the repository, and confirm the setup. 🎉
⚙️ Step 5: Verify
Head over to your GitHub account, and now you’ll see the newly created repository with the settings you defined in your Terraform configuration.
🧹 Step 6: Destroy (Optional)
To delete the repository managed by Terraform, simply run:
# terraform destroy
🎥 Video Tutorial
For a complete walkthrough, check out my step-by-step YouTube tutorial here:👉 Create GitHub Repository through Terraform - YouTube
✅ Conclusion
Using Terraform with GitHub makes repository management automated, consistent, and repeatable. This approach is especially powerful for teams managing multiple repositories or integrating GitHub with CI/CD pipelines.
By following this guide, you’ve learned how to:
Configure the GitHub provider in Terraform
Use variables for flexibility
Create a GitHub repository automatically
Manage and destroy GitHub repositories with ease






