Custom SSH key for Terraform

If you have a Terraform module in another private git repository, you need to tell Terraform which SSH key it should use. Here's how to do so -

To resolve that issue, you need to pass the SSH key as an environment variable - this environment variable can and should be marked as sensitive. In this case, we call it SSH_PRIVATE_KEY, with minor adjustments

Usually, SSH keys look like this -

————-
Somehexcode
Anotherhexhere
Yetanotherone
——-

When you paste it as an env0 variable, replace new lines with ;

———Somehexcode;Anotherhexhere;Yetanotherone——-

Then we can use Custom Flow to write it to a file

deploy:
  steps:
    terraformInit:
      before:
        - mkdir -p ~/.ssh
        - echo "$SSH_PRIVATE_KEY" | tr ';' '\n' | tr -d "\r" > ~/.ssh/private_tf_modules
        - chmod 400 ~/.ssh/private_tf_modules
        - echo -e "Host github.com\n User git\n Hostname github.com\n IdentityFile ~/.ssh/private_tf_modules\n StrictHostKeyChecking no" > ~/.ssh/config
        - echo "Created private key"

Then, we tell Linux when we want to SSH to github.com to use this SSH file.

We can use different Host configurations for different servers.