Creating a GitHub repository using Terraform involves several steps. Before you begin, make sure you have the following
prerequisites:
- GitHub Account: You must have a GitHub account.
- GitHub Personal Access Token: Create a personal access token on GitHub. This token is used by Terraform to authenticate with GitHub.
- Terraform: You must have Terraform installed on your local machine.
After you have the above prerequisites, follow the steps below:
1. Setup Terraform Configuration File:
Create a file named main.tf
and add the following content to it.
terraform {
required_providers {
github = {
source = "integrations/github"
version = "~> 5.0"
}
}
}
# Configure the GitHub Provider
provider "github" {
Token = "Please place your token here"
}
resource "github_repository" "rushi123" {
name = "Git"
description = "My awesome codebase"
visibility = "public"
}
![](https://rushiinfotech.in/wp-content/uploads/2023/08/01-copy-3-1024x411.png)
In this configuration file, we define a github
provider and a github_repository
resource. We use variables for the GitHub personal access token, organization name, repository name, description.
2. Create Variables File:
Create a file named terraform.tfvars and add the following content to it. github_token = “your_github_token” organization = “your_organization_name” repository_name = “your_repository_name” description = “your_repository_description” private = false Replace the placeholders with your actual values.
3. Initialize Terraform:
Run the following command to initialize your Terraform configuration.
terraform init
![](https://rushiinfotech.in/wp-content/uploads/2023/08/03-copy-3-1024x577.png)
4. Create GitHub Repository:
Run the following command to create the GitHub repository. "
terraform apply” Terraform will display the changes to be made and prompt you for confirmation. Type yes
to proceed.
![](https://rushiinfotech.in/wp-content/uploads/2023/08/06-copy-5-1024x402.png)
5. Verify GitHub Repository:
After the terraform apply command completes, you should see the URL of the created repository in the output. Visit this URL to verify that the repository was created successfully.
![](https://rushiinfotech.in/wp-content/uploads/2023/08/07-copy-4-1024x322.png)
Here is the repository which you created through terraform.
![](https://rushiinfotech.in/wp-content/uploads/2023/08/08-copy-4-1024x368.png)
That’s it! You have created a GitHub repository using Terraform.
Note: To destroy the created resources, you can run terraform destroy
and type yes
at the confirmation prompt.
![](https://rushiinfotech.in/wp-content/uploads/2023/08/09-copy-4-1024x401.png)
Successfully, you have destroyed the repository.
![](https://rushiinfotech.in/wp-content/uploads/2023/08/11-copy-4-1024x354.png)