Terraform

Terraform

What is terraform

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It is used for building, changing, and versioning infrastructure efficiently. Terraform allows you to define your infrastructure as code in a declarative configuration language, and it automates the process of provisioning and managing cloud resources, on-premises infrastructure, or any other supported infrastructure provider.

  1. Infrastructure as Code (IaC): Terraform enables you to define your infrastructure in a code-like configuration file. This approach provides benefits such as version control, collaboration, and the ability to automate infrastructure provisioning.

  2. Providers: Terraform supports a wide range of infrastructure providers, including AWS, Azure, Google Cloud Platform (GCP), and many others. Each provider has its own set of resources and data sources that can be managed using Terraform.

    Provider Basics

    After writing the code and before applying, we first execute terraform init command. It will analyze the Provider used in the code and try to download the Provider plug-in locally.

    If we look at the folder where we have executed the terraform init command, we will find that there is a .terraform folder:

     terraform {
       required_providers {
         mycloud = {
           source  = "mycorp/mycloud"
           version = "~> 1.0"
           configuration_aliases = [ mycloud.alternate ]
         }
       }
     }
    
  3. Resources: Resources in Terraform represent the infrastructure components you want to create or manage, such as virtual machines, networks, databases, and more. You define these resources in your Terraform configuration files.

    Resource Syntax

    Resources are defined through resource blocks, and I will first explain the scenario of defining a single resource object through resource blocks.

     resource "aws_instance" "web_server" {
       ami           = "ami-a1b2c3d4"
       instance_type = "t2.micro"
     }
    

    In the above example, immediately following the resource keyword is the resource type, which in the above example is aws_instance. The following is the Local Name of the resource, which is in the example web_server.

    The Local Name can be used to reference the resource in the code in the same module, but the combination of type and Local Name must be unique in the current module, and the Local Name of two resources of different types can be the same.

    The content in the curly braces is the block, and the values ​​of the various parameters used to create the resource are defined in the block. In the example we define the image i'd used by the EC2 server and its size.

  4. Variables: Terraform allows you to define variables to parameterize your configurations. Variables make it easier to reuse configurations across environments or make your code more flexible.

    Declaring an Input Variable

    Each input variable accepted by a module must be declared using a variable block:

     variable "image_id" {
       type = string
     }
    
     variable "availability_zone_names" {
       type    = list(string)
       default = ["us-west-1a"]
     }
    
     variable "docker_ports" {
       type = list(object({
         internal = number
         external = number
         protocol = string
       }))
       default = [
         {
           internal = 8300
           external = 8300
           protocol = "tcp"
         }
       ]
     }
    

    The label after the variable keyword is a name for the variable, which must be unique among all variables in the same module. This name is used to assign a value to the variable from outside and to reference the variable's value from within the module.

  5. Outputs: Outputs in Terraform allow you to expose certain values from your infrastructure so that they can be used by other parts of your system or by other Terraform configurations.

    Declaring an Output Value

    Each output value exported by a module must be declared using an output block:

     output "instance_ip_addr" {
       value = aws_instance.server.private_ip
     }
    

    The label immediately after the output keyword is the name, which must be a valid identifier. In a root module, this name is displayed to the user; in a child module, it can be used to access the output's value.

    The value argument takes an expression whose result is to be returned to the user. In this example, the expression refers to the private_ip attribute exposed by an aws_instance resource defined elsewhere in this module (not shown). Any valid expression is allowed as an output value.

  6. State: Terraform maintains a state file that keeps track of the current state of your infrastructure. This state file is essential for Terraform to understand the differences between your desired state (as defined in your configuration) and the actual state of your infrastructure.

  7. Modules: Modules in Terraform are reusable units of code that encapsulate a set of related resources and configurations. They help you organize and share Terraform code.

  8. Terraform CLI: Terraform provides a command-line interface (CLI) for initializing, planning, applying, and destroying infrastructure. The terraform command is used to interact with Terraform configurations.

  9. Terraform Workflow: The typical Terraform workflow involves the following steps:

    • Initialize: Use terraform init to initialize a working directory with the necessary providers and modules.

    • Plan: Use terraform plan to create an execution plan that shows what Terraform will do before actually applying changes.

    • Apply: Use terraform apply to create or update resources according to the configuration.

    • Destroy: Use terraform destroy to destroy resources created by Terraform.

  10. Terraform Cloud: Terraform Cloud is a service offered by HashiCorp that provides collaboration and automation features for Terraform projects. It allows for remote state management, version control integration, and team collaboration.

  11. Terraform Ecosystem: Terraform has a large and active ecosystem, with a variety of community-contributed modules and providers that extend its capabilities. This ecosystem helps users manage infrastructure across different cloud providers and technologies.