1

I have a github provider declared with a personal access token. Trying to create a repository like the following works:

resource "github_repository" "test" {
  name = "test"
  visibility = "private"
}

However, I want to create something reusable. Whenever I try to create that exact same github_repository from a submodule, I get the following error:

╷
│ Error: POST https://api.github.com/user/repos: 401 Requires authentication []
│
│   with module.test.module.repository.github_repository.test,
│   on modules\serverless_api_repository\main.tf line 1, in resource "github_repository" "source_code":
│    1: resource "github_repository" "test" {
│
╵

Why can't I create a github_repository from a submodule, when it works in the main scope?

JSON Brody
  • 736
  • 1
  • 4
  • 23
  • Is the provider configured the same for both modules? – Matthew Schuchard Jan 09 '22 at 12:07
  • There is no provider explicitly configured for the submodule. I understand that the submodule will use the same provider configurations defined in the main scope. – JSON Brody Jan 09 '22 at 15:53
  • Because the provider isn't specified in the submodule I don't think it will have access to the environment variables as it would in the root module. I would specify the required provider in the module but never set a max version (min version only if required). – Jake Nelson Jan 10 '22 at 03:58
  • 2
    @JakeNelson According to [the docs](https://www.terraform.io/language/meta-arguments/module-providers#default-behavior-inherit-default-providers) it seems like I shouldn't need to specify a provider in the submodule. The submodule should just inherit the github provider from the parent scope because there is no `alias` and I don't specify any `providers` argument in the `module` block. – JSON Brody Jan 10 '22 at 05:05
  • @JSONBrody, can you please show us how you are configuring your providers and credentials being used by this provider? – Jake Nelson Jan 10 '22 at 06:00
  • @JakeNelson it's just a `github` provider with only a PAT for `token`, no other args. Version is `~> 4.18.0`. – JSON Brody Jan 10 '22 at 06:06
  • The token is is defined in the provider config static for testing, passed in as a Terraform var, or specified as an OS environment variable? – Jake Nelson Jan 10 '22 at 06:09
  • @JakeNelson hardcoded into the provider declaration – JSON Brody Jan 10 '22 at 06:16
  • Please consider accepting an answer if one has been helpful. See [how does accepting an answer work](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) for info. Or if you found another solution, please add it here, I'd love to learn how you solved it! – Jake Nelson Jan 13 '22 at 02:27
  • @JakeNelson I did not solve it. I abandoned the approach. – JSON Brody Nov 05 '22 at 20:29

2 Answers2

0

Did a quick check to see if anyone else is facing this issue and found this. The resources are different to yours I think but it may be the same issue regardless.

You could try passing the provider down to the module call.

e.g.

provider "github" {
  ...
}

module "my-repo" {
  source    = "./my-repo"
  providers = {
    github  = "github"
  }
}

...
Jake Nelson
  • 1,748
  • 13
  • 22
0

In my case I needed to add the Organization parameter at the Github Provider level:

provider "github" {
  organization = "myorgname"
  token        = var.token
}

That did the work.