Hashicorp Configuration Language: Difference between revisions
Jump to navigation
Jump to search
(→Lists) |
|||
(11 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
* [[Terraform_Concepts#Hashicorp_Configuration_Language_.28HCL.29|Terraform Concepts]] | * [[Terraform_Concepts#Hashicorp_Configuration_Language_.28HCL.29|Terraform Concepts]] | ||
=Overview= | =Overview= | ||
=Comments= | |||
# This is a comment | |||
=Block= | =Block= | ||
==Terraform Block== | |||
<syntaxhighlight lang='text'> | |||
terraform { | |||
required_version = ">= 0.12" | |||
} | |||
</syntaxhighlight> | |||
==Resource Block== | ==Resource Block== | ||
Line 11: | Line 23: | ||
==Data Block== | ==Data Block== | ||
Declares a [[Terraform Concepts#Data_Source|data source]]. | |||
data "''data-source-name''" "''local-name''" { | |||
... | |||
} | |||
<syntaxhighlight lang='yaml'> | |||
data "aws_ami" "example" { | |||
most_recent = true | |||
owners = ["self", "0000000"] | |||
filter { | |||
name = "name" | |||
values = ["consul-ubuntu-*"] | |||
} | |||
tags = { | |||
Name = "app-server" | |||
Tested = "true" | |||
} | |||
} | |||
</syntaxhighlight> | |||
==Module Block== | ==Module Block== | ||
Represents a [[Terraform_Concepts#Using_a_Module|module call]]. | |||
=Interpolation Expression= | =Interpolation Expression= | ||
=Input Variable= | =Input Variable= | ||
{{External|https://www.terraform.io/docs/configuration/variables.html}} | |||
<syntaxhighlight lang='text'> | <syntaxhighlight lang='text'> | ||
variable "region" { | variable "region" { | ||
description = "This is the region" | |||
type = string | |||
default = "us-east-1" | default = "us-east-1" | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
"default" may be "null" | |||
<syntaxhighlight lang='text'> | |||
variable "masters" { | |||
description = "The number of master nodes" | |||
type = number | |||
default = 1 | |||
} | |||
</syntaxhighlight> | |||
To use the variable: | To use the variable: | ||
Line 41: | Line 93: | ||
==Lists== | ==Lists== | ||
{{External|https://learn.hashicorp.com/terraform/getting-started/variables#lists}} | {{External|https://learn.hashicorp.com/terraform/getting-started/variables#lists}} | ||
<syntaxhighlight lang='text'> | |||
variable "security_groups" { | |||
description = "The list of security groups" | |||
type = list | |||
default = ["sg-a41f9d51704199e97"] | |||
} | |||
</syntaxhighlight> | |||
==Maps== | ==Maps== | ||
Line 46: | Line 106: | ||
=Output Variable= | =Output Variable= | ||
{{External|https://www.terraform.io/docs/configuration/outputs.html}} | |||
{{External|https://learn.hashicorp.com/terraform/getting-started/outputs}} | {{External|https://learn.hashicorp.com/terraform/getting-started/outputs}} | ||
Revision as of 02:36, 14 November 2019
External
Internal
Overview
Comments
# This is a comment
Block
Terraform Block
terraform {
required_version = ">= 0.12"
}
Resource Block
Declares a resource.
Data Block
Declares a data source.
data "data-source-name" "local-name" { ... }
data "aws_ami" "example" {
most_recent = true
owners = ["self", "0000000"]
filter {
name = "name"
values = ["consul-ubuntu-*"]
}
tags = {
Name = "app-server"
Tested = "true"
}
}
Module Block
Represents a module call.
Interpolation Expression
Input Variable
variable "region" {
description = "This is the region"
type = string
default = "us-east-1"
}
"default" may be "null"
variable "masters" {
description = "The number of master nodes"
type = number
default = 1
}
To use the variable:
region= var.region
This is also an interpolation expression, prefixed with “var.”
Variables can be assigned in multiples way, and this is the descending order of precedence:
- command-line flags (-var = ‘region=something’)
- from a file (.tfvars, -var-file=…) terraform.tfvars or *.auto.tfvars in the current directory are automatically loaded. Multiple -var-file can be used.
- From environment variables: TF_VAR_name. This can only be used for string variables.
- UI Input
- Variable defaults (“default” keyword).
Lists
variable "security_groups" {
description = "The list of security groups"
type = list
default = ["sg-a41f9d51704199e97"]
}
Maps
Output Variable
Output variables are a way to organize data to be easily queried and shown back to the Terraform user. Terraform usually stores hundred or thousands of attribute values, but only a few of those are important. Output variables can be used as an input to other modules.
Also see: