Search code examples
terraformterraform-modulesterraform-provider-github

How to change the value of a nested Terraform argument?


I have a Terraform / Github provider question. I'm trying to change a value for an argument that belongs in a nested block, whilst calling the custom module that the block belongs to. Can't figure out for the life of me how to do it!

Here's how the module looks (with some unnecessary code removed and tweaks for org purposes):

resource "github_repository" "repo" {
  name                    = var.name
  description             = var.description
  visibility              = "private"
  has_issues              = true
  has_projects            = false
  has_wiki                = false
  auto_init               = var.auto_init
  archived                = var.archived
  topics                  = var.topics
  delete_branch_on_merge  = var.delete_branch_on_merge
}

resource "github_branch_default" "default"{
  repository = github_repository.repo.name
  branch     = var.default_branch_name
}

resource "github_branch_protection_v3" "repo_branch_protection" {
  count = var.enable_branch_protection ? 1 : 0
  repository     = github_repository.repo.name
  branch         = github_branch_default.default.branch
  enforce_admins = true

  required_pull_request_reviews {
    dismiss_stale_reviews = true
    dismissal_teams       = []
    dismissal_users       = []
    required_approving_review_count = var.approval_count
  }

  required_status_checks {
    strict   = true
    contexts = var.required_status_checks
  }

  restrictions {
    teams = []
    users = []
  }
}

And here's how I call it from main.tf without trying to alter what I want, which runs successfully:

module "new-repo" {
  source = "./modules/create-repo"
  name            = "new-repo"
  description     = "oh look a new repo"
  owner_team_name = "my-team"
  auto_init       = true
}

The value I'm wanting to change here is for the required_approving_review_count argument, and I've tried a couple different methods. Like this:

module "new-repo" {
    source = "./modules/create-repo"
    name            = "new-repo"
    description     = "oh look a new repo"
    owner_team_name = "my-team"
    auto_init       = true
    required_approving_review_count = 2
}

and like this:

module "new-repo" {
    source = "./modules/create-repo"
    name            = "new-repo"
    description     = "oh look a new repo"
    owner_team_name = "my-team"
    auto_init       = true
    required_pull_request_reviews {
    required_approving_review_count = 2
    }
}

Both resulting in the error

"an argument named required_approving_review_count is not expected here" 

and

"an argument named required_pull_request_reviews is not expected here"

The variable in question is set up and looks like this:

variable "approval_count" {
    type = number
    default = 1
    description = "number of approvals required to merge a PR"
}

The module runs just fine without trying to change that value. How do I change that value when I call upon the module?!


Solution

  • You need to assign a value to the variable you have defined on the module level, i.e., var.approval_count. In your module, for the required_pull_request_reviews block, you have this defined:

    required_approving_review_count = var.approval_count
    

    That means, in order to add a value to the required_approving_review_count argument, when calling the module, you need to provide a value for the approval_count variable.

    For this to work you need to change your code to:

    module "new-repo" {
        source = "./modules/create-repo"
        name            = "new-repo"
        description     = "oh look a new repo"
        owner_team_name = "my-team"
        auto_init       = true
        approval_count  = 2
    }
    

    Please make sure you understand how input values are passed to the child module [1].


    [1] https://developer.hashicorp.com/terraform/language/modules/syntax#calling-a-child-module