Skip to main content

Destroying Infrastructure

Removing infrastructure is the operation nobody rehearses. The command is easy enough, and the uncertainty is not about the command: it is whether the thing you are about to remove is still holding something up, and whether the list on screen is the whole list.

In Octo you do not run a destroy command. You take the thing out of your intent and run the same four steps as always, and the removal is reported the same way every other change is reported, at the level you removed it.

Removing one piece​

This page works on the shop as its first pass leaves it, a region and the receipts bucket, before checkout is added. To remove the bucket, comment out its module:

octo.yaml
#  - moduleId: receipts-module
# moduleClass: AwsS3StorageServiceModule
# moduleInputs:
# region: "${{region-module.model.region}}"
# bucketName: "${env.RECEIPTS_BUCKET_NAME}"
# accessPrefixes:
# - uploads

Then generate:

npm run octo:generate
INFO: Wrote 1 terragrunt folder(s) to ".octo-tf", emptied 1 (deleted from intent). {"emptied":["receipts-module"],"outputDir":".octo-tf","written":["region-module"]}

"Emptied" is the important word. Octo did not delete .octo-tf/receipts-module/; it rewrote it with the Terraform block and the providers, and no resources. Terragrunt still finds the folder, Terraform still has its state, and an empty configuration against a populated state is exactly what makes the next apply destroy those resources.

Deleting the folder would do the opposite. Terraform would never look at that state again, and the bucket would sit in AWS with nothing tracking it. Generate never deletes a folder for this reason; the commit after the destroy does, once the folder's state holds nothing.

Reading what will go​

npm run octo:validate
You removed receipts-module. That destroys or replaces 2 things — check those before anything else.
- 2 destroy ± 0 replace + 0 create ~ 0 update

WHAT WILL CHANGE
- removed receipts-module [receipts-module | grouped by module | -2]
- bucket-shop-receipts-914372
- aws_s3_bucket_public_access_block.bucket-shop-receipts-914372-public-access
- aws_s3_bucket.bucket-shop-receipts-914372

DATA LOSS — destroying these loses the data they hold
[receipts-module] Terraform plans to destroy "aws_s3_bucket.bucket-shop-receipts-914372", and the data resource "bucket-shop-receipts-914372" holds is lost with it.

Destroys sort to the top of the report, and the counters lead with them. A removal card is headed by the module rather than by a model context, because the models are gone from your intent, and the module ID is the only name left that means anything.

Read the whole list before going further. The question worth asking is not whether these should go, but whether anything you did not remove is quietly in there too.

Applying it​

npm run octo:lock
npm run octo:apply
This apply would DESTROY:
[receipts-module] aws_s3_bucket_public_access_block.bucket-shop-receipts-914372-public-access (delete)
[receipts-module] aws_s3_bucket.bucket-shop-receipts-914372 (delete) - the data it holds is lost with it

To approve exactly these, re-run with:
--allowDestroy 'receipts-module/aws_s3_bucket_public_access_block.bucket-shop-receipts-914372-public-access'
--allowDestroy 'receipts-module/aws_s3_bucket.bucket-shop-receipts-914372'

Error: Refusing to apply. Run "octo validate" to see why each of these is being destroyed, then re-run with --allowDestroy for each one you mean, or --auto-approve for all of them!

Terragrunt is always given -auto-approve, so there is no prompt to answer during an apply. This check is what stands in for one: octo apply plans first, and refuses if anything would be destroyed. A destroy that takes data with it, like the bucket's objects, says so on its line.

npm run octo:apply -- --allowDestroy 'receipts-module/aws_s3_bucket.bucket-shop-receipts-914372'

Each --allowDestroy says you have read that one line. A destroy you did not name is still refused, so one that appears after you read the list cannot ride along:

This apply would DESTROY:
[receipts-module] aws_s3_bucket_public_access_block.bucket-shop-receipts-914372-public-access (delete)

To approve exactly these, re-run with:
--allowDestroy 'receipts-module/aws_s3_bucket_public_access_block.bucket-shop-receipts-914372-public-access'

Error: Refusing to apply. Run "octo validate" to see why each of these is being destroyed, then re-run with --allowDestroy for each one you mean, or --auto-approve for all of them!

--auto-approve lets every destroy through at once, for a plan whose every line you have read, such as taking everything down.

The refusal keeps the lock, so if the list tells you not to go ahead, release it with npm run octo:unlock -- --lockId <the ID octo lock printed>.

Committing the removal​

npm run octo:commit
WHAT CHANGED
- removed receipts-module [receipts-module | grouped by module | -2]
- bucket-shop-receipts-914372
- aws_s3_bucket_public_access_block.bucket-shop-receipts-914372-public-access
- aws_s3_bucket.bucket-shop-receipts-914372

MODULES THAT NO LONGER RENDER ANYTHING
receipts-module

Committing after a destroy is not optional. Until it runs, Octo's record still says the bucket exists, and anything Terraform did not delete itself has not been dealt with yet.

The last section names the folders that are now empty. The commit deletes each one whose Terraform state holds nothing, and prints a line for it:

Deleted folder "receipts-module", whose module left intent and whose destroy is committed.

A folder whose destroy did not finish, after a failed apply, keeps its state and stays.

Deleting is not renaming​

The one case worth being careful about. Change the bucket name in the shop's definition file and the report says:

WHAT WILL CHANGE
+ added service=shop-receipts-v2-s3-storage [service=shop-receipts-v2-s3-storage,region=eu-west-1,account=123456789012,app=shop | receipts-module | +2]
+ bucket-shop-receipts-v2-914372
+ aws_s3_bucket_public_access_block.bucket-shop-receipts-v2-914372-public-access
+ aws_s3_bucket.bucket-shop-receipts-v2-914372
- removed receipts-module [receipts-module | grouped by module | -2]
- bucket-shop-receipts-914372
- aws_s3_bucket_public_access_block.bucket-shop-receipts-914372-public-access
- aws_s3_bucket.bucket-shop-receipts-914372

A rename is a destroy and a create, because a resource's identity is derived from what you wrote. For a bucket that means the objects inside it. The report tells you before it happens, and the apply refuses until you approve that destroy, but nothing will stop you if you insist.

A moduleId is different. Rename receipts-module and the bucket keeps its resource ID, because the ID comes from the models, not from the module; only its folder changes. Octo moves the bucket's Terraform state from receipts-module/ into the new folder before Terraform plans, so the apply destroys nothing and needs no approval, and the commit deletes the old, empty folder.

Taking everything down​

Comment out every module except the app and the account, which produce nothing in AWS themselves and are what the rest of the file hangs off. A tags entry scoped to one of the modules you commented out goes with it, or generate stops with Module not found! and the ID that entry names:

npm run octo:generate

npm run octo:lock
npm run octo:apply -- --auto-approve
npm run octo:commit

Afterwards, .octo/ still exists and describes an app with nothing in it, and the commit has deleted every folder in .octo-tf/. That is the correct resting state, and re-adding a module from here works normally.