Pull private module within Terraform docker container

Viewed 693

To run my Terraform I have a docker-compose setup that pulls the hashcorp/terraform:light image and then builds my infrastructure.

I recently introduced a private module which sits in my private Github repo. It works fine when I run terraform get on my machine but within the Docker compose set up I get the following error:

Could not download module "privateModule" (privateModule.tf:1) source code from
"git@github.com:sum/private.go.deploy.git": error downloading
'ssh://git@github.com/sum/private.go.deploy.git': /usr/bin/git exited with
128: Cloning into '.terraform/modules/privateModule'...
Warning: Permanently added 'github.com,140.82.121.3' (RSA) to the list of
known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

I am attempting to pass my SSH keys to the container so it can pull the private Github repo but it doesn't seem to do anything.

version: '3.4'

services:
  terraform:
    image: hashicorp/terraform:light
    volumes:
      - .:/terraform
      - ~/.ssh:/.ssh
    working_dir: /terraform
    environment:
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
      - AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN}

The command in my Makefile is:

tf-init:
    docker-compose run --rm terraform init

Is there a possible way to allow my container to pull the private dependency from Git for use with Terraform?

1 Answers

The ssh folder wasn't being mounted into the correct place.

By mounting it into root/.ssh it fixed my issue and I can successfully download private Terraform modules!

version: '3.4'

services:
  terraform:
    image: hashicorp/terraform:light
    volumes:
      - .:/terraform
      - ~/.ssh:/root/.ssh
    working_dir: /terraform
    environment:
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
      - AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN}
Related