Last active
January 17, 2025 19:21
-
-
Save VincentSchmid/dcb41e955f375808da3d94d746eca8d0 to your computer and use it in GitHub Desktop.
justfile for terraform configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
terraform_version := "1.6.6" | |
current_env := "dev" | |
var_file_name := "./configuration." + current_env + ".tfvars" | |
backend_file_name := "backend." + current_env + ".tfvars" | |
terraform_loglevel := "ERROR" | |
terraform_code_folder := "terraform" | |
# runs terraform init & fmt & validate & tflint (add -reconfigure or -upgrade as params) | |
tf-check *params: | |
cd {{terraform_code_folder}} \ | |
&& tfenv use {{terraform_version}} \ | |
&& export TF_LOG={{terraform_loglevel}} \ | |
&& terraform init -var-file={{var_file_name}} -backend-config={{backend_file_name}} {{params}} \ | |
&& terraform fmt && terraform validate && tflint | |
# Will run terraform plan and store a plan file as myplan.tfplan | |
tf-plan *params: | |
cd {{terraform_code_folder}} \ | |
&& export TF_LOG={{terraform_loglevel}} \ | |
&& terraform plan -var-file={{var_file_name}} -out=myplan.tfplan {{params}} | |
# Will run terraform apply on the planfile myplan.tfplan and then delete it | |
tf-apply: | |
cd {{terraform_code_folder}} \ | |
&& export TF_LOG={{terraform_loglevel}} \ | |
&& terraform apply myplan.tfplan \ | |
&& rm myplan.tfplan | |
tf-run *params: | |
cd {{terraform_code_folder}} \ | |
&& export TF_LOG={{terraform_loglevel}} \ | |
&& terraform apply -var-file={{var_file_name}} {{params}} | |
tf-refresh: | |
cd {{terraform_code_folder}} \ | |
&& export TF_LOG={{terraform_loglevel}} \ | |
&& terraform refresh -var-file={{var_file_name}} | |
tf-destroy *params: | |
cd {{terraform_code_folder}} \ | |
&& export TF_LOG={{terraform_loglevel}} \ | |
&& terraform destroy -var-file={{var_file_name}} {{params}} | |
tf-import resource instance: | |
cd {{terraform_code_folder}} \ | |
&& export TF_LOG={{terraform_loglevel}} \ | |
&& terraform import -var-file={{var_file_name}} {{resource}} {{instance}} | |
tf-state *params: | |
cd {{terraform_code_folder}} \ | |
&& export TF_LOG={{terraform_loglevel}} \ | |
&& terraform state {{params}} | |
tf *commands: | |
cd {{terraform_code_folder}} \ | |
&& export TF_LOG={{terraform_loglevel}} \ | |
&& terraform {{commands}} | |
# General Terraform Helpers | |
# creates and initialises the folder and moudle within given folder | |
tf-create-module relative_module_folder_path: | |
mkdir -p {{relative_module_folder_path}} | |
touch {{relative_module_folder_path}}/main.tf | |
touch {{relative_module_folder_path}}/variables.tf | |
touch {{relative_module_folder_path}}/output.tf | |
@echo '\nmodule "{{relative_module_folder_path}}" {{ "{" }}\n\tsource = "./{{relative_module_folder_path}}"\n}' | |
# prints latest provider version for given provider | |
tf-latest-provider-version provider: | |
@version=$(curl -s https://registry.terraform.io/v1/providers/{{provider}}/versions | jq -r '.versions[] | select(.version | test("^[0-9]+")) | .version' | sort -V | tail -n1) && \ | |
echo {{provider}} latest version $version | |
# prints the latest versions for common providers | |
tf-latest-common-providers: | |
@just tf-latest-provider-version hashicorp/azurerm | |
@just tf-latest-provider-version hashicorp/kubernetes | |
@just tf-latest-provider-version hashicorp/helm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment