The Core Terraform Workflow
init, validate, plan, and apply in detail, reading a plan's symbols, destroying safely, and other useful commands.
The core workflow
Working with Terraform follows the same short cycle every time: initialize the folder, validate the code, preview the changes with a plan, then apply them. Most of a DBA's safety comes from one habit: always read the plan before applying it.
1. Initialize the directory
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding oracle/oci versions matching ">= 6.0"...
- Installing oracle/oci...
Terraform has been successfully initialized!
- What it does
- Prepares the working directory. It reads the configuration files, downloads the provider plugins they need (such as OCI, AWS, or Azure) into a hidden
.terraformfolder, downloads any modules, and sets up the backend, where state will be stored. It also writes a.terraform.lock.hclfile recording the exact provider versions, which you commit to Git so everyone uses the same ones. - When to run it
- The first time you work in a project, after cloning it, and whenever you add or change providers, modules, or the backend. It's safe to run again at any time.
2. Validate the code syntax (optional but recommended)
# tidy the formatting first, then check the code
$ terraform fmt
$ terraform validate
Success! The configuration is valid.
- What it does
- Checks that the configuration's syntax and internal logic are correct, such as missing brackets, unknown arguments, wrong types, or references to variables that don't exist, without connecting to any cloud. It's quick, needs no credentials, and is ideal as the first check in a pipeline.
- What it doesn't catch
- Problems only the cloud knows about, such as a name that's already taken, a size that isn't available, or missing permissions. Those appear at plan or apply.
- terraform fmt
- Rewrites files in the standard layout, so code from different people looks the same and diffs stay clean. Pipelines often run
terraform fmt -checkto reject untidy code.
3. Preview your changes
$ terraform plan -var-file=dev.tfvars -out=dev.tfplan
Terraform will perform the following actions:
# oci_database_autonomous_database.sales will be created
+ resource "oci_database_autonomous_database" "sales" {
+ compute_count = 2
+ db_name = "SALESDEV"
+ db_workload = "OLTP"
+ is_auto_scaling_enabled = true
+ id = (known after apply)
...
}
Plan: 1 to add, 0 to change, 0 to destroy.
- What it does
- Generates an execution plan. It reads the state, checks the real infrastructure through the providers, compares both with your code, and shows exactly what would be created, changed, or destroyed. It doesn't make any changes.
- Saving the plan
-out=dev.tfplansaves the plan to a file, so the apply that follows does exactly what was reviewed, and nothing else.
Each change in a plan is marked with a symbol. These are the ones to watch for:
| Symbol | Meaning | For a database |
|---|---|---|
+ | Create | A new database will be built |
~ | Update in place | A setting changes on the existing database, such as more ECPUs |
-/+ | Destroy and re-create (replace) | Danger: the database would be deleted and a new, empty one created. The plan says which argument "forces replacement". |
- | Destroy | Danger: the database would be deleted |
4. Deploy your infrastructure
$ terraform apply dev.tfplan
oci_database_autonomous_database.sales: Creating...
oci_database_autonomous_database.sales: Still creating... [1m0s elapsed]
oci_database_autonomous_database.sales: Creation complete after 2m14s
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
database_id = "ocid1.autonomousdatabase.oc1.iad.aaaa..."
- What it does
- Executes the actions in the plan, calling the cloud APIs to provision or update resources, in the right order, running independent changes in parallel. It then updates the state and prints the outputs.
- With or without a saved plan
- Applying a saved plan file runs it straight away. Running
terraform applyon its own makes a fresh plan, shows it, and waits for you to typeyes. - If something fails
- Terraform stops, keeps what succeeded in the state, and reports the error. Fix the cause, then plan and apply again; it carries on from where it stopped.
Destroying infrastructure
$ terraform destroy -var-file=dev.tfvars
terraform destroy removes everything the configuration manages, after showing a plan and asking for confirmation. It's useful for temporary environments, such as a test database built for one sprint, and saves money. For anything holding data you care about, protect it from destruction, as shown in Terraform for Databases.
Other useful commands
| Command | What it does |
|---|---|
terraform output | Shows the outputs again, such as a database ID |
terraform show | Shows the current state, or a saved plan, in readable form |
terraform state list | Lists every resource Terraform manages in this state |
terraform plan -refresh-only | Shows only what's changed outside Terraform: drift |
terraform console | An interactive prompt for trying out expressions and functions |
In practice: "1 to destroy"
A teammate's pull request renames a database in Terraform, from sales to sales_prod, to match the team's naming standard. It looks harmless, but the plan posted on the pull request ends with:
Plan: 1 to add, 0 to change, 1 to destroy.
Kofi reads the plan, not just the code. For that resource, the name can't be changed in place, so Terraform would delete the production database and create a new, empty one. Kofi blocks the merge. The team keeps the old name, adds prevent_destroy to every database resource, and makes "read the summary line" part of every review. One line of the plan saved the company's order history.
Key takeaways
terraform initprepares the folder: providers, modules, and backend. Run it first, and after adding providers or modules.terraform validatechecks syntax and logic offline;terraform fmtkeeps the code tidy.terraform planpreviews every create, update, replace, and destroy, without changing anything. Save it with-out.terraform applymakes the changes, ideally from the reviewed, saved plan.- Read the plan's summary first, and treat any unexpected replace (
-/+) or destroy (-) of a database as a stop sign.