Article Outline
- Introduction
- Terraform Architecture
- Terraform Workflow
- Terraform Best Practices
- Conclusion
- References
1. Introduction
Infrastructure as Code (IaC), is an approach to managing and provisioning infrastructure by writing code instead of the manual processes , “ClickOps”. IaC can be described as a mindset where you treat all aspects of operations (servers, databases, networks) as software. When you define your infrastructure using code , it enables you to automate and use all the best practices of software development. IaC eliminates human errors , speeds up infrastructure deployments and ensures infrastructure is version-controlled, just like software code.
Terraform is an open-source tool developed by HashiCorp and the most popular and widely used IaC tool used by DevOps, SREs and cloud architects. Terraform is widely used because of it’s declarative syntax, platform agnostic and its simplicity. Understanding how terraform works behind the hood will go along way to help you in write better terraform code.
In this article, we will explore Terraform architecture, its core components, and how it orchestrates infrastructure provisioning efficiently.
2. Terraform Architecture
Terraform follows a standard architecture to fulfill the necessary IaC tasks. Terraform architecture mainly consists of the following components:
- Terraform core
- Plugins (Providers and Provisioners)
- Terraform State
Terraform core
Terraform core is the engine/brain behind how terraform works. It is responsible for reading configurations files , building the dependency graphs from resources and data sources, managing state and applying changes. Terraform Core does not directly interact with cloud providers but communicates with plugins via remote procedure calls (RPCs) and the plugins in turn communicates with their corresponding platforms via HTTPs.
Plugins (Providers and Provisioners)
Terraform ability is enhance by plugins, which enable terraform to interact with cloud services and configure resources dynamically. Plugins acts as connectors or the glue between terraform and external APIs such as AWS, Azure, GCP, Kubernetes, Docker etc. Each plugin is written in the Go programming language and implements a specific interface. Terraform core knows how to install and execute plugins. Provisioners in Terraform are used to execute scripts or commands on a resource after it has been created or modified.
Terraform State
State is one of the most important core components of Terraform. Terraform state is a record about all the infrastructure and resources it created. It is a costumed JSON file that terraform uses to map real world resources to your configuration, keep track of metadata, and to improve performance for large infrastructures. By default, state is stored in a local file named “terraform.tfstate”. You can read more about terraform state here
There are two ways to manage state:
Local State: Local State refers to the default way by which Terraform stores state files (terraform.tfstate). It is suitable for small-scale projects or development environments and single person managing Terraform.
Remote State: Remote State refers to storing the Terraform state file (terraform.tfstate) in a remote backend rather than locally on your machine. This enables collaboration, prevents state loss, and supports features like state locking and versioning. Some common remote backends include AWS S3,Terraform Cloud, Azure Blob Storage etc. More on Remote State
3. Terraform Workflow
Terraform follows a structured execution flow to provision, update, and manage infrastructure. This process ensures that infrastructure is deployed in a controlled and predictable manner. Terraform workflow consist of mainly three steps:
Write: The first step is to write your terraform configuration just like any other code using any editor of your choice.
Plan: This is the step where you review your configurations. Terraform plan will define the infrastructure to be created, modified, or destroyed depending on the current configuration and infrastructure.
Apply: The final step in the workflow is Apply, where you are ready to provision real infrastructure. Once you approve of the changes ,terraform will go ahead perform the desired actions as defined execution.
4. Terraform Best Practices
You should never edit the Terraform state files by hand or write code that reads them directly. If for some reason you need to manipulate the state file which should be a relatively rare occurrence, use the terraform import or terraform state commands.
It’s a good practice to store your work in a version controlled repository even when you’re just operating as an individual.
When working as a team, it’s important to delegate ownership of infrastructure across these teams and empower them to work in parallel without conflicts.
Never Store your state file in a version controlled repository.
Always use state locking on your state files to prevent data loss, conflicts and state file corruption.
Integrate Terraform to your CI/CD pipelines to make your DevOps pipeline efficient.
5. Conclusion
Well well, we have come to the end of this deep dive into terraform Architecture.
To learn more about Terrafrom visit the official Terraform page . Don't forget to add your comments , till then keep coding.
.
6 References
- https://developer.hashicorp.com/terraform/language/state
- https://thinksys.com/cloud/terraform-architecture-best-practices/
- https://developer.hashicorp.com/terraform/intro/core-workflow
- https://spacelift.io/blog/terraform-architecture
- https://developer.hashicorp.com/terraform/cloud-docs/recommended-practices
- Terraform:Up & Running , Third Edition by Yevgeniy Brikman
Top comments (0)