-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform.sh
executable file
·58 lines (47 loc) · 1.36 KB
/
terraform.sh
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
#!/bin/bash
set -exo pipefail
function tf_exec() {
env_file=$(grep -v '#' ${BASE_DIR}/.env.${ENV})
# shellcheck disable=SC2086
export ${env_file?}
cp -fr "${BASE_DIR}"/_templates/_*.tf "${BASE_DIR}"/"${TARGET_DIR}"/
cd "${BASE_DIR}"/"${TARGET_DIR}"
terraform init \
-backend-config "bucket=${BACKEND_BUCKET}" \
-backend-config "key=${TARGET_DIR}".tfstate \
-backend-config "region=${BACKEND_REGION}" \
-backend-config "profile=${BACKEND_PROFILE}" \
-reconfigure
if [ -e "${ENV}.tfvars" ]; then
TF_VAR_FILE="-var-file ${ENV}.tfvars"
else
TF_VAR_FILE=""
fi
if [ "${CODEBUILD_BUILD_ID}" ]; then
terraform "${TF_CMD}" ${TF_VAR_FILE} ${TF_ARGS} | $(eval echo ${AFTER_TF})
else
terraform "${TF_CMD}" ${TF_VAR_FILE} ${TF_ARGS}
fi
}
# Main
BASE_DIR=$(cd "$(dirname "$0")";pwd)
ENV=$1
TARGET_DIRS=$2
TF_CMD=$3
# shellcheck disable=SC2124
TF_ARGS=${@:4}
# 引数が少なければエラーにする
# -hや--helpが指定されたらヘルプを表示する
if [ "${TARGET_DIRS}" == 'ALL' ]; then
TARGET_DIRS='.'
# else 複数ディレクトリ対応
fi
EXCLUDE_DIRS='_templates\|modules\|\.terraform'
TARGET_DIRS=$(
cd "${BASE_DIR}"
find ${TARGET_DIRS} -type f -name '*.tf' -exec dirname {} \; | grep -v ${EXCLUDE_DIRS} | uniq
)
for TARGET_DIR in $TARGET_DIRS; do
tf_exec
rm -f "${BASE_DIR}"/"${TARGET_DIR}"/_*.tf
done