diff --git a/README.md b/README.md index 2197588..60bde80 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/scripts/rename-backends-manager-bucket.sh b/scripts/rename-backends-manager-bucket.sh index 15e4e53..7a8c817 100755 --- a/scripts/rename-backends-manager-bucket.sh +++ b/scripts/rename-backends-manager-bucket.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash set +eu + usage() { echo echo "ERROR: missing command line arguments" @@ -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 @@ -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 @@ -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 @@ -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"