Automate Infrastructure using Terraform

What is Terraform?

Manish Jindal
Google Cloud - Community
3 min readNov 30, 2022

--

HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and On-premise resources in human-readable configuration files that you can version, reuse, and share.

How does terraform work?

Terraform creates and manages resources on cloud platforms and other services through their application programming interfaces (Target APIs).

Providers enable Terraform to work with virtually any platform or service with an accessible API.

Terraform Language

Terraform’s language is Terraform’s primary user interface. Configuration files you write in Terraform language tell Terraform what plugins to install, what infrastructure to create, and what data to fetch. Terraform language also lets you define dependencies between resources and create multiple similar resources from a single configuration block.

Components of terraform language

  • Provider : Terraform relies on plugins called “providers” to interact with cloud providers, SaaS providers, and other APIs.
  • Resource : Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects.
  • Data Source : Data sources allow Terraform to use information defined outside of Terraform, defined by another separate Terraform configuration, or modified by functions.
  • Variables and Outputs : Terraform language includes a few kinds of blocks for requesting or publishing named values.
  • Modules : Modules are containers for multiple resources that are used together. A module consists of a collection of .tf and/or .tf.json files kept together in a directory. Modules are the main way to package and reuse resource configurations with Terraform.
  • Expressions : Expressions refer to or compute values within a configuration
  • Functions : The Terraform language includes a number of built-in functions that you can call from within expressions to transform and combine values. The general syntax for function calls is a function name followed by comma-separated arguments in parentheses.

Terraform Workflow

Once you define resource in the terraform code, which may be across multiple cloud providers and services. You perform following steps to successfully create the infrastructure in the target provider.

  • Init: Initialise the cloud provider
  • Plan: Terraform creates an execution plan describing the infrastructure it will create, update, or destroy based on the existing infrastructure and your configuration.
  • Apply: On approval, Terraform performs the proposed operations in the correct order, respecting any resource dependencies.

Terraform Statefile

Terraform keeps track of your real infrastructure in a state file, which acts as a source of truth for your environment. Terraform uses the state file to determine the changes to make to your infrastructure so that it will match your configuration.

Terraform allows you to collaborate on your infrastructure with its remote state backend.

Provision Infrastructure using Terraform Configuration Language

There are three key components when you define your infrastructure using the terraform language.

  1. Providers : Providers allow Terraform to interact with cloud providers, SaaS providers, and other APIs, Provider configurations belong in the root module of a Terraform configuration.
provider "google" {
project = "my-project-id"
region = "us-central1"
}

2. Terraform Block : The special terraform configuration block type is used to configure some behaviours of Terraform itself, such as requiring a minimum Terraform version to apply your configuration.

terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 4.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
}
}
backend "gcs" {
bucket = "terraform-statefiles"
prefix = "terraform/state"
}
required_version = ">= 0.13"
}

3. Terraform Code : Terraform allows you to write infrastructure as a code (Resources) for the target provider APIs using the terraform language components.

After writing the terraform code for the target provider API use terraform workflow (init, plan, apply) to create the resources in the target provider.

Destroying Infrastructure

The terraform destroy command terminates resources managed by your Terraform project. This command is the inverse of terraform apply in that it terminates all the resources specified in your Terraform state. It does not destroy resources running elsewhere that are not managed by the current Terraform project.

--

--