Declarative
You describe the result; Terraform works out the steps
The file states what should exist, not how to build it.
The scripts in the last lesson said what to do. Infrastructure as code says what should exist. This block declares a resource. Its type is a cloud server, and web is your name for it. Inside are its settings: how many, which machine image, which size. Hand that to Terraform, and it makes the real world match: three servers. Run it a second time, and it changes nothing. They are already there.
Declarative · Going deeper · 1 of 2
Providers: one tool, many platforms
A provider translates resources into one platform's API calls.
count and for_each · Going deeper · 2 of 2
count numbers the copies; for_each names them
Use for_each when copies have identities: count shifts every index after a removal.
Plan and apply
plan compares what you want with what exists
Configuration, state and reality: plan compares, apply closes the gap.
Terraform keeps three things side by side. The configuration is what you want: three servers. The state is its own record of what it has built: two. And the real infrastructure is what actually exists in the cloud: also two. Plan compares them and reports the difference. One to add. Nothing has changed yet. Apply makes the API call that creates the third server, and writes it into the state. All three now agree.
Plan and apply · Going deeper
The workflow, command by command
init, plan, apply. And fmt and validate before them.
Reading a plan
Read the plan: -/+ means destroy, then create again
+ creates, ~ changes in place, -/+ replaces.
The plan is the most important screen in Terraform, and it has a small alphabet. A plus sign: this will be created. A tilde: this will be changed in place. The resource stays; one of its settings is updated. Minus and plus together: this must be replaced. Terraform will destroy the resource and build a new one. Here that resource is a database. Before you type yes, be sure that is what you want.
lifecycle · Going deeper
lifecycle changes how Terraform replaces, protects or ignores a resource
prevent_destroy makes a destructive plan fail. create_before_destroy avoids a gap.
Drift
A change made by hand is drift; apply will undo it
Reality that differs from the configuration is treated as a mistake to correct.
What happens when someone bypasses the code? The configuration says a small server, and the state agrees. But somebody resized the server by hand in the console. Reality no longer matches. The next plan notices, and proposes to change the size back. You now choose. Apply, and the manual change is undone. Or decide that the change was right, put it in the configuration, and the plan comes back empty.
Drift · Going deeper
Where the state lives
Remote, locked, and never in Git.
Modules
A module is a blueprint; variables make each use differ
One module, many environments, each with its own variables.
Nobody wants to write the same forty resources twice. A module packages a set of resources as one blueprint: here a network, a load balancer and some servers. It has inputs. Development uses the module and asks for one small server. Production uses the very same module and asks for three large ones. The two environments are guaranteed to be built the same way, because they are built by the same code.
Variable precedence · Going deeper
Six places can set a variable, and the last one loaded wins
The command line beats .tfvars files, which beat TF_VAR_ environment variables.
Lesson complete
You finished “Infrastructure as code”
Nicely done.