top of page
  • Facebook
  • Twitter
  • Linkedin

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:


⚙️ 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

Sep 20

2 min read

0

21

0

Related Posts

Comments

Share Your ThoughtsBe the first to write a comment.

Contact Us

Thanks for submitting!

 Address. 500 Terry Francine Street, San Francine, CA 94158

Tel. 123-456-7890

© 2035 by ITG. Powered and secured by Wix

bottom of page