How can I reuse the terraform output values in another Github action using terraform

110 Views Asked by At

Currently we are mapping manually repos details as below, meaning that we are adding every repos details manually for attribute.repository.

workload_identity_pool_attribute_mapping = {
  "google.subject"             = "assertion.sub"
  "attribute.repository"       = <<-EOT
  {
    "example-github-organization/example-repo": "true",
    "example-github-organization/example-repo-1": "true"
  }[assertion.repository]
  EOT
}

Now we've requirement that want to reuse below output details into another repo to map the string for GCP workload_identity_pool_attribute_mapping.

as we've a Github repo where we're maintaining our github repos centrally and created Output to save those details into terraform output, as below.

{"outputs": {
    "git_repos": {
      "value": [
        "example-repo",
        "example-repo-1",
      ],
      "type": [
        "tuple",
        [
          "string",
          "string"
        ]
      ]
    }
  }
}

Want to reuse those output details as a input for another repos for mapping.

Tried multiple approach to fill the request but not able to not able fulfill the requirement.

data "google_client_config" "current" {
    }
    
data "terraform_remote_state" "repo" {
  backend = "gcs"
  config = {
    bucket       = var.tfstate_bucket
    prefix       = "example-project"
    access_token = data.google_client_config.current.access_token
  }
}

Expected result is that if any changes on repos list it has to add newly created repo into attribute.repository. Here example-github-organization is using to same for each repo and all repos name is going to start with example.

workload_identity_pool_attribute_mapping = {
  "google.subject"             = "assertion.sub"
  "attribute.repository"       = <<-EOT
  {
    "example-github-organization/example-repo": "true",
    "example-github-organization/example-repo-1": "true"
  }[assertion.repository]
  EOT
}
  

I'm new to terraform please forgive me if i didn't follow the right format

1

There are 1 best solutions below

0
Antham On

Here is the solution which will help you add a string into jsonencode

data "google_client_config" "current" {
      }
    
data "terraform_remote_state" "repo" {
  backend = "gcs"
  config = {
    bucket = var.tfstate_bucket
    prefix = "your-project"
    access_token = data.google_client_config.current.access_token
  }
}

locals {
  repos_mapping        = { for repo in data.terraform_remote_state.repo.outputs.git_repos : "your-github-organization/${repo}" => "true" }
  attribute_repository = <<-EOT
    ${jsonencode(local.repos_mapping)}[assertion.repository]
  EOT
}

# Assuming you are applying this mapping in a resource or data source that supports workload_identity_pool_attribute_mapping
resource "google_iam_workload_identity_pool" "example" {
  # Other necessary configuration

  workload_identity_pool_attribute_mapping = {
    "google.subject"             = "assertion.sub"
    "attribute.exam_repository" = local.attribute_repository
  }
}