4

I am trying to consume a Terraform module that I've got in a dedicated private modules repository in BitBucket. I have an app repository in BitBucket that has Terraform templates in there that consumes a module in the modules repository. When I check in changes to the app repository it fires off BitBucket Pipelines.

When BitBucket Pipelines run on my app repository, the Terraform template in there has my Modules repository as the source for a module the template uses. The Pipeline runs terraform init which fails because the Pipeline needs to enter the password for the modules source repository. I've seen people use git credential helper for storing credentials, but that seems to be a solution for caching previously entered credentials. That wouldn't be the case in this solution as this is a Docker image that wouldn't have previously had my git credentials cached on it.

I've also spent a lot of time trying to get ssh working with it. I've generated public/private ssh keys on my app repository, given the public key to my modules repository and then set the module source to be my ssh git path. Terraform can't figure out the repository path though, telling me it's not a valid repository.

Error downloading modules: Error loading modules: error downloading 'ssh://git@bitbucket.org:foo/bar.git': C:\Program Files\Git\cmd\git.exe exited with 128: Cloning into '.terraform\modules\f169a1ea8d4eb404'... ssh: Could not resolve hostname bitbucket.org:foo: Name or service not known fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

When using ssh I've tried setting the source as both git::ssh://git@bitbucket.org:foo/bar.git and git::git@bitbucket.org:foo/bar.git. Each time I'm told that's not a valid repository, the host name is unknown and that it can't be authorized. I've also added bitbucket.org to my known_hosts file on Windows under the $HOME/.ssh path. I'm not sure if the SSH keys are being used correctly or if Terraform can't figure out the BitBucket repository path.

I'd prefer to go the SSH route over HTTPS/cached credentials but I can't get it working, even if I run 'terraform init' locally on my Windows machine. So I'd like to start there, what do I have to do on Windows to get Terraform to consume a BitBucket repository with SSH as a Modules source?

Johnathon Sullinger
  • 7,097
  • 5
  • 37
  • 102
  • Actually, the second one, git::git@bitbucket.org:foo/bar.git, is correct. ssh:// would require a /. – VonC Sep 17 '18 at 05:04

1 Answers1

0

I'm not sure if the SSH keys are being used correctly or if Terraform can't figure out the BitBucket repository path

Try and launch the all process again after setting the environment variable GIT_SSH_COMMAND to ssh -v: that will show you the key used for the ssh session.
Note: ssh://git@bitbucket.org:foo/bar.git is wrong: it should be ssh://git@bitbucket.org/foo/bar.git.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `ssh -v` doesn’t output anything, the command shows that `-v` isn’t an option in the usage help. Does that apply to Windows or only Linux? Also good pointing out my invalid URL. My example ssh path was wrong; it’s using the correct format in my code. I’ll update my OP to reflect it – Johnathon Sullinger Sep 17 '18 at 01:10
  • @JohnathonSullinger Yes, it works in Windows too, provided that `` is in your `%PATH%`. – VonC Sep 17 '18 at 10:59