2014-07-28 19:43:00 +02:00
---
layout: "docs"
2018-12-20 05:34:34 +01:00
page_title: "Configuration Language"
2018-05-07 03:32:16 +02:00
sidebar_current: "docs-config-index"
2014-10-22 05:21:56 +02:00
description: |-
2018-05-05 22:51:35 +02:00
Terraform uses text files to describe infrastructure and to set variables.
These text files are called Terraform _configurations_ and are
written in the Terraform language.
2014-07-28 19:43:00 +02:00
---
2019-01-17 01:31:01 +01:00
# Configuration Language
2014-07-28 19:43:00 +02:00
2019-01-17 01:30:43 +01:00
-> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
earlier, see
[0.11 Configuration Language ](../configuration-0-11/index.html ).
2020-10-02 20:02:59 +02:00
> **Hands-on:** Try the [Terraform: Get Started](https://learn.hashicorp.com/collections/terraform/aws-get-started?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) collection on HashiCorp Learn.
2020-07-31 22:16:35 +02:00
2018-05-05 22:51:35 +02:00
Terraform uses its own configuration language, designed to allow concise
descriptions of infrastructure. The Terraform language is declarative,
describing an intended goal rather than the steps to reach that goal.
## Resources and Modules
2018-12-11 01:14:33 +01:00
The main purpose of the Terraform language is declaring [resources ](./resources.html ).
2018-05-05 22:51:35 +02:00
All other language features exist only to make the definition of resources
more flexible and convenient.
2018-12-11 01:14:33 +01:00
A group of resources can be gathered into a [module ](./modules.html ),
2018-05-05 22:51:35 +02:00
which creates a larger unit of configuration. A resource describes a single
infrastructure object, while a module might describe a set of objects and the
necessary relationships between them in order to create a higher-level system.
A _Terraform configuration_ consists of a _root module_ , where evaluation
2018-12-11 01:14:33 +01:00
begins, along with a tree of child modules created when one module calls
2018-05-05 22:51:35 +02:00
another.
2018-12-11 01:14:33 +01:00
## Arguments, Blocks, and Expressions
The syntax of the Terraform language consists of only a few basic elements:
```hcl
resource "aws_vpc" "main" {
cidr_block = var.base_cidr_block
}
< BLOCK TYPE > "< BLOCK LABEL > " "< BLOCK LABEL > " {
# Block body
< IDENTIFIER > = < EXPRESSION > # Argument
}
```
- _Blocks_ are containers for other content and usually represent the
configuration of some kind of object, like a resource. Blocks have a
_block type,_ can have zero or more _labels,_ and have a _body_ that contains
any number of arguments and nested blocks. Most of Terraform's features are
controlled by top-level blocks in a configuration file.
- _Arguments_ assign a value to a name. They appear within blocks.
- _Expressions_ represent a value, either literally or by referencing and
combining other values. They appear as values for arguments, or within other
expressions.
For full details about Terraform's syntax, see:
- [Configuration Syntax ](./syntax.html )
- [Expressions ](./expressions.html )
2018-05-05 22:51:35 +02:00
## Code Organization
The Terraform language uses configuration files that are named with the `.tf`
2018-12-11 01:14:33 +01:00
file extension. There is also [a JSON-based variant of the language ](./syntax-json.html )
2018-05-05 22:51:35 +02:00
that is named with the `.tf.json` file extension.
Configuration files must always use UTF-8 encoding, and by convention are
usually maintained with Unix-style line endings (LF) rather than Windows-style
line endings (CRLF), though both are accepted.
A _module_ is a collection of `.tf` or `.tf.json` files kept together in a
directory. The root module is built from the configuration files in the
current working directory when Terraform is run, and this module may reference
child modules in other directories, which can in turn reference other modules,
etc.
The simplest Terraform configuration is a single root module containing only
a single `.tf` file. A configuration can grow gradually as more resources
are added, either by creating new configuration files within the root module
or by organizing sets of resources into child modules.
## Configuration Ordering
Because Terraform's configuration language is declarative, the ordering of
2018-12-11 01:14:33 +01:00
blocks is generally not significant. (The order of `provisioner` blocks within a
resource is the only major feature where block order matters.)
2018-05-05 22:51:35 +02:00
Terraform automatically processes resources in the correct order based on
relationships defined between them in configuration, and so you can organize
resources into source files in whatever way makes sense for your infrastructure.
2018-12-11 01:14:33 +01:00
## Terraform CLI vs. Providers
2018-05-05 22:51:35 +02:00
2018-12-11 01:14:33 +01:00
The Terraform command line interface (CLI) is a general engine for evaluating
2019-05-09 01:31:37 +02:00
and applying Terraform configurations. It defines the Terraform language syntax
2018-12-11 01:14:33 +01:00
and overall structure, and coordinates sequences of changes that must be made to
make remote infrastructure match the given configuration.
2018-05-05 22:51:35 +02:00
2018-12-11 01:14:33 +01:00
This general engine has no knowledge about specific types of infrastructure
objects. Instead, Terraform uses plugins called
[providers ](./providers.html ) that each define and manage a
set of resource types. Most providers are associated with a particular cloud or
on-premises infrastructure service, allowing Terraform to manage infrastructure
objects within that service.
2018-05-05 22:51:35 +02:00
2018-12-11 01:14:33 +01:00
Terraform doesn't have a concept of platform-independent resource types
— resources are always tied to a provider, since the features of similar
resources can vary greatly from provider to provider. But Terraform CLI's shared
configuration engine ensures that the same language constructs and syntax are
available across all services and allows resource types from different services
to be combined as needed.
2018-05-05 22:51:35 +02:00
## Example
The following simple example describes a simple network topology for Amazon Web
Services, just to give a sense of the overall structure and syntax of the
Terraform language. Similar configurations can be created for other virtual
network services, using resource types defined by other providers, and a
practical network configuration will often contain additional elements not
shown here.
```hcl
2020-07-31 06:07:36 +02:00
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 1.0.4"
}
}
}
2018-05-05 22:51:35 +02:00
variable "aws_region" {}
variable "base_cidr_block" {
description = "A /16 CIDR range definition, such as 10.1.0.0/16, that the VPC will use"
default = "10.1.0.0/16"
}
variable "availability_zones" {
description = "A list of availability zones in which to create subnets"
2018-12-11 01:14:33 +01:00
type = list(string)
2018-05-05 22:51:35 +02:00
}
provider "aws" {
region = var.aws_region
}
resource "aws_vpc" "main" {
# Referencing the base_cidr_block variable allows the network address
# to be changed without modifying the configuration.
cidr_block = var.base_cidr_block
}
resource "aws_subnet" "az" {
# Create one subnet for each given availability zone.
count = length(var.availability_zones)
# For each subnet, use one of the specified availability zones.
availability_zone = var.availability_zones[count.index]
# By referencing the aws_vpc.main object, Terraform knows that the subnet
# must be created only after the VPC is created.
vpc_id = aws_vpc.main.id
# Built-in functions and operators can be used for simple transformations of
# values, such as computing a subnet address. Here we create a /20 prefix for
# each subnet, using consecutive addresses for each availability zone,
# such as 10.1.16.0/20 .
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 4, count.index+1)
}
```
For more information on the configuration elements shown here, use the
site navigation to explore the Terraform language documentation sub-sections.
2018-05-14 02:01:54 +02:00
To start, see [_Resource Configuration_ ](./resources.html ).