Skip to main content

1. About terraform

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp that allows you to define and provision data center infrastructure using a declarative configuration language.

Key Concepts​

  1. Configuration Files: Terraform uses configuration files, typically with a .tf extension, written in HashiCorp Configuration Language (HCL) or JSON. These files define the desired state of your infrastructure.
  2. Providers: Providers are plugins that enable Terraform to interact with various cloud platforms (like AWS, Azure, Google Cloud) and other services. Providers are responsible for understanding API interactions and translating configuration into API requests.
  3. Resources: Resources represent the components of your infrastructure, such as virtual machines, databases, and networks. Each resource is defined with specific parameters to control its properties.
  4. Modules: Modules are reusable configurations that can be shared and versioned. They allow you to encapsulate and manage complex sets of resources.
  5. State: Terraform maintains a state file (terraform.tfstate) to keep track of the resources it manages. This file is used to map your configuration to real-world infrastructure and determine changes that need to be applied.

Workflow​

  1. Write Configuration: Define your infrastructure in .tf files using HCL. This includes specifying providers, resources, and variables.
  2. Initialize: Run terraform init to initialize the working directory. This downloads provider plugins and sets up the backend.
  3. Plan: Execute terraform plan to preview changes that Terraform will make to achieve the desired state. This step helps identify potential issues before applying changes.
  4. Apply: Use terraform apply to apply the changes required to reach the desired state. Terraform will create, update, or delete resources as needed.
  5. Destroy: To remove all resources defined in your configuration, use terraform destroy. This will clean up the infrastructure managed by Terraform.
  6. Manage State: Regularly review and manage the state file, especially if working in a team. Consider using remote state storage to collaborate effectively.