Skip to content

Commit ef11c97

Browse files
committed
feat: add TERRAFORM_BIN variable for OpenTofu compatibility
All terraform binary invocations now use ${terraform_bin} which defaults to "terraform" but can be overridden via the TERRAFORM_BIN environment variable, e.g. TERRAFORM_BIN=tofu.
1 parent 2c9b7e0 commit ef11c97

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

bin/terraform.sh

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
set -uo pipefail;
99

10+
##
11+
# Terraform Binary
12+
##
13+
readonly terraform_bin="${TERRAFORM_BIN:-terraform}";
14+
1015
##
1116
# Set Script Version
1217
##
@@ -748,16 +753,16 @@ if [ "${bootstrapped}" == 'true' ]; then
748753
# Configure remote state storage
749754
echo "Setting up S3 remote state from s3://${bucket}/${backend_key}";
750755
[ "${lock_table}" == 'true' ] && echo "Using DynamoDB Table for state locking: ${bucket}";
751-
terraform init ${no_color} ${lockfile_or_upgrade} \
756+
${terraform_bin} init ${no_color} ${lockfile_or_upgrade} \
752757
|| error_and_die 'Terraform init failed';
753758

754759
if [ "${action}" == 'destroy' ] && [ "${destroy_response}" == 'I am not an idiot, I know what I am doing!' ]; then
755760
echo -e "terraform {\n backend \"local\" {}\n}" > backend_tfscaffold.tf;
756-
terraform init -migrate-state -force-copy;
761+
${terraform_bin} init -migrate-state -force-copy;
757762
fi;
758763
else
759764
# We are bootstrapping. Download the providers, skip the backend config.
760-
terraform init \
765+
${terraform_bin} init \
761766
-backend=false \
762767
${no_color} \
763768
${lockfile} \
@@ -779,7 +784,7 @@ case "${action}" in
779784
detailed='-detailed-exitcode';
780785
fi;
781786

782-
terraform "${action}" \
787+
${terraform_bin} "${action}" \
783788
-input=false \
784789
${refresh} \
785790
${tf_var_params} \
@@ -807,9 +812,9 @@ case "${action}" in
807812
;;
808813
'graph')
809814
mkdir -p build || error_and_die "Failed to create output directory '$(pwd)/build'";
810-
terraform graph ${extra_args} -draw-cycles | dot -Tpng > "build/${project}-${aws_account_id}-${region}-${environment}.png" \
815+
${terraform_bin} graph ${extra_args} -draw-cycles | dot -Tpng > "build/${project}-${aws_account_id}-${region}-${environment}.png" \
811816
|| error_and_die 'Terraform simple graph generation failed';
812-
terraform graph ${extra_args} -draw-cycles -verbose | dot -Tpng > "build/${project}-${aws_account_id}-${region}-${environment}-verbose.png" \
817+
${terraform_bin} graph ${extra_args} -draw-cycles -verbose | dot -Tpng > "build/${project}-${aws_account_id}-${region}-${environment}-verbose.png" \
813818
|| error_and_die 'Terraform verbose graph generation failed';
814819
exit 0;
815820
;;
@@ -827,7 +832,7 @@ case "${action}" in
827832
extra_args+=' -auto-approve=true';
828833
else # action is `destroy`
829834
# Check terraform version - if pre-0.15, need to add `-force`; 0.15 and above instead use `-auto-approve`
830-
if [ "$(terraform version | head -n1 | cut -d' ' -f2 | cut -d'.' -f1)" == 'v0' ] && [ "$(terraform version | head -n1 | cut -d' ' -f2 | cut -d'.' -f2)" -lt 15 ]; then
835+
if [ "$(${terraform_bin} version | head -n1 | cut -d' ' -f2 | cut -d'.' -f1)" == 'v0' ] && [ "$(${terraform_bin} version | head -n1 | cut -d' ' -f2 | cut -d'.' -f2)" -lt 15 ]; then
831836
echo 'Compatibility: Adding to terraform arguments: -force';
832837
force='-force';
833838
else
@@ -845,7 +850,7 @@ case "${action}" in
845850

