Using Private Repo (SSH) Keys and (HTTPS) Tokens

Often Terraform modules are not stored in public git repos for any number of reasons. Fortunately, with the right keys or tokens, it’s not that hard to access them. In this article we’ll go over a few common examples of how to access terraform modules in private repos with Terraform Operator. In general, Terraform can accept either ssh keys and/or tokens to access modules in git repos.

Example 1: Private Repos over HTTPS on Github

To start, you’ll need an basic understanding of Git, and GitHub. Let’s pretend our private git repo is hosted on GitHub and we want to use HTTPS to download our module. First, get an “Access Token” from GitHub. Check the “repo” access checkbox. (Need help? Check out this article to help you Generate Access Tokens from Github Account.)

When you create the token, the token will only show up once. Copy it. We’ll save this to the cluster as a Secret.

Create a Secret in your cluster with the token.

GHTOKEN=$(printf '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' | base64)

cat << EOF | kubectl apply -f -
apiVersion: v1
  mytoken: $GHTOKEN
kind: Secret
  name: gh-access-token
  namespace: default
type: Opaque

In the above code, we saved our Github token to the cluster as a Secret called gh-access-token with data key mytoken in the default namespace. Next we’ll add these item to our terraform k8s-resource manifest.

Create the terraform k8s-resource manifest, let’s call it terraform.yaml. The following is an example of my manifest. Notice the scmAuthMethods. The scmAuthMethods is an array of objects.

  1. In example below, we’ll fill in host: with That is the host our token is valid for.
  2. Next, we’re using git: as the SCM (Source Control Management).
  3. Under git:, we’re going with the https: protocol. The other protocol for git is ssh: which will be covered later.
  4. And finally under https:, the secret that was created earlier is defined in tokenSecretRef:.

Take a look for the final manifest:

# terraform.yaml

kind: Terraform
  name: my-module
  terraformVersion: 1.1.9
  customBackend: |-
    terraform {
      backend "kubernetes" {
        secret_suffix     = "my-module"
        namespace         = "default"
        in_cluster_config = true

  # *-------------------------*
  - host:
          name: gh-access-token
          key: mytoken
  # *-------------------------*

  keepLatestPodsOnly: true
  ignoreDelete: false
  writeOutputsToStatus: true

Apply the manifest using kubectl and we’re done and terraform-operator will handle the rest.

kubectl apply -f terraform.yaml

Watch this short terminal capture which follows the steps above of using a Github Token with Terraform Operator.

A few notes:
  1. When an HTTPS token is defined for a host, all git pulls, whether it be downloading the initial module or any module pulled within the a module will use the defined token.
  2. Using an HTTPS token to pull a git module that pulls over SSH will not work. Use scmAuthMethods[].git.ssh.
  3. Only a single token can be used to pull git resources over HTTPS. Use SSH if there are multiple git hosts for your Terraform modules.