[Infrastructure as Code (IaC)] Terraform GitHub Examples about using Terraform GitHub Provider to manage GitHub repositories and SSH keys

Terraform GitHub Examples

Examples about using Terraform GitHub Provider to manage repositories and SSH keys.

Github: https://github.com/CloudoLife/terraform-github-examples

Prerequsites

Your must have a GitHub account, api token and an organization, and Terraform CLI have been installed.

GitHub Account

SSH Key

Check out our guide to generating SSH keys or troubleshoot common SSH Problems.

Terrraform and Terrafrom GitHub Provider

Configuration

GitHub Provider Variables

First, Specify values for GitHub Provider variables within terraform.tfvars.

Remember to replace , .

1
2
github_token = "<Your GitHub Token>"
github_organization = "<Your Organiztion>"

SSH key

Config github_user_ssh_keys within ./ssh_keys

1
2
3
4
5
6
7
8
9
10
11
# GitHub: github_user_ssh_key - Terraform by HashiCorp
# https://www.terraform.io/docs/providers/github/r/user_ssh_key.html
resource "github_user_ssh_key" "id_rsa" {
title = "id_rsa"
key = file("~/.ssh/id_rsa.pub")
}

resource "github_user_ssh_key" "id_ed25519" {
title = "id_ed25519"
key = file("~/.ssh/id_ed25519.pub")
}

Repository

Config GitHub repositories within ./repositories

1
2
3
4
5
6
7
8
# GitHub: github_repository - Terraform by HashiCorp
# https://www.terraform.io/docs/providers/github/r/repository.html
resource "github_repository" "terraform-github-examples" {
name = "terraform-github-examples"
description = "Examples about Terraform GitHub Provider."

private = false
}

Run

Terraform Init

1
terraform init

It will download the Terraform GitHub Provider binary.

Terraform apply

1
terraform apply

It will dispaly the execute plan and wait for your make yes or no.
Then yes to create GitHub SSH keys and repositories.

Known issues

Currently only organizaiton repositories is supported.

Reference

[1] Provider: GitHub - Terraform by HashiCorp - https://www.terraform.io/docs/providers/github/index.html

[2] terraform-providers/terraform-provider-github: Terraform GitHub provider - https://github.com/terraform-providers/terraform-provider-github