Sobes.tech
Junior

What is Terraform?

sobes.tech AI

Answer from AI

Terraform is an open-source tool created by HashiCorp for managing infrastructure as code (IaC). It allows describing infrastructure using a declarative language HCL (HashiCorp Configuration Language) or JSON, and then automates the deployment, modification, and removal of it.

Key features of Terraform:

  • Declarative: You describe the desired state of the infrastructure, and Terraform determines the actions needed to achieve it.
  • Immutable: Terraform prefers to create new resources instead of modifying existing ones during significant configuration changes, which increases reliability and predictability.
  • Providers: Support for a large number of cloud service providers (AWS, Azure, GCP), as well as other services (Kubernetes, Docker, databases) through a provider system.
  • State: Terraform tracks the state of the deployed infrastructure in a state file, allowing it to understand the current state and calculate necessary changes.
  • Modules: Ability to create reusable configuration blocks for organizing code and simplifying management of complex infrastructures.
  • Planning: Before applying changes, Terraform shows a plan of actions to be performed, helping to avoid unexpected consequences.

The lifecycle of working with Terraform usually includes the following stages:

  1. Initialization (terraform init): Loading necessary providers and modules.
    terraform init
    // Initialize the working directory
    
  2. Planning (terraform plan): Generating a plan that shows which resources will be created, changed, or destroyed.
    terraform plan
    // Create and display the execution plan
    
  3. Applying (terraform apply): Executing the plan to create or modify infrastructure.
    terraform apply
    // Apply the plan to the target infrastructure
    
  4. Destroying (terraform destroy): Removing all resources managed by the current Terraform configuration.
    terraform destroy
    // Remove all resources managed by the current Terraform state
    

An example of a simple resource description in HCL:

resource "aws_instance" "example" {
  # Resource type (AWS instance) and its name in the configuration
  ami           = "ami-0abcdef1234567890" # AMI ID
  instance_type = "t2.micro"            # Instance type
  tags = {
    # Tags for the resource
    Name = "example-instance"
  }
}

Terraform significantly simplifies and automates infrastructure management, making it more predictable, scalable, and version-controlled.