Practical Guide to Terraform

Practical Guide to Terraform

What is Terraform?

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It allows you to define and provision infrastructure resources using declarative configuration files. With Terraform, you can manage infrastructure across various cloud providers and on-premises environments in a consistent and predictable manner.

Getting Started with Terraform:

  1. Installation on Windows:

    • Download the Terraform binary for Windows from the official Terraform website.

    • Extract the downloaded zip file to a directory.

    • Add the directory containing the terraform.exe binary to your system's PATH environment variable.

  2. Installation on Linux (Ubuntu):

    • You can install Terraform on Ubuntu using apt package manager.

    • Open a terminal window and run the following commands:

        sudo apt update
        sudo apt install terraform
      
    • Verify the installation by running terraform --version.

Basic Terraform Commands:

  • terraform init: Initializes a Terraform configuration in the current directory, downloading any necessary plugins.

  • terraform plan: Creates an execution plan, showing the proposed changes to infrastructure.

  • terraform apply: Applies the changes defined in the Terraform configuration to provision or modify infrastructure.

  • terraform destroy: Destroys the infrastructure defined in the Terraform configuration, removing all associated resources.

Core Concepts on Terraform

  1. Variables: In Terraform, variables encompass input and output values organized as key-value pairs. Input variables allow customization of deployments by providing parameters during runtime, while output variables are the return values of Terraform modules that can be utilized by other configurations. Explore more about Terraform Variables in our blog.

  2. Provider: Terraform facilitates infrastructure provisioning on major cloud platforms like AWS, Azure, and OCI, among others. A provider acts as a plugin interfacing with the diverse APIs necessary for creating, updating, and deleting various resources. Delve deeper into Terraform Providers by reading our blog.

  3. Module: A module comprises any collection of Terraform configuration files within a folder. Each Terraform configuration inherently contains at least one module, referred to as its root module.

  4. State: Terraform maintains a record of created infrastructure within a state file. This file enables Terraform to identify previously created resources, manage them, and update them as needed.

  5. Resources: Cloud Providers offer a multitude of services, which are represented as resources within Terraform. These resources encompass a wide range of components, from compute instances and virtual networks to higher-level entities like DNS records. Each resource possesses its unique attributes defining its characteristics.

  6. Data Source: Serving a read-only function, data sources allow Terraform to fetch or compute data from resources or entities not explicitly managed within the current Terraform configuration.

  7. Plan: This stage in the Terraform lifecycle involves determining the necessary actions to transition from the real or current state of the infrastructure to the desired state. It outlines what needs to be created, updated, or destroyed.

  8. Apply: As a crucial stage in the Terraform lifecycle, the apply phase implements the changes outlined in the planning stage, effectively transitioning the real or current state of the infrastructure to match the desired state.

Terraform Configuration Files

Terraform configuration files, typically named *.tf, are written in HashiCorp Configuration Language (HCL) or JSON format. These files define the infrastructure resources and configurations that Terraform manages. Here's an overview of Terraform configuration files:

  1. Main Configuration File:

    • The main configuration file, often named main.tf, serves as the primary entry point for your Terraform project.

    • It contains the core infrastructure definitions and configuration settings.

    • In this file, you define providers, resources, variables, outputs, and other essential components.

  2. Provider Configuration:

    • Provider configuration files, named <provider_name>.tf, specify the settings for a particular cloud provider or service.

    • These files declare the provider plugin and configure authentication credentials, region settings, and other provider-specific configurations.

  3. Variable Files:

    • Variable files, such as variables.tf or <name>.tfvars, define input variables used in the Terraform configuration.

    • Input variables allow you to customize configurations without modifying the main configuration file.

    • They can be used for dynamic values, sensitive information, or reusable parameters.

  4. Output Files:

    • Output files, typically named outputs.tf, specify output values that Terraform will display after applying the configuration.

    • Output values can include resource attributes, computed values, or any other information you want to expose from the infrastructure.

  5. Module Configuration:

    • Module configuration files, named <module_name>.tf, define reusable modules that encapsulate related sets of resources and configurations.

    • Modules promote code reuse, maintainability, and modularity in Terraform projects.

    • They allow you to abstract and share common infrastructure patterns across multiple deployments.

  6. State File:

    • The Terraform state file, named terraform.tfstate by default, stores the state of your managed infrastructure.

    • It keeps track of resource metadata, dependencies, and mappings between Terraform resources and real-world infrastructure objects.

    • The state file is critical for Terraform to manage and update infrastructure in subsequent runs.

  7. Backend Configuration:

    • Backend configuration files, such as backend.tf, specify backend settings for storing and accessing the Terraform state file.

    • Backends define where the state file is stored, such as local files, remote storage services (e.g., AWS S3, Azure Blob Storage), or Terraform Cloud.

Project: Deploy S3 bucket using Terraform:

  1. Create a new directory for your Terraform project.

  2. Inside the directory, create a file named main.tfand provider.tf

  3. Add the following code to define an S3 bucket- Source Code

  4. Run terraform init to initialize the project.

  5. Run terraform plan to review the execution plan.

  6. If the plan looks good, run terraform apply to create the S3 bucket.

Deploy a Virtual Machine in Azure:

  1. Create a new directory for your Terraform project.

  2. Inside the directory, create a file named main.tf.

  3. Add the following code to main.tf to define an Azure VM: Source Code

  4. Run terraform init to initialize the project.

  5. Run terraform plan to review the execution plan.

  6. If the plan looks good, run terraform apply to deploy the VM in Azure.

Once you are comfortable with these simple projects, let's dive into something big. Let's Use Terraform to Deploy deploy more project-based infrastructures

Project 1 -

Article - Deploying-3-tier-application-to-azure-app-service-via-terraform

Source Code: Azure App Service + MySQL using Terraform

Project 2-

Article : Deploy-azure-ubuntu-vm-with-nginx-webserver-using-terraform

Source Code : Terraform-Setup-Ubuntu20.04-and-Nginx-for-ReactApp/blob/master/README.md

Like and share your thoughts. Did you find this helpful?