Terraform manages infrastructure through a four-phase workflow — writing templates, initialization, preview, and execution — using two core concepts: Terraform configurations and HashiCorp Configuration Language (HCL).
The Terraform workflow consists of four phases: writing templates, initialization, preview, and execution. The writing phase centers on two concepts: Terraform configurations and HCL.
Terraform directory
The writing phase starts with creating a directory for your Terraform configuration. A Terraform configuration is a complete set of .tf files that instructs Terraform how to manage a specific collection of infrastructure. A configuration directory can contain multiple files and subdirectories, and is composed of a root module and optional child modules.
-
Root module
The root module is the working directory where you run Terraform commands. Terraform reads all
.tffiles in this directory to create an execution plan. While you can define all resources, input variables, output values, and providers in a single file, best practice is to split them into separate files by logical function. -
Child modules
Child modules are optional. Each child module encapsulates a reusable feature or simplifies complex logic in the root module. The root module references child modules to reduce complexity and improve reusability and readability.
// Root module
-- main.tf
--providers.tf
-- variables.tf
-- outputs.tf
// Child modules
-- -- -- servers/
-- -- -- --main.tf
-- -- -- --variables.tf
-- -- -- --outputs.tf
Each module typically contains the following configuration files:
-
Resource configuration file (main.tf)
Declares the infrastructure resources to manage and their desired state. Resource-specific names based on type, product, or scenario also work — for example, ecs.tf, network.tf, or database.tf.
-
Provider configuration file (provider.tf)
Declares the name, version, and authentication method for the providers used in the current module, such as aliyun/alicloud or hashicorp/random.
-
Input variable configuration file (variables.tf)
Declares the input variables used in the current module. Separating variables makes configurations more flexible — pass different values when running Terraform commands without modifying the core configuration.
-
Output value configuration file (outputs.tf)
Declares the values the module exports after resources are created, such as instance IDs or IP addresses.
HCL language
HCL (HashiCorp Configuration Language) is used to create and manage API-based resources, which are primarily cloud resources. It is the language Terraform uses to define, create, and manage cloud resources and their dependencies in your Alibaba Cloud environment. A resource is an infrastructure object — a physical entity such as a virtual machine, disk, container, or network, or a logical unit such as a Resource Access Management (RAM) rule or a security group rule.
HCL is a configuration language, not a programming language. It is a JSON variant designed to be readable by both humans and machines. Instead of control loops and statements, HCL uses a small set of primitives — variables, resources, outputs, and modules — combined with assignments, count, and built-in functions to express resource definitions.
HCL syntax
HCL syntax consists of five elements: blocks, arguments, identifiers, expressions, and comments.
The following is a real-world example, followed by the general syntax template:
resource "alicloud_vpc" "myvpc" {
vpc_name = "the-first-vpc"
cidr_block = "172.16.0.0/12"
}
<Block Type> "<Block Label>" "<Block Label>" {
# Block Body
<Identifier> = <Value/Expression> # Argument
}
-
Block
A block groups related configuration into a named container. Each block has a type — such as
resource,variable,output, ormodule. Complex blocks can nest other block types inside them. -
Argument
An argument assigns a value to a name inside a block. Blocks can have both required and optional arguments.
Identifier — The name of an argument, block type, or other Terraform-specific construct. Identifiers can contain letters, underscores, hyphens, and digits, but must not start with a digit.
-
Expression
An expression represents the value assigned to an identifier. It can be a simple literal — a string or number — or a complex type such as an object, array, or map.
-
Comment
Use
#or//for single-line comments. Use/*and*/for multi-line comments.
HCL is declarative: blocks define the desired end state of infrastructure, so the order of blocks or files has no effect on the outcome. This lets Terraform manage resource creation and dependencies without requiring you to script individual steps.
Good Terraform configurations combine proper file organization, modular design for code reuse, and parameterization for flexibility. As configurations grow, state management and multi-environment patterns become equally important.