Terraform is an Infrastructure as Code (IaC) tool that allows you to define and manage infrastructure using declarative configuration files. It is commonly used to manage cloud resources, but it can also be used to manage other services like GitHub. In this article, we'll explore how to import an existing GitHub organization into Terraform using the provided Bash script from the [locus313/terraform-import-github-organization](https://github.com/locus313/terraform-import-github-organization) GitHub repository. ## Prerequisites Before we begin, ensure you have the following: 1. **Terraform Installed**: If you don't have Terraform installed, you can follow the official [Terraform installation guide](https://learn.hashicorp.com/terraform/getting-started/install.html). 2. **GitHub Token**: Generate a personal access token from GitHub with the necessary permissions to manage the organization. 3. **Existing GitHub Organization**: You should have a GitHub organization that you want to import into Terraform. 4. **jq Installed**: A lightweight and flexible command-line JSON processor, used in the script. Install it using your package manager (e.g., `sudo apt-get install jq` on Debian-based systems). ## Steps to Import GitHub Organization into Terraform ### Step 1: Clone the Repository First, clone the `terraform-import-github-organization` repository to your local machine: ```bash git clone https://github.com/locus313/terraform-import-github-organization.git cd terraform-import-github-organization ``` ### Step 2: Review the Bash Script The core of the process is managed by the `terraform-import-github-org.sh` script. This script automates the import of existing GitHub organization resources into Terraform. Here is a brief overview of what the script does: 1. **Fetches Repositories**: Lists all repositories in the specified GitHub organization. 2. **Fetches Teams**: Lists all teams within the organization. 3. **Fetches Team Memberships**: Lists members for each team. 4. **Generates Terraform Configuration**: Outputs the necessary Terraform configuration for the fetched resources. 5. **Imports Resources into Terraform**: Uses the `terraform import` command to bring the existing resources under Terraform management. ### Step 3: Set Up Environment Variables The script relies on environment variables to function correctly. Set the following environment variables in your shell: ```bash export GITHUB_TOKEN="your_github_token_here" export GITHUB_ORG="your_github_organization_name" ``` ### Step 4: Run the Script Make the script executable and run it: ```bash chmod +x terraform-import-github-org.sh ./terraform-import-github-org.sh ``` ### Step 5: Initialize Terraform After running the script, it will generate a `main.tf` file containing the configuration for your GitHub organization. Initialize Terraform in the directory: ```bash terraform init ``` ### Step 6: Plan and Apply Before applying the changes, you can use the `terraform plan` command to see what changes Terraform will make: ```bash terraform plan ``` If everything looks good, apply the changes using: ```bash terraform apply ``` ### Conclusion By following the steps outlined above, you can successfully import and manage your GitHub organization using Terraform and the provided Bash script. This approach leverages Terraform's powerful IaC capabilities to manage not only cloud resources but also your GitHub organization, ensuring consistency and repeatability. For more details and advanced configurations, refer to the [locus313/terraform-import-github-organization](https://github.com/locus313/terraform-import-github-organization) repository.