I’m trying to create multiple Github repos and add files to that newly created repos using a JSON file.
# Terraform Locals
locals {
binding_pairs = toset([
for pair in setproduct(
keys(data.github_repository_file.main),
keys(github_repository.main),
) : {
files = pair[0]
repos = pair[1]
}
])
}
locals {
json_files = fileset("${path.module}/environments/", "*.json")
json_data = [for i in local.json_files : jsondecode(file("${path.module}/environments/${i}"))]
json_content = { for i in local.json_data : i.github_repo_name => i.github_repo_description }
terraform_data = [for i in local.json_files : file("${path.module}/environments/${i}")]
}
# Terraform Data Sources
data "github_repository_file" "main" {
for_each = var.github_repo_files
repository = var.source_repo_name
branch = "main"
file = each.key
}
# Terraform GitHub Repository
resource "github_repository" "main" {
for_each = local.json_content
name = each.key
description = each.value
auto_init = true
visibility = "public"
}
# Terraform GitHub Repository Files
resource "github_repository_file" "main" {
for_each = { for p in local.binding_pairs : "${p.files}:${p.repos}" => p }
repository = github_repository.main[each.value.repos].id
branch = "main"
file = data.github_repository_file.main[each.value.files].file
content = data.github_repository_file.main[each.value.files].content
overwrite_on_create = true
}
resource "github_repository_file" "tfvars" {
for_each = github_repository.main
repository = github_repository.main[each.value].id
branch = "main"
file = "terraform.tfvars"
content = local.terraform_data[each.key] **<< This part how to do??**
overwrite_on_create = true
}
I need to create a terraform.tfvars.json and the content should be from the JSON file. For example, there will be a folder called environments and JSON files will be created inside those folders.
Example: environments/dev-env.json
{
"github_repo_name": "Dev",
"github_repo_description": "GitHub Repo for Dev Environment",
"app_name": "devapp",
"short_app_name": "devapp",
"environment": "test",
"location": "west europe",
"vnet_address_space": "10.3.0.0/16",
"subnet_address_space": "10.3.1.0/24",
"app_subnet_address_space": "10.3.2.0/24"
}
So now I need to create the terraform.tfvars.json
files dynamically when someone creates new JSON files in environments folder.
How to create **github_repository_file.tfvars**
resource?