Satish Radadiya
4 min readDec 5, 2022

Using Terraform for Goole Cloud Platform (Terraform with GCP)

What is Terraform?

HashiCorp Terraform is an infrastructure as a code tool that lets you define 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 its 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 the 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 resources in the terraform code, which may be across multiple cloud providers and services. You perform the 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 the configuration block type is used to configure some behaviors 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 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 the command terminates resources managed by your Terraform project. This command is the inverse 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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Satish Radadiya
Satish Radadiya

No responses yet

Write a response

Recommended from Medium

Lists

See more recommendations