IaC Fundamentals
What Infrastructure as Code is, why it matters, and how Terraform handles multi-cloud and hybrid-cloud workflows.
What is Infrastructure as Code?
Infrastructure as Code (IaC) is defining infrastructure in a declarative manner, meaning you write the code in configuration files, instead of setting it up by clicking around in consoles or with one-off scripts. Infrastructure here usually means servers, but also covers networks, DNS, databases and so on.
Terraform talks to platforms and services through their API, using providers. It keeps track of your real infrastructure in a state file, which acts as a source of truth.
The workflow consists of three parts:
- Write
- Plan
- Apply
When you have finished writing the code, you plan which means that Terraform builds an execution plan, showing what will be either created, destroyed or updated. Then when you have approved, Terraform will apply the changes in the correct order, respecting dependencies.
Terraform is not configuration management. Ansible, Chef and Puppet configures software on servers that already exist, however you can use these in tandem with each other.
Advantages of IaC patterns
With Terraform, you can manage any infrastructure, there are literally thousands of providers, whereas Cloud-specific providers such as CloudFormation (AWS) and ARM/Bicep (Azure) are agnostic to their cloud provider, or you can even write your own.
You also get to track your infrastructure, meaning that state records what's deployed and plan shows changes before they happen - giving large control of what and when.
Automating changes in a declarative configuration and the dependency graph mean Terraform can handle ordering for you and can build independent resources in parallel.
By standardizing configurations, you can make reusable modules, to save time and enforce best practices. A proven setup that works once, will work everytime.
Because infrastructure is code, you can version control it with Git, so that every change has a history, can go through review and be rolled back with ease. Terraform adds shared state, locking and governance on top.
Multi-cloud, hybrid cloud and service-agnostic workflows
Terraform letrs you manage Azure, AWS, GCP and others with one tool, one language and one workflow. You can even mix providers in the same config, which improves fault tolerence by spreading workloads across clouds, so you team only needs to learn one way of working.
That same workflow can reach into your own datacenter as well, VMware, OpenStack and Proxmox lets you manage on-prem infrastructure right next to your cloud resources.
Terraform isn't limited to servers and networks. Providers can also manage DNS, monitoring, version control, K8s, identity and plenty of SaaS tools.
If it's a service with an API, Terraform can most likey manage it.
The cross-provider dependency graph lets you spin up a VM in AWS, wait for its IP address, then from that create a Cloudflare DNS, all in one run.