Hashicorp Configuration Language: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 132: Line 132:
{{External|https://www.terraform.io/docs/configuration/functions.html}}
{{External|https://www.terraform.io/docs/configuration/functions.html}}
{{External|https://www.terraform.io/docs/configuration/expressions.html#function-calls}}
{{External|https://www.terraform.io/docs/configuration/expressions.html#function-calls}}
<syntaxhighlight lang='text'>
...
cidr_block = "${substr("${var.my_cidr_block}", 0, 5)}.1.0/24"
...
</syntaxhighlight>

Revision as of 23:55, 15 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.

References to Named Values or Interpolation Expression

https://www.terraform.io/docs/configuration/expressions.html#references-to-named-values

Resource elements can be used by other resources via interpolation expressions:

resource "aws_subnet" "something" {

  vpc_id = "${aws_vpc.main.id}"
  ...
}

Input Variable

https://www.terraform.io/docs/configuration/variables.html
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
}

Variables can be used via interpolation expression, prefixed with “var.”

To use the variable:

...
Name = "${var.playground_name}-playground-vpc"
...


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

https://learn.hashicorp.com/terraform/getting-started/variables#lists
variable "security_groups" {
  description = "The list of security groups"
  type        = list
  default     = ["sg-a41f9d51704199e97"]
}

Maps

https://learn.hashicorp.com/terraform/getting-started/variables#maps

Output Variable

https://www.terraform.io/docs/configuration/outputs.html
https://learn.hashicorp.com/terraform/getting-started/outputs

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:

terraform output

Functions

https://www.terraform.io/docs/configuration/functions.html
https://www.terraform.io/docs/configuration/expressions.html#function-calls
...
cidr_block = "${substr("${var.my_cidr_block}", 0, 5)}.1.0/24"
...