I was receiving Error: POST https://api.github.com/user/repos: 401 Requires authentication [] while applying and deploying repository on github.com with Terraform. This error occurs because terraform is not able to authenticate to GitHub.com API and not able to perform the action.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
.\060-github_configuration> terraform apply --auto-approve Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # github_branch.main will be created + resource "github_branch" "main" { + branch = "main" + etag = (known after apply) + id = (known after apply) + ref = (known after apply) + repository = "vcloud-lab.com" + sha = (known after apply) + source_branch = "main" + source_sha = (known after apply) } # github_branch_default.default will be created + resource "github_branch_default" "default" { + branch = "main" + etag = (known after apply) + id = (known after apply) + rename = false + repository = "vcloud-lab.com" } # github_repository.main will be created + resource "github_repository" "main" { + allow_auto_merge = false + allow_merge_commit = true + allow_rebase_merge = true + allow_squash_merge = true + archived = false + auto_init = true + default_branch = (known after apply) + delete_branch_on_merge = false + description = "Terraform test repository for vcloud-lab.com" + etag = (known after apply) + full_name = (known after apply) + git_clone_url = (known after apply) + gitignore_template = "Terraform" + html_url = (known after apply) + http_clone_url = (known after apply) + id = (known after apply) + merge_commit_message = "PR_TITLE" + merge_commit_title = "MERGE_MESSAGE" + name = "vcloud-lab.com" + node_id = (known after apply) + primary_language = (known after apply) + private = (known after apply) + repo_id = (known after apply) + squash_merge_commit_message = "COMMIT_MESSAGES" + squash_merge_commit_title = "COMMIT_OR_PR_TITLE" + ssh_clone_url = (known after apply) + svn_url = (known after apply) + topics = (known after apply) + visibility = "public" + web_commit_signoff_required = false + security_and_analysis (known after apply) } Plan: 3 to add, 0 to change, 0 to destroy. Changes to Outputs: + remote_url = (known after apply) github_repository.main: Creating... ╷ │ Error: POST https://api.github.com/user/repos: 401 Requires authentication [] │ │ with github_repository.main, │ on Create GitHub repository and branches using Terraform.tf line 22, in resource "github_repository" "main": │ 22: resource "github_repository" "main" { │ ╵ |
To resolve this issue generate and create GitHub.com Personal Access Token (PAT) (Developers Settings > Personal Access Tokens > Generate Classic Token) with required access roles as shown in my earlier article Create GitHub repository and branches using Terraform. Once you generate a PAT copy it.
Next use the GitHub token to add it in environment variable as below.
$env:GITHUB_TOKEN = 'ghp_pqsVFd3gp2iJ'
Another way of adding and using token is providing token inside provider block.
provider "github" {
token = env.GITHUB_TOKEN
owner = env.GITHUB_OWNER
}
Once PAT authentication is successful, repository creation will complete on GitHub.com.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
.\060-github_configuration> terraform apply --auto-approve Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # github_branch.main will be created + resource "github_branch" "main" { + branch = "main" + etag = (known after apply) + id = (known after apply) + ref = (known after apply) + repository = "vcloud-lab.com" + sha = (known after apply) + source_branch = "main" + source_sha = (known after apply) } # github_branch_default.default will be created + resource "github_branch_default" "default" { + branch = "main" + etag = (known after apply) + id = (known after apply) + rename = false + repository = "vcloud-lab.com" } # github_repository.main will be created + resource "github_repository" "main" { + allow_auto_merge = false + allow_merge_commit = true + allow_rebase_merge = true + allow_squash_merge = true + archived = false + auto_init = true + default_branch = (known after apply) + delete_branch_on_merge = false + description = "Terraform test repository for vcloud-lab.com" + etag = (known after apply) + full_name = (known after apply) + git_clone_url = (known after apply) + gitignore_template = "Terraform" + html_url = (known after apply) + http_clone_url = (known after apply) + id = (known after apply) + merge_commit_message = "PR_TITLE" + merge_commit_title = "MERGE_MESSAGE" + name = "vcloud-lab.com" + node_id = (known after apply) + primary_language = (known after apply) + private = (known after apply) + repo_id = (known after apply) + squash_merge_commit_message = "COMMIT_MESSAGES" + squash_merge_commit_title = "COMMIT_OR_PR_TITLE" + ssh_clone_url = (known after apply) + svn_url = (known after apply) + topics = (known after apply) + visibility = "public" + web_commit_signoff_required = false + security_and_analysis (known after apply) } Plan: 3 to add, 0 to change, 0 to destroy. Changes to Outputs: + remote_url = (known after apply) github_repository.main: Creating... github_repository.main: Creation complete after 5s [id=vcloud-lab.com] github_branch.main: Creating... github_branch.main: Creation complete after 2s [id=vcloud-lab.com:main] github_branch_default.default: Creating... github_branch_default.default: Creation complete after 2s [id=vcloud-lab.com] Apply complete! Resources: 3 added, 0 changed, 0 destroyed. Outputs: remote_url = "https://github.com/janviudapi/vcloud-lab.com.git" .\060-github_configuration> |
Useful Articles
Terraform variable multiple validation advanced blocks example
Terraform variable type list with for_each for loop examples
Terraform convert single string to list or set
Terraform workspaces with example
Terraform map of object for loop with if condition example
Terraform for_each for loop list of object without count example
Terraform using for loop in attribute value without for_each
Part 0: HashiCorp HCP Terraform Cloud backend configuration
Part 1: Create GitHub repository and branches using Terraform
Part 2 Terraform modules using a github.com repository as a source
Part 3 Automating and Planning Azure Resources with Terraform and GitHub Actions
Part 4 GitHub Actions deploy azure resources with Terraform backend
Part 4.1 GitHub Actions deploy azure resources with PowerShell
Part 4.2 GitHub Actions manage Microsoft Azure Cloud with az CLI
Azure OIDC OpenID Connect password less with GitHub Actions
GitHub repository integration with Terraform Cloud to Deploy and Manage Azure
Azure DevOps Enable creation of classic build release pipelines grayed out
Adding parameters in Azure DevOps pipelines examples Part 1
Azure Web App Containers Cannot perform credential operations for providers Microsoft.ContainerRegistry ad admin user is disabled, enable it