This repository was archived by the owner on Mar 30, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_terraform.sh
More file actions
executable file
·72 lines (68 loc) · 2.28 KB
/
run_terraform.sh
File metadata and controls
executable file
·72 lines (68 loc) · 2.28 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
terraform_function () {
path=$1
mode=$2
varfile=$3
cd $path || exit
if [[ -d $path/.terraform ]]
then
terraform init -reconfigure
else
terraform init
fi
if [ $mode == "create" ] && [ -n "$varfile" ]
then
terraform plan
terraform apply -var-file=$varfile -auto-approve
elif [ $mode == "create" ] && [ -z "$varfile" ]
then
terraform plan
terraform apply -auto-approve
else
terraform destroy -auto-approve
fi
}
# Get the absolution of path
abs_path_file_execute=$(realpath "$0")
abs_path_folder_root=$(dirname "$(dirname "$abs_path_file_execute")")
abs_path_folder_terraform_vm="$abs_path_folder_root/terraform/modules/vm/"
abs_path_folder_terraform_iam="$abs_path_folder_root/terraform/modules/iam/"
abs_path_folder_terraform_network="$abs_path_folder_root/terraform/modules/network/"
abs_path_folder_terraform_bucket="$abs_path_folder_root/terraform/modules/bucket/"
# Bash Menu Script Example
echo "---------MENU------------"
PS3='Please enter your choice: '
options=("Create IAM" "Create virtual machine" "Create bucket" "Create network" "Destroy IAM" "Destroy virtual machine" "Destroy bucket" "Destroy network" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Create IAM")
terraform_function $abs_path_folder_terraform_iam "create" ""
;;
"Create virtual machine")
terraform_function $abs_path_folder_terraform_vm "create" ""
;;
"Create bucket")
terraform_function $abs_path_folder_terraform_bucket "create" "terraform.tfvars"
;;
"Create network")
terraform_function $abs_path_folder_terraform_network "create" ""
;;
"Destroy IAM")
terraform_function $abs_path_folder_terraform_iam "destroy" ""
;;
"Destroy virtual machine")
terraform_function $abs_path_folder_terraform_vm "destroy" ""
;;
"Destroy bucket")
terraform_function $abs_path_folder_terraform_bucket "destroy" ""
;;
"Destroy network")
terraform_function $abs_path_folder_terraform_network "destroy" ""
;;
"Quit")
break
;;
*) echo "invalid option $REPLY";;
esac
done