Terraform resources

From wikinotes

Resources are the composable building blocks of your terraform configuration.

Documentation

overview https://www.terraform.io/language/resources
provisioners https://www.terraform.io/language/resources/provisioners/syntax

Meta Arguments

Meta Arguments are available on all resource types, and allow you to customize how they are built.

provider    # google, aws, ...
depends_on  # build-order
count
for_each
lifecycle

Provisioners

Basics

You can use provisioners to hand off the additional information to another configuration management tool.

resource "aws_instance" "web" {
  # ...

  provisioner "local-exec" {
    command = "echo The server's IP address is ${self.private_ip}"
  }
}

Connections

Provisioner communication-style can be configured with a connection block.

provisioner "file" {
  source      = "conf/myapp.conf"
  destination = "/etc/myapp.conf"

  connection {
    type     = "ssh"
    user     = "root"
    password = "${var.root_password}"
    host     = "${var.host}"
  }
}

Builtin Provisioners

There are three types of provisioners:

file https://www.terraform.io/language/resources/provisioners/file
local-exec https://www.terraform.io/language/resources/provisioners/local-exec
remote-exec https://www.terraform.io/language/resources/provisioners/remote-exec

External Provisioners

You can also require external providers.
They are configured within the toplevel terraform block.

terraform {
  required_providers {
    mycloud = {
      source  = "mycorp/mycloud"
      version = "~> 1.0"
    }
  }
}

Version request syntax

"~> 1.0.4"  # I believe this is like ruby, putting upper bound on digit above last defined
">= 1.0"