github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/configuration/index.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Configuration Language"
     4  sidebar_current: "docs-config-index"
     5  description: |-
     6    Terraform uses text files to describe infrastructure and to set variables.
     7    These text files are called Terraform _configurations_ and are
     8    written in the Terraform language.
     9  ---
    10  
    11  # Configuration Language
    12  
    13  -> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
    14  earlier, see
    15  [0.11 Configuration Language](../configuration-0-11/index.html).
    16  
    17  Terraform uses its own configuration language, designed to allow concise
    18  descriptions of infrastructure. The Terraform language is declarative,
    19  describing an intended goal rather than the steps to reach that goal.
    20  
    21  ## Resources and Modules
    22  
    23  The main purpose of the Terraform language is declaring [resources](./resources.html).
    24  All other language features exist only to make the definition of resources
    25  more flexible and convenient.
    26  
    27  A group of resources can be gathered into a [module](./modules.html),
    28  which creates a larger unit of configuration. A resource describes a single
    29  infrastructure object, while a module might describe a set of objects and the
    30  necessary relationships between them in order to create a higher-level system.
    31  
    32  A _Terraform configuration_ consists of a _root module_, where evaluation
    33  begins, along with a tree of child modules created when one module calls
    34  another.
    35  
    36  ## Arguments, Blocks, and Expressions
    37  
    38  The syntax of the Terraform language consists of only a few basic elements:
    39  
    40  ```hcl
    41  resource "aws_vpc" "main" {
    42    cidr_block = var.base_cidr_block
    43  }
    44  
    45  <BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
    46    # Block body
    47    <IDENTIFIER> = <EXPRESSION> # Argument
    48  }
    49  ```
    50  
    51  - _Blocks_ are containers for other content and usually represent the
    52    configuration of some kind of object, like a resource. Blocks have a
    53    _block type,_ can have zero or more _labels,_ and have a _body_ that contains
    54    any number of arguments and nested blocks. Most of Terraform's features are
    55    controlled by top-level blocks in a configuration file.
    56  - _Arguments_ assign a value to a name. They appear within blocks.
    57  - _Expressions_ represent a value, either literally or by referencing and
    58    combining other values. They appear as values for arguments, or within other
    59    expressions.
    60  
    61  For full details about Terraform's syntax, see:
    62  
    63  - [Configuration Syntax](./syntax.html)
    64  - [Expressions](./expressions.html)
    65  
    66  ## Code Organization
    67  
    68  The Terraform language uses configuration files that are named with the `.tf`
    69  file extension. There is also [a JSON-based variant of the language](./syntax-json.html)
    70  that is named with the `.tf.json` file extension.
    71  
    72  Configuration files must always use UTF-8 encoding, and by convention are
    73  usually maintained with Unix-style line endings (LF) rather than Windows-style
    74  line endings (CRLF), though both are accepted.
    75  
    76  A _module_ is a collection of `.tf` or `.tf.json` files kept together in a
    77  directory. The root module is built from the configuration files in the
    78  current working directory when Terraform is run, and this module may reference
    79  child modules in other directories, which can in turn reference other modules,
    80  etc.
    81  
    82  The simplest Terraform configuration is a single root module containing only
    83  a single `.tf` file. A configuration can grow gradually as more resources
    84  are added, either by creating new configuration files within the root module
    85  or by organizing sets of resources into child modules.
    86  
    87  ## Configuration Ordering
    88  
    89  Because Terraform's configuration language is declarative, the ordering of
    90  blocks is generally not significant. (The order of `provisioner` blocks within a
    91  resource is the only major feature where block order matters.)
    92  
    93  Terraform automatically processes resources in the correct order based on
    94  relationships defined between them in configuration, and so you can organize
    95  resources into source files in whatever way makes sense for your infrastructure.
    96  
    97  ## Terraform CLI vs. Providers
    98  
    99  The Terraform command line interface (CLI) is a general engine for evaluating
   100  and applying Terraform configurations. It defines the Terraform language syntax
   101  and overall structure, and coordinates sequences of changes that must be made to
   102  make remote infrastructure match the given configuration.
   103  
   104  This general engine has no knowledge about specific types of infrastructure
   105  objects. Instead, Terraform uses plugins called
   106  [providers](./providers.html) that each define and manage a
   107  set of resource types. Most providers are associated with a particular cloud or
   108  on-premises infrastructure service, allowing Terraform to manage infrastructure
   109  objects within that service.
   110  
   111  Terraform doesn't have a concept of platform-independent resource types
   112  — resources are always tied to a provider, since the features of similar
   113  resources can vary greatly from provider to provider. But Terraform CLI's shared
   114  configuration engine ensures that the same language constructs and syntax are
   115  available across all services and allows resource types from different services
   116  to be combined as needed.
   117  
   118  ## Example
   119  
   120  The following simple example describes a simple network topology for Amazon Web
   121  Services, just to give a sense of the overall structure and syntax of the
   122  Terraform language. Similar configurations can be created for other virtual
   123  network services, using resource types defined by other providers, and a
   124  practical network configuration will often contain additional elements not
   125  shown here.
   126  
   127  ```hcl
   128  variable "aws_region" {}
   129  
   130  variable "base_cidr_block" {
   131    description = "A /16 CIDR range definition, such as 10.1.0.0/16, that the VPC will use"
   132    default = "10.1.0.0/16"
   133  }
   134  
   135  variable "availability_zones" {
   136    description = "A list of availability zones in which to create subnets"
   137    type = list(string)
   138  }
   139  
   140  provider "aws" {
   141    region = var.aws_region
   142  }
   143  
   144  resource "aws_vpc" "main" {
   145    # Referencing the base_cidr_block variable allows the network address
   146    # to be changed without modifying the configuration.
   147    cidr_block = var.base_cidr_block
   148  }
   149  
   150  resource "aws_subnet" "az" {
   151    # Create one subnet for each given availability zone.
   152    count = length(var.availability_zones)
   153  
   154    # For each subnet, use one of the specified availability zones.
   155    availability_zone = var.availability_zones[count.index]
   156  
   157    # By referencing the aws_vpc.main object, Terraform knows that the subnet
   158    # must be created only after the VPC is created.
   159    vpc_id = aws_vpc.main.id
   160  
   161    # Built-in functions and operators can be used for simple transformations of
   162    # values, such as computing a subnet address. Here we create a /20 prefix for
   163    # each subnet, using consecutive addresses for each availability zone,
   164    # such as 10.1.16.0/20 .
   165    cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 4, count.index+1)
   166  }
   167  ```
   168  
   169  For more information on the configuration elements shown here, use the
   170  site navigation to explore the Terraform language documentation sub-sections.
   171  To start, see [_Resource Configuration_](./resources.html).