A Beginner’s Introduction to Terraform: Simplify Infrastructure Management

Utsav Desai
8 min readApr 6, 2023

--

What is Terraform?

Terraform is an open-source infrastructure-as-code (IAC) tool developed by HashiCorp that allows you to manage and provision your cloud infrastructure resources in a declarative way. It supports various cloud platforms such as Amazon Web Services (AWS), Microsoft Azure, Google Cloud Platform (GCP), and many others.

Using Terraform, you can define your infrastructure as a set of declarative configuration files written in a human-readable domain-specific language (DSL). These configuration files describe the desired state of your infrastructure resources, such as virtual machines, load balancers, databases, and network configurations.

Terraform can then apply these configuration files to create, modify, and destroy the necessary resources automatically. It also provides features such as dependency management, parallel execution, and a built-in execution plan preview, which allows you to see what changes Terraform will make before actually applying them.

Overall, Terraform enables you to manage your infrastructure in a more efficient and scalable way, and it helps to ensure that your infrastructure remains consistent and predictable over time.

What are the key features of Terraform?

Some of the key features of Terraform are:

  1. Declarative Configuration: Terraform allows you to define your infrastructure as a set of declarative configuration files, which describe the desired state of your infrastructure resources.
  2. Multi-Cloud Support: Terraform supports multiple cloud providers such as AWS, Azure, GCP, and many others. This allows you to manage your infrastructure resources across different clouds using a single tool.
  3. Resource Management: Terraform can create, modify, and delete infrastructure resources in a safe and automated way, which helps to reduce the risk of human error and ensures that your infrastructure is always in the desired state.
  4. Dependency Management: Terraform can manage dependencies between resources and ensure that they are created in the correct order to avoid conflicts and errors.
  5. State Management: Terraform maintains a state file that tracks the current state of your infrastructure resources. This state file can be versioned and shared with others, making it easy to collaborate on infrastructure changes.
  6. Plan Preview: Terraform provides a built-in plan preview feature that allows you to see what changes will be made to your infrastructure before actually applying them. This helps to prevent unintended changes and provides greater confidence when making infrastructure changes.
  7. Community Driven: Terraform is an open-source tool that has a large and active community of contributors. This means that it is constantly evolving and improving based on user feedback and community contributions.

What are the use cases of Terraform?

Terraform is a versatile tool that can be used in a wide range of use cases, some of which include:

  1. Infrastructure Provisioning: Terraform can be used to provision infrastructure resources such as virtual machines, load balancers, databases, and network configurations in a declarative way, making it easier to manage and maintain the infrastructure.
  2. Application Deployment: Terraform can be used to deploy and manage applications on infrastructure resources, providing a consistent and repeatable deployment process.
  3. Disaster Recovery: Terraform can be used to automate the recovery of infrastructure resources in the event of a disaster, such as an outage or data center failure.
  4. Cloud Migration: Terraform can be used to migrate on-premises infrastructure to the cloud or between different cloud providers, simplifying the migration process and reducing the risk of errors.
  5. Compliance and Governance: Terraform can be used to enforce compliance policies and governance requirements by defining infrastructure resources that meet the necessary standards.
  6. Test and Development Environments: Terraform can be used to create and manage test and development environments that are consistent with production environments, reducing the risk of errors and improving the quality of testing.

Overall, Terraform provides a flexible and scalable approach to managing infrastructure resources, making it an ideal tool for a wide range of use cases.

What are the benefits of using modules in Terraform?

Using modules in Terraform provides several benefits, including:

  1. Reusability: Modules allow you to encapsulate infrastructure resources and configurations into reusable components that can be used across different projects and environments. This reduces the amount of code duplication and improves the consistency of your infrastructure.
  2. Modularity: Modules allow you to break down your infrastructure into smaller, more manageable components, making it easier to maintain and modify your infrastructure over time.
  3. Abstraction: Modules provide an abstraction layer that simplifies the complexity of infrastructure resources and configurations, making it easier to understand and manage your infrastructure.
  4. Collaboration: Modules can be shared and reused across different teams and projects, promoting collaboration and sharing of best practices.
  5. Testing: Modules can be tested independently, allowing you to verify their functionality and ensure that they meet your requirements before integrating them into your infrastructure.
  6. Versioning: Modules can be versioned, allowing you to track changes and roll back to previous versions if necessary.

Overall, using modules in Terraform promotes code reuse, modularity, and collaboration, making it easier to manage and maintain your infrastructure resources over time.

Architecture of Terraform

Terraform architecture is based on a client-server model, where the client is the Terraform CLI and the server is the infrastructure provider that manages the resources.

