Language Introduction

The Terraform Configuration Language (abbreviated as HCL) is an important credential for describing and operating infrastructure managed through Terraform.

Through configuration files written in this language, you can tell Terraform:

  • Which plugins to install

  • Which infrastructure to create

  • Which data to fetch

The Terraform Configuration Language also allows you to define dependencies between resources and manage multiple similar resources using a single configuration block. It features simple configuration, strong readability, and compatibility with JSON syntax.

About the Terraform Language

The main purpose of the Terraform language is to declare resources, which represent infrastructure objects. All other language features exist only to make resource definitions more flexible and convenient.

A Terraform configuration is a complete document in the Terraform language that tells Terraform how to manage a given collection of infrastructure. A configuration can consist of multiple files and directories.

Basic Syntax Elements

The syntax of the Terraform language consists of the following basic elements:

<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
  # Block content (including but not limited to meta-arguments, variable definitions)
  <IDENTIFIER> = <EXPRESSION>
}
  1. Blocks: Containers for other content, usually representing the configuration of some kind of object, such as resources. Blocks have a block type, can have zero or more labels, and have a block body that contains any number of arguments and nested blocks. Most of Terraform's features are controlled by top-level blocks in configuration files.

  2. Arguments: Assign values to names. They appear within blocks.

  3. Expressions: Represent a value, either literally or by referencing and combining other values. They appear as values for arguments or within other expressions.

Declarative Nature

The Terraform language is declarative, describing the intended goal rather than the steps to reach that goal. The ordering of blocks and files is generally not significant; they will be executed by Terraform drawing an execution graph and determining the execution order when the Terraform configuration file is executed. When declaring, you only need to consider the implicit and explicit reference relationships between resources.

Configuration Example

The following example describes a simple network topology for Huawei Cloud, showcasing the overall structure and syntax of the Terraform language. Similar configurations can be applied to resource types provided by other services. Actual network configurations typically contain additional elements not shown here.

Basic Learning

To master the core concepts of the Terraform configuration language, it is recommended to learn the basics in the following order:

  1. Configuration Language Basicsarrow-up-right

    • Files and Directory Structure: Understand how Terraform configuration files are organized, including .tf files, variable files, state files, etc.

    • Syntax Rules: Master the basic rules of HCL syntax, including correct writing of blocks, arguments, and expressions

    • Resources and Data Sources: Understand the difference between resources and data sources, and their roles in configuration

  2. Resources and Data Sourcesarrow-up-right

    • Resource Behavior: Understand resource lifecycle behaviors such as state management, creation, updates, and deletion

    • Dependencies: Master implicit and explicit dependencies between resources, and how Terraform resolves these dependencies

    • Meta-arguments: Learn how to use meta-arguments like count, for_each, depends_on

  3. Variables and Outputsarrow-up-right

    • Variable Definition: Master input variable definitions, type constraints, default value settings, etc.

    • Output Values: Understand the definition and use of output values, and how to reference them in other configurations

    • Local Values: Learn the concept and use cases of local values to improve configuration maintainability

  4. Expressionsarrow-up-right

    • Reference Expressions: Master how to reference resource attributes, variables, data sources, etc.

    • Operators: Learn the use of arithmetic, logical, and comparison operators

    • Conditional Expressions: Understand the syntax and application scenarios of conditional expressions

Advanced Learning

After mastering the basics, you can further learn the following advanced features:

  1. Functionsarrow-up-right

    • Built-in functions

    • Numeric functions

    • String functions

    • Collection functions

  2. Modulesarrow-up-right

    • Module usage

    • Module creation

    • Module composition

Practice Recommendation: Try the Terraform Configuration Writing Tutorialarrow-up-right.

Learn more through the following links:

Last updated