11This example demonstrates a basic usage of this module.
22
3- On a system, you have a root module tfstate-s3-manager which
4- includes terraform-aws-multi-stack-backends, and the main.tf
5- defines the stacks to manage:
6-
7- - there are 2 stacks, each with 2 root modules, so a total of
8- 4 folders
9- - the structure of the folder is irrelevant, as they are
10- specified by path so wherever they are located on the local
11- system, is where terraform-aws-multi-stack-backends will
12- look for them
13-
14- First you ` terraform apply ` each root module of each stack.
15- The order does not matter.
3+ On your system,
4+
5+ - you have a root module tfstate-s3-manager which
6+ includes terraform-aws-multi-stack-backends
7+ - you have (in this example) 4 root modules, each in their own folder
8+ - two of those root modules belong to one stack, the other two belong
9+ to the other stack
10+ - the ` tfstate-s3-manager/main.tf ` defines the stacks to manage:
11+ ``` hcl
12+ stacks_map = {
13+ stack-1 = {
14+ network = {
15+ path = "../stack1-network"
16+ }
17+ cluster = {
18+ path = "../stack1-cluster"
19+ }
20+ },
21+ stack-2 = {
22+ network = {
23+ path = "../stack2-network"
24+ }
25+ cluster = {
26+ path = "../stack2-cluster"
27+ }
28+ },
29+ }
30+ ```
31+ - the names are arbitrary (stack-1, network, etc)
32+
33+ See the left green box in following diagram:
34+
35+ ![ Root modules on local filesystem (left green box)] ( stack-backends-setup.png )
36+
37+ ### Step 1
38+
39+ Assume you have already run ` terraform apply ` in each root
40+ module of each stack. The order does not matter. This step
41+ can be done after step 2 as well.
1642
1743Then ` terraform apply ` in the ` tfstate-s3-manager ` root module,
1844which will create the common bucket, its replica, dynamodb
@@ -22,21 +48,50 @@ in the `main.tf`). See the following diagram.
2248
2349![ Step 1: setup common bucket and stack backend files] ( stack-backends-setup.png )
2450
51+ ### Step 2
52+
2553Then you should (although this is optional) move the
2654` tfstate-s3-manager ` tfstate to s3, by running ` terraform init `
2755for that root module. This can be done before or after step 3.
2856
2957![ Step 2: move manager state to s3] ( move-manager-to-s3.png )
3058
59+ ### Step 3
60+
3161Finally you can go to each stack's root module's and `terraform
3262init`. This will move each stack's root module's tfstate from
3363local host to s3, using the ` backend.tf ` generated by the
3464` tfstate-s3-manager ` . See the following diagram.
3565
3666![ Step 3: move stack states to s3] ( move-stacks-to-s3.png )
3767
68+ ### Subsequently
69+
3870From then on, whenever you ` terraform apply ` in one of the
3971root modules associated with a stack, the * whole stack* will
4072get locked. See the following diagram.
4173
4274![ Stack state locking in s3] ( stack-state-locking.png )
75+
76+ To delete a root module from a stack, if the stack has other
77+ root modules in main.tf
78+
79+ - ` terraform destroy ` for the root module you no longer need
80+ - edit the ` tfstate-s3-manager/main.tf ` by removing the entry;
81+ ATTENTION: if there are no modules left for that stack in main.tf,
82+ * leave the stack's empty map* so that the dynamodb table does not
83+ get destroyed yet, eg
84+ ``` hcl
85+ stacks_map = {
86+ stack-3 = {}, // no longer needed
87+ }
88+ ```
89+ - depending on your situation:
90+
91+ - just delete the module folder, OR
92+ - ` terraform apply ` to remove the corresponding ` backend.tf ` then
93+ ` terraform init `
94+
95+ Once a stack's module have all been removed, you can delete the empty
96+ stack map from stacks_map, and ` terraform apply ` to remove the dynamodb
97+ table for that stack.
0 commit comments