846851
apply_plan="build/${plan_file_name}";
847852

848-
terraform "${action}" \
853+
${terraform_bin} "${action}" \
849854
-input=false \
850855
${refresh} \
851856
-parallelism=300 \
@@ -854,7 +859,7 @@ case "${action}" in
854859
${apply_plan};
855860
exit_code=$?;
856861
else
857-
terraform "${action}" \
862+
${terraform_bin} "${action}" \
858863
-input=false \
859864
${refresh} \
860865
${tf_var_params} \
@@ -875,7 +880,7 @@ case "${action}" in
875880

876881
# Push Terraform Remote State to S3
877882
# TODO: Add -upgrade to init when we drop support for <0.10
878-
echo 'yes' | terraform init ${lockfile} || error_and_die 'Terraform init failed';
883+
echo 'yes' | ${terraform_bin} init ${lockfile} || error_and_die 'Terraform init failed';
879884

880885
# Hard cleanup
881886
rm -f terraform.tfstate; # Prime not the backup
@@ -892,7 +897,7 @@ case "${action}" in
892897

893898
if [ "${output_json}" == 'true' ] && [ "${action}" != 'destroy' ]; then
894899
echo "Writing terraform output to $(pwd)/.terraform.output.json";
895-
terraform output -json -no-color > .terraform.output.json;
900+
${terraform_bin} output -json -no-color > .terraform.output.json;
896901
fi;
897902

898903
if [ -f 'post.sh' ]; then
@@ -908,7 +913,7 @@ case "${action}" in
908913
rm -f .terraform.output.json;
909914
fi;
910915

911-
terraform "${action}" ${extra_args};
916+
${terraform_bin} "${action}" ${extra_args};
912917
status="${?}";
913918

914919
if [ "${status}" -ne 0 ]; then
@@ -917,24 +922,24 @@ case "${action}" in
917922

918923
if [ "${output_json}" == 'true' ]; then
919924
echo "Writing terraform output to $(pwd)/.terraform.output.json";
920-
terraform output -json -no-color > .terraform.output.json \
925+
${terraform_bin} output -json -no-color > .terraform.output.json \
921926
|| error_and_die 'Terraform output -json failed.';
922927
fi;
923928
;;
924929
'*taint')
925-
terraform "${action}" ${extra_args} || error_and_die "Terraform ${action} failed.";
930+
${terraform_bin} "${action}" ${extra_args} || error_and_die "Terraform ${action} failed.";
926931
;;
927932
'import')
928-
terraform "${action}" ${tf_var_params} ${extra_args} || error_and_die "Terraform ${action} failed.";
933+
${terraform_bin} "${action}" ${tf_var_params} ${extra_args} || error_and_die "Terraform ${action} failed.";
929934
;;
930935
'shell')
931936
echo -e "Here's a shell for the ${component} component.\nIf you want to run terraform actions specific to the ${environment} environment, pass the following options to your terraform commands:\n\n${tf_var_params} ${extra_args}\n\n'exit 0' / 'Ctrl-D' to continue, other exit codes will abort tfscaffold with the same code.";
932937
bash -l || exit "${?}";
933938
;;
934939
*)
935940
echo -e 'Generic action case invoked. Only the additional arguments will be passed to terraform, you break it you fix it:';
936-
echo -e "\tterraform ${action} ${extra_args}";
937-
terraform "${action}" ${extra_args} \
941+
echo -e "\t${terraform_bin} ${action} ${extra_args}";
942+
${terraform_bin} "${action}" ${extra_args} \
938943
|| error_and_die "Terraform ${action} failed.";
939944
;;
940945
esac;

0 commit comments

Comments
 (0)