404 When Creating github_repository_webhook with Terraform

Viewed 831

We upgraded to using the integrations/github provider source and ever since we have started getting a 404 when attempting to create a github_repository_webhook with Terraform. I believe we have all of the necessary pieces required based on the docs, but the API uri in the logs is missing the org. NOTE: Real org and repo names have been redacted.

main.tf

resource "aws_codepipeline_webhook" "codepipeline_webhook" {
  name            = "test-github-webhook"
  authentication  = "GITHUB_HMAC"
  target_action   = "CC"
  target_pipeline = aws_codepipeline.pipeline.name

  authentication_configuration {
    secret_token = data.aws_secretsmanager_secret_version.github_token.secret_string
  }

  filter {
    json_path    = "$.ref"
    match_equals = "refs/heads/{Branch}"
  }

  tags = merge(var.tags, {
    Name = "test-github-webhook"
  })
}

# Wire the CodePipeline webhook into a GitHub repository.
resource "github_repository_webhook" "github_webhook" {
  repository = "my_repo"

  configuration {
    url          = aws_codepipeline_webhook.codepipeline_webhook.url
    content_type = "json"
    insecure_ssl = true
    secret       = data.aws_secretsmanager_secret_version.github_token.secret_string
  }

  events = ["push"]
}

backend.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "3.65.0"
    }
    github = {
      source  = "integrations/github"
      version = "~> 4.0"
    }
  }
}

provider "github" {
  token    = data.aws_secretsmanager_secret_version.github_token.secret_string
  owner    = "my_org"
  base_url = "https://github.com/my_org/" # we have github enterprise
}

Error on create:

Error: POST https://api.github.com/repos//my_repo/hooks: 404 Not Found []

Note that the org is missing completely from the URL. I've also tried including the org name in the github_repository_webhook resource, but the url still comes out with a double slash and a 404:

Error: POST https://api.github.com/repos//my_org/my_repo/hooks: 404 Not Found []

When I remove the provider source and version completely, terraform falls back to the hashicorp/terraform source and the webhook creates without any issues. Has anyone else run into this problem?

3 Answers

You've probably solved this by now, but just in case there are any others who run into this problem.

This solution assumes you're using Terraform version >=0.13 and the new Terraform GitHub Integration.

Check to make sure that all modules that use the github provider have defined a github required_providers block. If this block is not defined within the module, then it seems that the (deprecated) "hashicorp/github" provider is used even if you've configured this block on the root module where you've defined the github provider. (This is what was causing the error for me.) That is, make sure you've defined the following in each of your modules:

terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 4.0"
    }
  }
}

Assuming you've done the above, then try double checking that you've set up the GitHub integration properly. Some notable gotchas are:

  • When upgrading from hashicorp/github to integrations/github, use terraform state replace-provider. Otherwise, Terraform will still require the old provider to interact with the state file.
  • The organization argument has been deprecated in favor of the owner argument (should behave the same in the mean time, but just in case).

What is the output of terraform providers? I suspect you have both versions of the github provider in your state. If you do,

terraform state replace-provider hashicorp/github integrations/github Should fix you up.

I ran into this issue a few months ago. Your url is missing the organization. It should be of the format

https://api.github.com/repos/my_organization/my_repo/hooks

Related