Terraform Config Doc Section

Starting page
https://www.terraform.io/docs/configuration/index.html

CONFIGURATION

You can use both terraform syntax and json ( .tf and .tf.json) but in json you can't use comments and it is more machine readable , instead tf was created for human.

LOAD ORDER AND SEMANTICS

When you run a command all the files in the directory with extensions .tf or .tf.json are loaded in alphabetical order.
The configuration within the loaded files are appended to each other (not merged) two resources with the same name are not merged, and will instead cause a validation error.
The order of variables, resources, etc. defined within the configuration doesn't matter.

CONFIGURATION SYNTAX

The syntax of Terraform configurations is called HashiCorp Configuration Language (HCL).
here 3 basic points

  1. Single line comments start with #
  2. Multi-line comments are wrapped with /* and */
  3. Values are assigned with the syntax of key = value (whitespace doesn't matter).

read the others point in the page https://www.terraform.io/docs/configuration/syntax.html

INTERPOLATION SYNTAX

and extract from the page https://www.terraform.io/docs/configuration/interpolation.html

Available Variables

#simple var usage
${var.foo}

#math interpolation
${count.index + 1}

#escape interpolation with double dollar signs: 
$${foo} 
#will be rendered as a literal ${foo}.

#map reference
${var.amis["us-east-1"]

#To reference attributes of your own resource
${self.private_ip_address}

Built-in Functions

#creates an IP address with the given host number
cidrhost("10.0.0.0/8", 2) 
#returns 10.0.0.2

#Removes empty string elements from a list
compact(list)

#Combines two or more lists into a single list
concat(list1, list2, ...)

OVERRIDES

This is not the default behaviour but can be used in some exception.
To have it the files must have and overrides names. These must be override or end in _override, excluding the extension. Examples of valid override files are override.tf, override.tf.json, temp_override.tf.

it can be used for temp modification or to override default terraform configuration files.

RESOURCE CONFIGURATION

it is interesting the meta-parameters sections and the count example
how to create 3 machines with 3 different ips

variable "instance_ips" {
  default = {
    "0" = "10.11.12.100"
    "1" = "10.11.12.101"
    "2" = "10.11.12.102"
  }
}

resource "aws_instance" "app" {
  count = "3"
  private_ip = "${lookup(var.instance_ips, count.index)}"
  # ...
}

DATA SOURCE CONFIGURATION

not so much clear how to use it, seems a way to save information

PROVIDER CONFIGURATION

Every resource in Terraform is mapped to a provider based on longest-prefix matching. For example the aws_instance resource type would map to the aws provider

You can define multiple instances of the same provider in order to support multiple regions, multiple hosts, etc

To define multiple provider instances, repeat the provider configuration multiple times, but set the alias field and name the provider.

# The default provider
provider "aws" {
    # ...
}

# West coast region
provider "aws" {
    alias = "west"

    region = "us-west-2"
}

After naming a provider, you reference it in resources with the provider field:

resource "aws_instance" "foo" {
    provider = "aws.west"

    # ...
}

VARIABLES

type (optional) - Valid values are string, list, and map. String is the default
examples

String

variable "key" {
  type    = "string"
  default = "value"
}

A map allows a key to contain a lookup table.

variable "images" {
  type = "map"
  default = {
    us-east-1 = "image-1234"
    us-west-2 = "image-4567"
  }
}

A list

variable "users" {
  type    = "list"
  default = ["admin", "ubuntu"]
}
  • you can setup a variable using the environment variables before terraform apply, in the original page you can find some examples for the 3 variables type
  • Variables can be collected in files and passed all at once , If a variable is defined in more than one file passed, the last variable file (reading left to right) will be the definition used.

OUTPUT CONFIGURATION

you can have in output some values

#single value
output "address" {
  value = "${aws_instance.db.public_dns}"
}
#multiples values
output "addresses" {
  value = ["${aws_instance.web.*.public_dns}"]
}

you can specify the parameter sensitive = true so the value are not showed but you can access using the terraform output

MODULE CONFIGURATION

  • The module block configures a module and tells Terraform to build its resource
  • The only required key is source, which tells Terraform where this module can be downloaded from.
module "consul" {
    source = "github.com/hashicorp/consul/terraform/aws"
    servers = 5
}

ATLAS CONFIGURATION

Terraform can be configured to be able to upload to HashiCorp's Atlas using the push command https://www.terraform.io/docs/commands/push.html

ENVIRONMENT VARIABLES

you can set some environment variables before run commands to change the behavior of terraform
you can:

  • change the log level
  • change the log path
  • disable input for variables
  • display information in a different way module_depth
  • set the variable inside the code
  • skip unit test
Salvo diversa indicazione, il contenuto di questa pagina è sotto licenza Creative Commons Attribution-ShareAlike 3.0 License