github.com/hugorut/terraform@v1.1.3/website/docs/language/index.mdx (about)

     1  ---
     2  page_title: Overview - Configuration Language
     3  description: >-
     4    You can use the Terraform language to write configuration files that tell
     5    Terraform how to manage a collection of infrastructure.
     6  ---
     7  
     8  # Terraform Language Documentation
     9  
    10  This is the documentation for Terraform's configuration language. It is relevant
    11  to users of [Terraform CLI](/cli),
    12  [Terraform Cloud](/cloud), and
    13  [Terraform Enterprise](/enterprise). Terraform's language is
    14  its primary user interface. Configuration files you write in Terraform
    15  language tell Terraform what plugins to install, what infrastructure to create,
    16  and what data to fetch. Terraform language also lets you define dependencies
    17  between resources and create multiple similar resources from a single
    18  configuration block.
    19  
    20  > **Hands-on:** Try the [Write Terraform Configuration](https://learn.hashicorp.com/collections/terraform/configuration-language) tutorials on HashiCorp Learn.
    21  
    22  ## About the Terraform Language
    23  
    24  The main purpose of the Terraform language is declaring
    25  [resources](/language/resources), which represent infrastructure objects. All other
    26  language features exist only to make the definition of resources more flexible
    27  and convenient.
    28  
    29  A _Terraform configuration_ is a complete document in the Terraform language
    30  that tells Terraform how to manage a given collection of infrastructure. A
    31  configuration can consist of multiple files and directories.
    32  
    33  The syntax of the Terraform language consists of only a few basic elements:
    34  
    35  ```hcl
    36  resource "aws_vpc" "main" {
    37    cidr_block = var.base_cidr_block
    38  }
    39  
    40  <BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
    41    # Block body
    42    <IDENTIFIER> = <EXPRESSION> # Argument
    43  }
    44  ```
    45  
    46  - _Blocks_ are containers for other content and usually represent the
    47    configuration of some kind of object, like a resource. Blocks have a
    48    _block type,_ can have zero or more _labels,_ and have a _body_ that contains
    49    any number of arguments and nested blocks. Most of Terraform's features are
    50    controlled by top-level blocks in a configuration file.
    51  - _Arguments_ assign a value to a name. They appear within blocks.
    52  - _Expressions_ represent a value, either literally or by referencing and
    53    combining other values. They appear as values for arguments, or within other
    54    expressions.
    55  
    56  The Terraform language is declarative, describing an intended goal rather than
    57  the steps to reach that goal. The ordering of blocks and the files they are
    58  organized into are generally not significant; Terraform only considers implicit
    59  and explicit relationships between resources when determining an order of
    60  operations.
    61  
    62  ### Example
    63  
    64  The following example describes a simple network topology for Amazon Web
    65  Services, just to give a sense of the overall structure and syntax of the
    66  Terraform language. Similar configurations can be created for other virtual
    67  network services, using resource types defined by other providers, and a
    68  practical network configuration will often contain additional elements not
    69  shown here.
    70  
    71  ```hcl
    72  terraform {
    73    required_providers {
    74      aws = {
    75        source  = "hashicorp/aws"
    76        version = "~> 1.0.4"
    77      }
    78    }
    79  }
    80  
    81  variable "aws_region" {}
    82  
    83  variable "base_cidr_block" {
    84    description = "A /16 CIDR range definition, such as 10.1.0.0/16, that the VPC will use"
    85    default = "10.1.0.0/16"
    86  }
    87  
    88  variable "availability_zones" {
    89    description = "A list of availability zones in which to create subnets"
    90    type = list(string)
    91  }
    92  
    93  provider "aws" {
    94    region = var.aws_region
    95  }
    96  
    97  resource "aws_vpc" "main" {
    98    # Referencing the base_cidr_block variable allows the network address
    99    # to be changed without modifying the configuration.
   100    cidr_block = var.base_cidr_block
   101  }
   102  
   103  resource "aws_subnet" "az" {
   104    # Create one subnet for each given availability zone.
   105    count = length(var.availability_zones)
   106  
   107    # For each subnet, use one of the specified availability zones.
   108    availability_zone = var.availability_zones[count.index]
   109  
   110    # By referencing the aws_vpc.main object, Terraform knows that the subnet
   111    # must be created only after the VPC is created.
   112    vpc_id = aws_vpc.main.id
   113  
   114    # Built-in functions and operators can be used for simple transformations of
   115    # values, such as computing a subnet address. Here we create a /20 prefix for
   116    # each subnet, using consecutive addresses for each availability zone,
   117    # such as 10.1.16.0/20 .
   118    cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 4, count.index+1)
   119  }
   120  ```