In practice
I pick Terraform when infrastructure can be described in files and deployed like code, instead of clicked together in a provider console. It is in the stack of CAS Marketplace, Gaia Lens, TPH Global and Kuma.de. For one server with containers its overhead does not pay off.
What is Terraform?
Terraform is an infrastructure as code tool: you describe cloud and on-premises resources in readable configuration files that can be versioned and reused. The configuration is declarative, it describes the end state, and Terraform works out the order and dependencies between resources itself.
It reaches platforms through providers, which let it work with their APIs; those for AWS and Google Cloud sit in the Terraform Registry. Before a change it shows a plan, and after approval it carries the plan out. What it created is recorded in a state file that ties resources to the configuration.
- Configuration
- Readable files that can be versioned
- Model
- Declarative, describes the end state
- Changes
- Plan first, then approval
- State
- A file that ties resources to the config
How I work with Terraform
I start by asking whether the infrastructure has outgrown clicking by hand. One server with Docker does not need Terraform, several environments and cloud services do.
Configuration in the repository
I describe resources in files next to the application code and deploy them the same way, with review. Repeating groups of resources go into modules, reusable pieces of configuration.
The plan before a change
Before every change I read the plan, the list of resources Terraform will create, change or destroy. I check the deletions most carefully, because those are the hardest to undo.
State off the laptop
By default state sits in a local file, and a remote backend is the better choice, because a local file makes teamwork harder and is easy to lose. I keep state remote and out of the repository, with access for the people who deploy.
When it fits, and when it doesn't
Terraform makes sense where there is enough infrastructure for running it by hand to start hurting.
A good fit when
- A test and a production environment that should look the same.
- Infrastructure made of several cloud services that someone must be able to rebuild from scratch.
- A team where an infrastructure change should go through review like code.
- A project on several providers or with a possible change of provider.
Not a fit when
- One VPS with containers in Docker, whose configuration fits in a single file.
- A prototype that will be thrown away anyway.
- A team where nobody will take over the state file or read the plans.
When a team prefers to describe infrastructure in TypeScript, I consider Pulumi or AWS CDK. Pulumi and Terraform sit together in the stack of Gaia Lens.
Frequently asked questions
Is your infrastructure growing faster than your knowledge of it?
List what runs today and how you deploy it. I will answer whether this is the right moment for Terraform or whether a simpler deployment is better.