Skip to content

Commit 90f2247

Browse files
committed
v1.9.0 - Lock files, lock tables, S3 upgrade, kms key alias
1 parent c8c0efe commit 90f2247

File tree

3 files changed

+75
-13
lines changed

3 files changed

+75
-13
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
## 1.9.0 (06/09/2024)
2+
3+
FEATURES:
4+
5+
* New options:
6+
* -l/--lockfile: Local lock file
7+
* -t/--lock-table: Lock with DynamoDB Table
8+
* Bootstrap:
9+
* New multi-resource approach to S3 Bucket
10+
* KMS Key Alias for S3 Bucket KMS Key, for easy data-sourcing elsewhere
11+
* New tfscaffold: prefix for tfscaffold tags
12+
* Updated the warning for duplicate input variables
13+
14+
BUG FIXES:
15+
16+
* Some alignment and ordering
17+
18+
## 1.8.0 (Unreleased)
19+
20+
* Merged into 1.9.0
21+
122
## 1.7.1 (14/07/2023)
223

324
* Explicitly declare and clear out, refresh and destroy in case someone exports them

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ bin/terraform.sh \
111111
-e/--environment `environment` \
112112
-g/--group `group` (optional) \
113113
-i/--build-id `build_id` (optional) \
114+
-l/--lockfile `mode` (optional) \
114115
-p/--project `project` \
115116
-r/--region `region` \
116117
-d/--detailed-exitcode (optional) \
118+
-t/--lock-table (optional) \
117119
-n/--no-color (optional) \
118120
-w/--compact-warnings (optional) \
119121
-- \
@@ -134,9 +136,11 @@ Where:
134136
* `component_name`: The name of the terraform component in the components directory to run the `action` against.
135137
* `environment`: The name of the environment the component is to be actioned against, therefore implying the variables file(s) to be included
136138
* `group` (optional): The name of the group to which the environment belongs, permitting the use of a group tfvars file as a "meta-environment" shared by more than one environment
139+
* `lockfile` (optional): Passes the given lockfile mode to terraform.
137140
* `project`: The name of the project being deployed, as per the default bucket-prefix and state file keyspace
138141
* `region` (optional): The AWS region name unique to all components and terraform processes. Defaults to the value of the _AWS_DEFAULT_REGION_ environment variable.
139142
* `detailed-exitcode` (optional): Passes detailed exit code flag to terraform.
143+
* `lock-table` (optional): Tells tfscaffold to use a DynamoDB Table with the same name as the S3 Bucket for state file locking
140144
* `no-color` (optional): Passes no-color flag to terraform.
141145
* `compact-warnings` (optional): Passes compact-warnings flag to terraform.
142146
* `additional arguments`: Any arguments provided after "--" will be passed directly to terraform as its own arguments, e.g. allowing the provision of a 'target=value' parameter.

bin/terraform.sh

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
##
99
# Set Script Version
1010
##
11-
readonly script_ver="1.7.1";
11+
readonly script_ver="1.9.0";
1212