Terraform consists of four main components:

  1. Terraform CLI: This is the command-line interface that the user interacts with to define, manage, and execute Terraform configurations.
  2. Terraform Core: This is the main engine that interprets Terraform configurations, generates execution plans, and manages the state of the infrastructure.
  3. Terraform Providers: These are plugins that interface with the infrastructure providers such as AWS, Azure, GCP, and others. They manage the communication between Terraform and the infrastructure resources and ensure that the desired state is achieved.
  4. Terraform Modules: These are reusable components that define infrastructure resources and configurations. Modules allow users to encapsulate infrastructure resources and configurations into reusable components that can be shared and reused across different projects and environments.

The general workflow of Terraform involves the user defining a configuration file that describes the desired state of the infrastructure. The user then runs the ‘terraform init’ command, which initializes the working directory and downloads the necessary plugins and modules. The user then runs the ‘terraform plan’ command, which generates an execution plan that describes the changes that will be made to the infrastructure. Finally, the user runs the ‘terraform apply’ command, which executes the plan and creates, modifies, or deletes the necessary infrastructure resources.

Overall, Terraform architecture is designed to provide a consistent and predictable way to manage infrastructure resources across different providers, making it easier to manage and maintain infrastructure configurations over time.

Workflow of the core Terraform

The core workflow of Terraform involves the following steps:

  1. Configuration: The user writes a Terraform configuration file that describes the desired state of the infrastructure. This file can include resource definitions, variables, providers, and modules.
  2. Initialization: The user runs the ‘terraform init’ command, which initializes the Terraform working directory and downloads the necessary plugins and modules. This step also creates the state file that tracks the current state of the infrastructure.
  3. Planning: The user runs the ‘terraform plan’ command, which generates an execution plan that describes the changes that will be made to the infrastructure. This plan includes information about which resources will be created, modified, or deleted.
  4. Execution: The user runs the ‘terraform apply’ command, which executes the plan and creates, modifies, or deletes the necessary infrastructure resources. During the execution, Terraform communicates with the infrastructure provider to ensure that the desired state is achieved.
  5. State Management: Terraform maintains a state file that tracks the current state of the infrastructure resources. The state file is stored locally or remotely and can be shared between team members. This step ensures that Terraform can identify the changes that have been made to the infrastructure and maintain the desired state.
  6. Destruction: The user can also run the ‘terraform destroy’ command, which destroys all the infrastructure resources managed by Terraform. This command is useful when the user wants to delete the infrastructure or start over with a clean slate.

Overall, the core workflow of Terraform follows a predictable and consistent process that makes it easier to manage infrastructure resources and ensure that they remain in the desired state over time.

Example Of Terraform

Here is an example of using Terraform to create an AWS EC2 instance:

1) Configuration: Create a file called ‘main.tf’ and add the following code:

provider "aws" {
access_key = "<your-access-key>"
secret_key = "<your-secret-key>"
region = "us-west-2"
}

resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}

This configuration defines an AWS provider with the access key, secret key, and region information, and an EC2 instance resource with the specified AMI and instance type.

2) Initialization: Run the terraform init command in the same directory where the main.tf file is located. This command initializes the Terraform working directory and downloads the necessary plugins and modules.

$ terraform init

3) Validation: Run the terraform validate command to validate the syntax and configuration of the main.tf file.

$ terraform validate

4) Formatting: Run the terraform fmt command to format the main.tf file to comply with Terraform's standard style.

$ terraform fmt

5) Planning: Run the terraform plan command to generate an execution plan that describes the changes that will be made to the infrastructure.

$ terraform plan

6) Execution: Run the terraform apply command to execute the plan and create the necessary infrastructure resources.

$ terraform apply

7) State Management: Terraform maintains a state file that tracks the current state of the infrastructure resources. This file is created in the same directory where the main.tf file is located. You can view the state file by running the terraform state show command.

$ terraform state show

8) Destruction: If you want to destroy the infrastructure resources created by Terraform, run the terraform destroy command.

$ terraform destroy

This example demonstrates how to use various Terraform commands to initialize, validate, format, plan, apply, manage state, and destroy infrastructure resources. The main.tf file defines the infrastructure resources, and each command is used to perform a specific task on the configuration or infrastructure resources.

What’s Next?

We will look into the concepts like Variables, Provisioning, and Multi Resouce In One exercise.

--

--

Utsav Desai
Utsav Desai

Written by Utsav Desai

Utsav Desai is a technology enthusiast with an interest in DevOps, App Development, and Web Development.