Skip to content

Commit

Permalink
WIP for script
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Schoenborn authored and Oliver Schoenborn committed Feb 7, 2023
1 parent 61f625c commit 7fd3778
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
29 changes: 9 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,16 @@ Here is a different procedure that has fewer steps, some are simpler than previo
you don't need to delete the original bucket until the very end. It will be especially useful if you
have many sub-stacks. HOWEVER I have not tested yet, so please test it first.

1. change the value of `backends_bucket_name` (this new value is referred to here
1. WARN your team that no one can use terraform on this code, and ENSURE THAT TERRAFORM PLAN SHOWS
NO CHANGES NEEDED
2. change the value of `backends_bucket_name` (this new value is referred to here
as `NEW_BACKENDS_BUCKET_NAME`)
2. delete the `backend.tf` of this manager module
3. run `terraform init -migrate-state` to bring the manager's tfstate back to local
4. make terraform forget about the 2 current buckets:
```
terraform state rm module.tfstate_backends.module.multi_stack_backends.aws_s3_bucket.tfstate_backends
terraform state rm module.tfstate_backends.module.multi_stack_backends.aws_s3_bucket.replica
```
5. run `terraform apply` which will create the new buckets, replace the lock table for new name,
create a new `backend.tf` for manager, overwrite the `backend.tf` of all sub-stacks
in `var.stacks_map`, etc
6. copy tfstates to the new bucket just created, except the old manager state:
```
aws s3 cp s3://BACKENDS_BUCKET_NAME s3://NEW_BACKENDS_BUCKET_NAME
aws s3 rm s3://NEW_BACKENDS_BUCKET_NAME/_manager_
```
7. run `terraform init -migrate-state` to move the manager's tfstate back into s3
8. If you had any `terraform_remote_state` in your sub-stacks, point them to the new location
9. running `terraform apply` in any of the sub-stacks should show no init and no changes needed
10. manually delete the 2 old buckets
3. determine the path to the manager module in your tfstate (look at your code or output of
terraform state list)
4. run `script/rename-backends-manager-bucket.sh NEW_BUCKET_NAME MODULE_PATH`
5. If you had any `terraform_remote_state` in your sub-stacks, point them to the new location
6. running `terraform apply` in any of the sub-stacks should show no init and no changes needed
7. manually delete the 2 old buckets

## Upgrades

Expand Down
28 changes: 26 additions & 2 deletions scripts/rename-backends-manager-bucket.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bash

set +eu

usage() {
echo
echo "ERROR: missing command line arguments"
Expand Down Expand Up @@ -33,7 +34,17 @@ fi

current_bucket=$3
if [[ -z $current_bucket ]]; then
if [[ ! -f ./backend.tf ]]; then
echo "ERROR: No ./backend.tf file found. You must specify the bucket name as third arg."
exit 8
fi

current_bucket=$(sed -En 's/ *bucket *= *"([a-z0-9][a-z0-9.-]+[a-z0-9])"/\1/p' backend.tf)
if [[ -z $current_bucket ]]; then
echo "ERROR: Could not determine current bucket from ./backend.tf."
exit 10
fi

echo "Current bucket is: $current_bucket"
read -p "Is this correct (y/n)? " -n 1 -r
echo
Expand All @@ -42,6 +53,11 @@ if [[ -z $current_bucket ]]; then
fi
fi

if [[ $current_bucket == $new_bucket_name ]]; then
echo "ERROR: current and new bucket names are the same!!"
exit 5
fi

echo
echo "Moving manager tfstate to local host, without confirmation from user"
rm -f backend.tf
Expand All @@ -55,8 +71,8 @@ terraform state rm $manager_state_prefix.aws_s3_bucket.replica
echo
echo "Running terraform apply"
echo "which will create the new buckets, replace the lock table for new name,"
echo "create a new `backend.tf` for manager, overwrite the `backend.tf` of all sub-stacks"
echo "in `var.stacks_map`, etc"
echo "create a new 'backend.tf' for manager, overwrite the 'backend.tf' of all sub-stacks"
echo "in 'var.stacks_map', etc"
echo
terraform apply

Expand All @@ -67,3 +83,11 @@ aws s3 rm "s3://$new_bucket_name/_manager_" --recursive

# move the manager's tfstate back into s3
terraform init -migrate-state -force-copy

echo
echo "DONE!"
echo "NOTE: VERIFY that all tfstates have been properly transfered. Eg, run 'terraform apply'"
echo "in all substacks: no changes should be planned."
echo "ONCE YOU ARE SATISFIED, YOU CAN MANUALLY DELETE THE TWO PREVIOUS BUCKETS."
echo "Example: aws s3 rb \"s3://$current_bucket\" --force"
echo "Example: aws s3 rb \"s3://${current_bucket}-replica\" --force"

0 comments on commit 7fd3778

Please sign in to comment.