1313
##
1414
# Standardised failure function
@@ -38,10 +38,12 @@ Usage: ${0} \\
3838
-e/--environment [environment] \\
3939
-g/--group [group]
4040
-i/--build-id [build_id] (optional) \\
41+
-l/--lockfile [mode] \\
4142
-p/--project [project] \\
4243
-r/--region [region] \\
4344
-d/--detailed-exitcode \\
4445
-n/--no-color \\
46+
-t/--lock-table \\
4547
-w/--compact-warnings \\
4648
-- \\
4749
<additional arguments to forward to the terraform binary call>
@@ -92,12 +94,21 @@ detailed-exitcode (optional):
9294
Changes the plan operation to exit 0 only when there are no changes.
9395
Will be ignored for actions other than plan.
9496
97+
lock-table (optional):
98+
When not provided, false.
99+
Adds a dynamodb_table statement to the S3 backend configuration
100+
to use a DynamoDB table with the same name as the S3 bucket
101+
for terraform state locking.
102+
95103
no-color (optional):
96104
Append -no-color to all terraform calls
97105
98106
compact-warnings (optional):
99107
Append -compact-warnings to all terraform calls
100108
109+
lockfile:
110+
Append -lockfile=MODE to calls to terraform init
111+
101112
additional arguments:
102113
Any arguments provided after "--" will be passed directly to terraform as its own arguments
103114
EOF
@@ -116,8 +127,8 @@ fi
116127
##
117128
readonly raw_arguments="${*}";
118129
ARGS=$(getopt \
119-
-o dhnvwa:b:c:e:g:i:p:r: \
120-
-l "help,version,bootstrap,action:,bucket-prefix:,build-id:,component:,environment:,group:,project:,region:,detailed-exitcode,no-color,compact-warnings" \
130+
-o dhntvwa:b:c:e:g:i:l:p:r: \
131+
-l "help,version,bootstrap,action:,bucket-prefix:,build-id:,component:,environment:,group:,project:,region:,lockfile:,detailed-exitcode,lock-table,no-color,compact-warnings" \
121132
-n "${0}" \
122133
-- \
123134
"$@");
@@ -138,8 +149,10 @@ declare group;
138149
declare action;
139150
declare bucket_prefix;
140151
declare build_id;
152+
declare lockfile;
141153
declare project;
142154
declare detailed_exitcode;
155+
declare lock_table;
143156
declare no_color;
144157
declare compact_warnings;
145158
declare out="";
@@ -205,6 +218,13 @@ while true; do
205218
shift;
206219
fi;
207220
;;
221+
-l|--lockfile)
222+
shift;
223+
if [ -n "${1}" ]; then
224+
lockfile="-lockfile=${1}";
225+
shift;
226+
fi;
227+
;;
208228
-p|--project)
209229
shift;
210230
if [ -n "${1}" ]; then
@@ -220,6 +240,10 @@ while true; do
220240
shift;
221241
detailed_exitcode="true";
222242
;;
243+
-t|--lock-table)
244+
shift;
245+
lock_table="true";
246+
;;
223247
-n|--no-color)
224248
shift;
225249
no_color="-no-color";
@@ -272,6 +296,7 @@ if [ "${bootstrap}" == "true" ]; then
272296
&& error_and_die "The --bootstrap parameter and the -c/--component parameter are mutually exclusive";
273297
[ -n "${build_id}" ] \
274298
&& error_and_die "The --bootstrap parameter and the -i/--build-id parameter are mutually exclusive. We do not currently support plan files for bootstrap";
299+
[ -n "${environment_arg}" ] && readonly environment="${environment_arg}";
275300
else
276301
# Validate component to work with
277302
[ -n "${component_arg}" ] \
@@ -536,13 +561,8 @@ The following input variables appear to be duplicated:
536561
537562
${duplicate_variables}
538563
539-
This could lead to unexpected behaviour. Overriding of variables
540-
has previously been unpredictable and is not currently supported,
541-
but it may work.
542-
543-
Recent changes to terraform might give you useful overriding and
544-
map-merging functionality, please use with caution and report back
545-
on your successes & failures.
564+
Ensure this is intentional behaviour, and that the order of
565+
precedence for variable values is as you expect.
546566
###################################################################";
547567

548568
# Build up the tfvars arguments for terraform command line
@@ -579,13 +599,25 @@ else
579599
fi;
580600

581601
readonly backend_key="${backend_prefix}/${backend_filename}";
582-
readonly backend_config="terraform {
602+
declare backend_config
603+
if [ "${lock_table}" == "true" ]; then
604+
backend_config="terraform {
605+
backend \"s3\" {
606+
region = \"${region}\"
607+
bucket = \"${bucket}\"
608+
key = \"${backend_key}\"
609+
dynamodb_table = \"${bucket}\"
610+
}
611+
}";
612+
else
613+
backend_config="terraform {
583614
backend \"s3\" {
584615
region = \"${region}\"
585616
bucket = \"${bucket}\"
586617
key = \"${backend_key}\"
587618
}
588619
}";
620+
fi;
589621

590622
# We're now all ready to go. All that's left is to:
591623
# * Write the backend config
@@ -621,16 +653,21 @@ if [ "${bootstrapped}" == "true" ]; then
621653
# Nix the horrible hack on exit
622654
trap "rm -f $(pwd)/backend_tfscaffold.tf" EXIT;
623655

656+
declare lockfile_or_upgrade;
657+
[ -n ${lockfile} ] && lockfile_or_upgrade='-upgrade' || lockfile_or_upgrade="${lockfile}";
658+
624659
# Configure remote state storage
625660
echo "Setting up S3 remote state from s3://${bucket}/${backend_key}";
626-
terraform init -upgrade ${no_color} ${compact_warnings} \
661+
[ "${lock_table}" == "true" ] && echo "Using DynamoDB Table for state locking: ${bucket}";
662+
terraform init ${no_color} ${compact_warnings} ${lockfile_or_upgrade} \
627663
|| error_and_die "Terraform init failed";
628664
else
629665
# We are bootstrapping. Download the providers, skip the backend config.
630666
terraform init \
631667
-backend=false \
632668
${no_color} \
633669
${compact_warnings} \
670+
${lockfile} \
634671
|| error_and_die "Terraform init failed";
635672
fi;
636673

@@ -739,7 +776,7 @@ case "${action}" in
739776

740777
# Push Terraform Remote State to S3
741778
# TODO: Add -upgrade to init when we drop support for <0.10
742-
echo "yes" | terraform init || error_and_die "Terraform init failed";
779+
echo "yes" | terraform init ${lockfile} || error_and_die "Terraform init failed";
743780

744781
# Hard cleanup
745782
rm -f backend_tfscaffold.tf;

0 commit comments

Comments
 (0)