Introduction to Terraform
I had the privilege of being an NFD31 delegate in April 2023. HashiCorp gave a good demo on Infrastructure-as-Code (IaC) using Terraform. I had not used Terraform for some time, so this 3-part blog post series is my Terraform refresher.
This blog post focuses on Terraform basics.
What is Terraform?
Terraform is an Infrastructure-as-Code tool that lets you define your infrastructure in the cloud or on-prem in human-readable code that can be versioned and shared. You can deploy this code manually or use automation pipelines to create, update, or delete your infrastructure.
What are Terraform Providers?
Terraform has Providers for each platform. These Providers make API calls to their platform depending on the code’s intent, acting like a proxy for the API calls. For example, the Terraform Azure Provider will make an API call to Azure for VNET creation if that’s what you want your Terraform code to do. HashiCorp, vendors, and the community write thousands of providers to manage different types of resources. The providers are listed at https://registry.terraform.io/.
Terraform providers are written in Go. Terraform does not really know what to do with the target API, but the providers provide all the intelligence to orchestrate against an API. Providers are responsible for CRUD (Create, Update, Delete) operations and have the required logic and dependencies built in. For example, suppose you are trying to create a VNET in Azure. In that case, the Azure Provider knows that a VNET has to be created in an Azure Region and has to belong to a Resource Group and will handle the dependencies accordingly.
Imperative vs Declarative
It is important to note that Terraform uses a Declarative style of programming.
The imperative style focuses on “how” to achieve the target state, and you have to provide steps to be taken to reach from the current state to the target state. The declarative style focuses on “what” your end state is. You define the target state, and the tool automatically figures out how to achieve the target state. Terraform uses a declarative style, and tools like Ansible and Azure CLI use an imperative style. Imperative style does not keep track of the current state of your infrastructure, while declarative style does. Hence, Terraform has a state file with information about your infrastructure’s current state. We will learn more about the state file in the next blog.
Terraform Workflow
Terraform Workflow is a three-step process: Write, Plan, and Apply.
Write – Write your Terraform code to deploy your infrastructure using the specific providers as needed. This deployment could be for a single or multiple cloud providers.
Plan – Terraform will create an implementation plan to exactly show the infrastructure resource that it will create, update, or delete. It will compare the current state of infrastructure with the intent of your code and present a plan, i.e., a diff.
Apply – Terraform will apply the changes, aka implement the plan it showed you in the previous step. Before applying, Terraform will resolve the dependencies as needed and then create the resources based on your code.
The next blog post will focus on basic Terraform code to create Azure Network resources.
References
HashiCorp – https://developer.hashicorp.com/terraform/intro
Terraform at NFD31 – https://techfieldday.com/appearance/hashicorp-presents-at-networking-field-day-31