forked from HariSekhon/DevOps-Bash-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.envrc-terraform
56 lines (46 loc) · 2.42 KB
/
.envrc-terraform
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: Mon Feb 22 17:42:01 2021 +0000
#
# https://github.com/HariSekhon/DevOps-Bash-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
# ============================================================================ #
# T e r r a f o r m D i r E n v
# ============================================================================ #
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
#srcdir="$(dirname "${BASH_SOURCE[0]}")"
# XXX: beware that tfenv and tgswitch replace the terraform / terragrunt in the path and this is a race condition between different shells
# it is not as good as the KUBECONFIG trick done in the .envrc-kubernetes
# would auto-determine the version from the state file, but this commands seems to always return the version of your local binary pulling the state file, not the version from the actual terraform_version field in state file if you see when opening it up in the cloud bucket
# terraform state pull | jq -r .terraform_version
export TERRAFORM_VERSION=1.1.9
# overrides .terraform-version file to make this single source of truth
export TFENV_TERRAFORM_VERSION="$TERRAFORM_VERSION"
export TFENV_AUTO_INSTALL=true
# Terragrunt
export TG_VERSION=0.39.2
# if tgswitch is installed, trigger it to use the above TF_VERSION environment variable and switch to the correct version of Terragrunt
# better than adding the ugly shell hook from the docs - https://github.com/warrensbox/tgswitch?tab=readme-ov-file#get-the-version-from-a-subdirectory
if type -P tgswitch &>/dev/null; then
tgswitch
fi
# XXX: set these or other variables for Terraform code to find
export CLOUDFLARE_EMAIL=hari@...
export CLOUDFLARE_API_KEY=... # generate here: https://dash.cloudflare.com/profile/api-tokens
#export CLOUDFLARE_TOKEN=... # used by cloudflare_api.sh but not by terraform module
# export the variables for terraform
export TF_VAR_cloudflare_email="$CLOUDFLARE_EMAIL"
export TF_VAR_cloudflare_api_key="$CLOUDFLARE_API_KEY" # must be a key, not a token using the link above
# GITHUB_* environment variables may interfere with GitHub provider, so unset them
for env_var in $(env | awk -F= '$1 ~ /GITHUB/ {print $1}'); do
unset "$env_var"
done