Skip to content

Key Components of Terraform

image 3
Imagine the lego components, which are a good metaphor for the Terraform components.

They key components of Terraform are providers, resources, variables (inputs and outputs), modules, statefiles, backend and data sources.


Providers are plugins that define and interact with specific infrastructure platforms or services, such as AWS, Azure, Google Cloud, or others.

  • Each provider offers a set of resources that can be managed by Terraform. For example, the Google Cloud provider allows you to create and manage e2-micro compute instances, Cloud storage buckets, and other GCP resources.


Resources are the building blocks of your infrastructure and are defined within Terraform configurations. Each resource corresponds to a specific object or service such as such as VM, storage, load balancer, NAT gateway, and so forth, provided by a particular infrastructure provider.


In Terraform, variables are used to parameterize your infrastructure configuration. They allow you to define values that can be easily reused across your Terraform code, making it more flexible and maintainable.

There are two types of variables in Terraform:

Input Variables:

  • Input variables are defined in a Terraform configuration to serve as parameters for a module.
  • Input variables allow you to customize the behavior of a module without modifying its source code.
  • Varibles are typically defined in a separate file (e.g., and can have default values.
variable "instance_count" {
  description = "Number of instances to launch"
  type        = number
  default     = 5

Variable blocks have three optional arguments.

  • Description: A short description to document the purpose of the variable.
  • Type: The type of data contained in the variable.
  • Default: The default value.

Output Variables:

Output variables used to expose certain values from a module that can be used by other parts of your Terraform code. You can share information between different parts of your infrastructure configuration.

output "public_ip" {
  description = "The public IP address of the instance"
  value       = aws_instance.example.public_ip

While the description argument is optional, you should include it in all output declarations to document the intent and content of the output. Once you define variables, you can reference them throughout your Terraform configuration using interpolation syntax, like ${var.instance_count}.


Modules are containers for multiple resources that are used together. A module consists of a collection of .tf and/or .tf.json files kept together in a directory. Modules can be published and shared, promoting best practices and code reuse.

Root module and child modules

Every workspace has a root module ; it’s the directory where you run terraform apply.

Under the root module, you may have one or more child modules to help you organize and reuse configuration. Modules can be sourced either locally (meaning they are embedded within the root module) or remotely (meaning they are downloaded from a remote location as part of terraform init)

detailed module structure
detailed module structure

Data Source

Terraform data sources let you dynamically fetch data from APIs or other Terraform state backends. Examples of data sources include machine image IDs from a cloud provider or Terraform outputs from other configurations.

Data sources make your configuration more flexible and dynamic and let you reference values from other configurations, helping you scope your configuration while still referencing any dependent resource attributes. In Terraform Cloud, workspaces let you share data between workspaces.


When you run Terraform commands, Terraform stores its current version in your project’s state file, along with the state file version format. You can check the state file to track and check current state of your infrastructure.

Terraform will only update the state file version when a new version of Terraform requires a change to the state file’s format. Terraform will update the terraform_version whenever you apply a change to your configuration using a newer Terraform version.

grep -e '"version"' -e '"terraform_version"' terraform.tfstate
  "version": 4,
  "terraform_version": "0.15.0",


The backend is responsible for storing the Terraform state file. Various backends, such as local, remote, or version-controlled backends, can be configured based on your requirements.