Okay, here's the Terraform code and explanation, but with a **very important clarification**: **Terraform cannot *create* a GitHub Organization itself.** The organization entity must already exist in GitHub (created manually via the GitHub website). Terraform's GitHub provider is designed to manage resources *within* an existing organization (like settings, teams, members, repositories, etc.). Therefore, the Terraform code I can provide will help you **manage the settings** of an organization *after* you have created it manually. **Steps:** 1. **Manually Create the Organization:** Go to `https://github.com/organizations/new` (or your GitHub Enterprise equivalent) and create the organization you want to manage. 2. **Generate a Personal Access Token (PAT):** * Go to your GitHub Settings -> Developer settings -> Personal access tokens -> Tokens (classic). * Generate a new token. * Give it a descriptive name (e.g., `terraform-org-admin`). * Grant it the `admin:org` scope. This is crucial for managing organization settings. * **Copy the token immediately.** You won't be able to see it again. 3. **Use the Terraform Code Below:** --- **`main.tf`** ```terraform terraform { required_providers { github = { source = "integrations/github" version = "~> 6.0" # Use a recent version } } } # --- Variables --- variable "github_token" { type = string description = "GitHub Personal Access Token with admin:org scope." sensitive = true # It's strongly recommended to set this via an environment variable (TF_VAR_github_token or GITHUB_TOKEN) # or a secrets manager, not directly in a tfvars file unless secured. } variable "github_organization_name" { type = string description = "The name (login) of the GitHub organization (must be created manually first)." # Example: "my-cool-org" } variable "billing_email" { type = string description = "The billing email address for the organization." # Example: "billing@my-cool-org.com" } variable "company_name" { type = string description = "The display name for the company." default = null # Optional # Example: "My Cool Company, Inc." } variable "org_description" { type = string description = "The description of the organization." default = "Managed by Terraform" # Optional, provide a default or null } variable "blog_url" { type = string description = "The URL of the organization's blog or website." default = null # Optional # Example: "https://blog.my-cool-org.com" } variable "members_can_create_repos" { type = bool description = "Whether members can create public/private repositories." default = false # Defaulting to a more restrictive setting } # --- Provider Configuration --- provider "github" { token = var.github_token owner = var.github_organization_name # Specify the organization to manage # base_url = "https://github.example.com/api/v3/" # Uncomment for GitHub Enterprise } # --- Resource Definition --- # Manages the settings of an EXISTING organization resource "github_organization_settings" "org_settings" { billing_email = var.billing_email company = var.company_name blog = var.blog_url description = var.org_description # twitter_username = "mycoolorg" # Optional # location = "San Francisco, CA" # Optional # has_organization_projects = true # Optional # has_repository_projects = true # Optional # --- Member Privileges --- members_can_create_repositories = var.members_can_create_repos # members_can_create_public_repositories = true # Be specific if needed # members_can_create_private_repositories = true # Be specific if needed # members_can_fork_private_repositories = false # Optional # --- Repository Defaults --- # default_repository_permission = "read" # Options: "read", "write", "admin", "none" # members_allowed_repository_creation_type = "all" # Options: "all", "private", "none" } # --- Outputs (Optional) --- output "organization_id" { description = "The Node ID of the managed GitHub organization." value = github_organization_settings.org_settings.id # This is the Node ID, not the numeric ID } output "organization_login" { description = "The login name of the managed GitHub organization." value = var.github_organization_name # Directly from input variable } ``` **`terraform.tfvars.example`** (Do not commit your real token here!) ```hcl # Rename this file to terraform.tfvars and fill in your values # Or set environment variables: # export TF_VAR_github_token="ghp_YOUR_TOKEN_HERE" # export TF_VAR_github_organization_name="your-newly-created-org" # export TF_VAR_billing_email="billing@example.com" # github_token = "ghp_YOUR_TOKEN_HERE" # Better as ENV VAR github_organization_name = "your-newly-created-org" billing_email = "billing@example.com" company_name = "My Awesome Company" org_description = "This organization holds awesome projects, managed by Terraform." blog_url = "https://example.com" members_can_create_repos = false ``` **Explanation:** 1. **`terraform { ... }`**: Declares the required GitHub provider and pins it to a compatible version range. 2. **`variable "..." { ... }`**: Defines input variables: * `github_token`: Your PAT (marked `sensitive`). **Best practice:** Set this using the `TF_VAR_github_token` or `GITHUB_TOKEN` environment variable. * `github_organization_name`: The exact login name of the organization you created manually on GitHub. * `billing_email`: Required by the `github_organization_settings` resource. * Other optional variables (`company_name`, `org_description`, etc.) to configure common settings. 3. **`provider "github" { ... }`**: Configures the GitHub provider. * `token`: Uses the `github_token` variable. * `owner`: **Crucially**, this tells the provider *which* organization context these resources operate within. It must match the organization you created. * `base_url`: Use this only if you are targeting a GitHub Enterprise Server instance. 4. **`resource "github_organization_settings" "org_settings" { ... }`**: This is the core resource. It *does not create* the org but *configures* the existing one identified by the provider's `owner` setting. It sets various properties based on the input variables. Explore the [Terraform GitHub Provider documentation](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/organization_settings) for all available settings. 5. **`output "..." { ... }`**: Optionally outputs information about the managed organization. **How to Use:** 1. Save the Terraform code as `main.tf`. 2. Create a file named `terraform.tfvars` (or set environment variables as shown in the example) with your specific values (organization name, billing email, etc.). **Do not commit `terraform.tfvars` if it contains your token!** Use environment variables for the token. 3. Set the `GITHUB_TOKEN` or `TF_VAR_github_token` environment variable: ```bash export TF_VAR_github_token="ghp_YOUR_PAT_HERE" # Or if using GITHUB_TOKEN (provider default) # export GITHUB_TOKEN="ghp_YOUR_PAT_HERE" ``` 4. Initialize Terraform: ```bash terraform init ``` 5. Review the planned changes: ```bash terraform plan -var-file=terraform.tfvars # Omit -var-file if using only ENV VARS ``` 6. Apply the changes to configure your organization's settings: ```bash terraform apply -var-file=terraform.tfvars # Omit -var-file if using only ENV VARS ``` Now, Terraform will manage the specified settings for your *existing* GitHub organization.