Terraform git clone using Username and Token rather than an SSH key
2024-10-08
version 0.1, 2024-10-08
Locally every developer has their SSH key setup to access our git repos. For CI, we use a GitHub TOKEN instead, at least for now.
Without setting an SSH key for Terraform, it was failing to clone:
$ terraform init
Initializing the backend...
Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.
Initializing modules...
Downloading git::ssh://git@github.com/my-org/terraform-aws-iam-role.git?ref=0.3.3 for secrets_role...
╷
│ Error: Failed to download module
│
│ on main.tf line 398:
│ 398: module "secrets_role" {
│
│ Could not download module "secrets_role" (main.tf:398) source
│ code from
│ "git::ssh://git@github.com/my-org/terraform-aws-iam-role.git?ref=0.3.3":
│ error downloading
│ 'ssh://git@github.com/my-org/terraform-aws-iam-role.git?ref=0.3.3':
│ /usr/bin/git exited with 128: Cloning into
│ '.terraform/modules/secrets_role'...
│ Host key verification failed.
│ fatal: Could not read from remote repository.
│
│ Please make sure you have the correct access rights
│ and the repository exists.
│
╵
Doing a replacement in git allowed us to use the Token instead of the SSH key without changing anything in the codebase:
echo "Setting up git credentials replacement. HTTPS with username and token instead of an SSH key."
set +x
git config --global url."https://$GITHUB_USERNAME:$GITHUB_TOKEN@github.com/my-org".insteadOf "ssh://git@github.com/my-org"
set -x
|
Important
|
For Terraform, the URL divider has to use / not :, ssh://git@github.com/my-org not ssh://git@github.com:my-org.
|
$ terraform init Initializing the backend... Successfully configured the backend "s3"! Terraform will automatically use this backend unless the backend configuration changes. Initializing modules... Downloading git::ssh://git@github.com/my-org/terraform-aws-iam-role.git?ref=0.3.3 for secrets_role... - secrets_role in .terraform/modules/secrets_role Initializing provider plugins...