-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathterraform-apply
More file actions
executable file
·49 lines (41 loc) · 2.13 KB
/
terraform-apply
File metadata and controls
executable file
·49 lines (41 loc) · 2.13 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
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# Convenience script for running terraform apply for the specified module and
# configuration name. The configuration name is used to determine which .tfvars
# file to use for the -var-file option of terraform apply.
#
# Additional arguments to terraform apply can also be passed in using
# terraform's built-in environment variables TF_CLI_ARGS and TF_CLI_ARGS_name.
# For example, in CI/CD pipelines, you may want to set TF_CLI_ARGS="-input=false
# -auto-approve" to skip the confirmation prompt.
#
# See https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_cli_args-and-tf_cli_args_name
#
# Positional parameters:
# module_dir (required) - The location of the root module to initialize and apply
# config_name (required) - The name of the tfvars config.
# For accounts, the config name is the account name.
# For application modules the config name is the name of the environment (e.g. "dev", "staging", "prod").
# For application modules that are shared across environments, the config name is "shared".
# For example if a backend config file is named "myaccount.s3.tfbackend", then the config_name would be "myaccount"
# -----------------------------------------------------------------------------
set -euo pipefail
module_dir="$1"
config_name="$2"
tf_apply_args=("${@:3}")
# Convenience script for running terraform apply
# config_name – the name of the backend config.
# For example, if a backend config file is named "myaccount.s3.tfbackend",
# then the config_name would be "myaccount"
# module_dir – the location of the root module to initialize and apply
# 1. Set working directory to the terraform root module directory
cd "${module_dir}"
# 2. Run terraform apply with the tfvars file (if it exists) that has the same
# name as the backend config file
tf_vars_file="${config_name}.tfvars"
if [ -f "${tf_vars_file}" ]; then
tf_apply_args+=("-var-file=${tf_vars_file}")
fi
# tf_apply_args oddness to support Bash < v4.4
# https://stackoverflow.com/a/7577209
terraform apply ${tf_apply_args[@]+"${tf_apply_args[@]}"}