Terraform

From XennisWiki
Jump to: navigation, search

Terraform is an infrastructure as code software by HashiCorp. It allows users to define a datacenter infrastructure in a high-level configuration language, from which it can create an execution plan to build the infrastructure such as OpenStack or in a service provider such as AWS, IBM Cloud (formerly Bluemix), Google Cloud Platform, Linode, Microsoft Azure, Oracle Cloud Infrastructure, or VMware vSphere. Infrastructure is defined in a HCL Terraform syntax or JSON format. (Wikipedia)

Installation

Arch Linux

Install the terraform package.

Auto completion

terraform -install-autocomplete

Example

Google Cloud Platform

main.tf

terraform {
  backend "gcs" {
    bucket = "<my-project>-terraform-state"
    #credentials = ".credentials.json"
  }
}

variable "project" {
  description = "Project ID"
  default     = "<my-project>"
}

variable "region" {
  description = "Region"
  default     = "europe-west1"
}

variable "zone" {
  description = "Zone"
  default     = "europe-west1-c"
}

provider "google" {
  #credentials = "${file(".credentials.json")}"
  project = var.project
  region  = var.region
}

resource "google_project_services" "gps_main" {
  project = var.project

  services = [
    "cloudfunctions.googleapis.com",
    "cloudresourcemanager.googleapis.com",
    "logging.googleapis.com",
    "iam.googleapis.com",
    "iamcredentials.googleapis.com",
    "pubsub.googleapis.com",
    "serviceusage.googleapis.com",
    "storage-api.googleapis.com",
    "storage-component.googleapis.com",
    "vision.googleapis.com",
  ]
}

Initialize Terraform

export GOOGLE_APPLICATION_CREDENTIALS=path/to/credentials.json
terraform init

Apply the changes

terraform apply

Tips

Format and validate files

terraform fmt
terraform validate

External links