Skip to main content

Command Palette

Search for a command to run...

Day 45: Terraform

Published
4 min read
Day 45: Terraform
P

Hi, I am an AWS-certified cloud engineer and I write about my progress and learnings of DevOps.

Hello everyone!

As we have completed Ansible tasks, let's move on to the following tool in our DevOps journey, Terraform. In today's era, companies are adopting the multi-cloud model to get the best services from different cloud providers. Terraform is a tool that can facilitate infrastructure deployment in multiple clouds without having to write templates on every cloud provider.

What is Terraform?

Terraform is an infrastructure as code (IaC) tool that allows you to create, manage, and update infrastructure resources such as virtual machines, networks, and storage in a repeatable, scalable, and automated way.

Why use Terraform?

HashiCorp Terraform, as already mentioned, helps engineers automate the process of provisioning their cloud and hybrid infrastructure. With it, you no longer need to go into the cloud provider console - AWS, Azure, or GCP - and set everything up manually.

But what makes Terraform different from other IaC tools - like those mentioned above? Choosing a toolset to manage the infrastructure demands an informed decision in which you weigh up the various advantages and trade-offs to ensure a solution aligns with your specific needs.

Key Features of Terraform:

  1. Declarative Configuration: Terraform uses a declarative syntax to define infrastructure resources. Users describe the desired state of their infrastructure in configuration files, and Terraform ensures that the actual state matches this configuration.

  2. Multi-Cloud Support: Terraform supports multiple cloud providers, including AWS, Azure, Google Cloud Platform (GCP), and many others, making it vendor-agnostic. This allows users to manage resources across hybrid or multi-cloud environments.

  3. Resource Abstraction: Terraform abstracts cloud resources into a consistent and easily manageable format. Users define infrastructure components such as virtual machines, networks, and databases using Terraform’s configuration language, HashiCorp Configuration Language (HCL).

  4. Immutable Infrastructure: Terraform promotes the concept of immutable infrastructure, where changes are made by creating new resources rather than modifying existing ones. This ensures reproducibility and reduces the risk of configuration drift.

  5. Dependency Management: Terraform automatically handles resource dependencies, ensuring that resources are provisioned in the correct order to avoid issues and maintain consistency.

  6. State Management: Terraform maintains a state file that keeps track of the current infrastructure state. This enables Terraform to identify changes between the desired and actual states, making updates more efficient.

What is Infrastructure as Code (IaC)

Infrastructure as Code (IaC) is a method of managing and provisioning IT infrastructure using code, rather than manual configuration. It allows teams to automate the setup and management of their infrastructure, making it more efficient and consistent. This is particularly useful in the DevOps environment, where teams are constantly updating and deploying software.

What is a Resource?

In the context of Terraform, a resource represents a single infrastructure component or object that can be managed, such as a virtual machine, network, database, or storage bucket. Resources are defined in Terraform configurations using a specific syntax and are created, updated, or destroyed based on the desired state defined in the configuration.

What is a Provider?

A Provider in Terraform is a plugin that defines and manages resources within a specific infrastructure platform or service, such as AWS, Azure, Google Cloud, or VMware. Providers handle the translation of Terraform configurations into API calls for their respective platforms, allowing Terraform to interact with and manage resources on those platforms.

What is a State file in Terraform? What’s the importance of it?

The State file in Terraform is a critical component that keeps track of the current state of the managed infrastructure. It records the information about which resources were created, how they are configured, and their current status. The importance of the State file lies in several key aspects:

  • Resource Tracking: It helps Terraform understand which resources exist and what their attributes are.

  • Dependency Management: Terraform uses the State file to determine the order in which resources should be created, updated, or destroyed.

  • Change Detection: It allows Terraform to identify differences between the desired (defined in configurations) and current states, enabling it to make necessary updates.

  • Concurrency and Collaboration: The State file is used to coordinate and manage concurrent changes when multiple users are working on the same infrastructure code.

What is the Desired and Current State?

  • Desired State: The desired state in Terraform refers to the configuration you specify in your Terraform code. It defines how you want your infrastructure to be provisioned and what resources should exist with their specific attributes and relationships.

  • Current State: The current state is the actual state of the infrastructure as recorded in the State file. It reflects the real-world status of resources, including their attributes and relationships, after Terraform has performed actions like resource creation, updates, or deletions. Terraform continuously compares the desired state with the current state to determine if any changes are needed and what actions to take to achieve the desired state.


Thanks for reading ;)