Terraform Basics
A beginner's guide to Terraform and infrastructure as code
TLDR; Terraform is great for managing infrastructure as code.
Terraform in this article — unfortunately has nothing to do with terraforming Mars. Rather, this is something to help you setup servers, networking and almost anything that you do through Oracle Cloud, AWS or Azure.
Terraform is a nifty thing, it is cloud-agnostic, meaning you can use the same code with minor changes and it will still work. Terraform hides or abstracts all the underlying API calls to the Console and only asks that you write some HCL code in .tf files.
Terraform is also Declarative, meaning you just need to tell it what your Infra setup should look like and it figures out a way to get there.
Installing Terraform#
MacOS#
Use homebrew:
brew install terraform
bashLinux (Redhat/CentOS/Oracle Enterprise)#
Run the following on a terminal:
yum install terraform
bashWindows#
— i pity thee.
Terraform Providers#
There are a lot of cloud providers out there, terraform can connect with almost all of them. It can even connect to Helm or Kubernetes.
Providers are plugins, that allow you to interact with the your cloud provider. You would need to define the provider as in a file, preferably provider.tf
provider "google" {
credentials = file("service-account.json")
project = "buggy-code"
region = "us-west1"
}
hclYou need a credentials.json of a service account from your cloud so that terraform can authenticate and create resources. The region, preferably has to be the one closest to you.
Once this is done, run terraform init
on the terminal, which will download necessary plugins and initialise them.
Terraform Resources#
Terraform Resources are a library of resources that are dependent on your cloud provider. They can be anything from subnets, VCNs, Compute, Databases, Firewalls, basically anything — infra as code theme song here.
For example, the following resource creates a google compute instance, copies my ssh key into the VM, runs an update and also installs haproxy.
resource "google_compute_instance" "haproxy-server" {
name = "lb-1"
machine_type = "f1-micro"
zone = "us-west1-a"
metadata = {
ssh-keys = "sanjaybalaji:${file("~/.ssh/id_rsa.pub")}"
}
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
metadata_startup_script = "sudo apt-get update; sudo apt install -y haproxy"
network_interface {
network = "default"
access_config {
}
}
tags = ["http-server"]
}
hclTerraform Variables#
Variables in terraform need a 2 part process. They need to be first defined in variables.tf file, which is the basic template that contains default or can also be empty.
variable backend_server_count {
type = number
default = 2
}
variable backend_server_prefix {
type = string
default = "backend"
}
variable make_sticky {
type = string
default = "False"
}
hclYou can then add or change these default values during runtime in a file called terraform.tfvars, which could just contain the following.
backend_server_count=3
hclTerraform Statefile#
Statefile is what stores the current state of your infra-code. Anyone who gets a copy of this file and has access to your Console can delete it all. So store it securely.
Here are the most important terraform cli commands:
terraform refresh
— when you run this, it talks to the Cloud to fetch the latest state of your Infra from the cloud.terraform plan
— terraform creates a graph/path to get where you want it to be, the desired state.terraform apply
— does the actual talking and provisioning/deleting/modification when you run this.terraform taint
— marks a terraform resource to be run again, if it is a compute resource, it deletes and recreates the resource.terraform destroy
— deletes everything and undos everything your code did in the first place.
Always plan!#
Terraform is great, there are also some things you should be wary of, if you want to change the Base Image of a VM after provisioning, it deletes and recreates the VM, so all your configuration is lost. So, always run a plan to check the steps, to make sure no damage is being done.
Code Example#
You can find a code example for terraform in my github https://github.com/sanjayBala/gcp-ha-setup/tree/main/terraform ↗.
This article was originally published by me on Medium ↗.