Build Infrastructure – Terraform GCP Example

Reading Time: 3 minutes

Hi Readers, In this blog we will be looking about How to Build Build Infrastructure – Terraform GCP Example. We will be discussing about its basics and its key components.

With Terraform installed, you are ready to create some infrastructure.

Prerequisites

Set up GCP

After creating your GCP account, create or modify the following resources to enable Terraform to provision your infrastructure:

  • A GCP Project: GCP organizes resources into projects. Create one now in the GCP console and make note of the project ID.
  • Google Compute Engine: Enable Google Compute Engine for your project in the GCP console.
  • A GCP service account keyCreate a service account key to enable Terraform to access your GCP account.

Write configuration

The set of files used to describe infrastructure in Terraform is known as a Terraform configuration. You will now write your first configuration to create a network.

Each Terraform configuration must be in its own working directory. Create a directory for your configuration.

$ mkdir learn-terraform-gcp

Change into the directory.

$ cd learn-terraform-gcp

Terraform loads all files ending in .tf or .tf.json in the working directory. Create a main.tf file for your configuration.

$ touch main.tf

Open main.tf in your text editor, and paste in the configuration below. Be sure to replace <NAME> with the path to the service account key file you downloaded and <PROJECT_ID> with your project’s ID, and save the file.

terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "3.5.0"
    }
  }
}

provider "google" {
  credentials = file("<NAME>.json")

  project = "<PROJECT_ID>"
  region  = "us-central1"
  zone    = "us-central1-c"
}

resource "google_compute_network" "vpc_network" {
  name = "terraform-network"
}

Terraform Block

The terraform {} block contains Terraform settings. For each provider, the source attribute defines an optional hostname, a namespace, and the provider type.

The version attribute is optional, but we recommend using it to enforce the provider version. Without it, Terraform will always use the latest version of the provider, which may introduce breaking changes.

Providers

The provider block configures the specified provider, in this case google. A provider is a plugin that Terraform uses to create and manage your resources. You can define multiple provider blocks in a Terraform configuration to manage resources from different providers.

Resource

Use resource blocks to define components of your infrastructure. A resource might be a physical component such as a server, or it can be a logical resource such as a Heroku application.

Resource blocks have two strings before the block: the resource type and the resource name. Together, the resource type and resource name form a unique ID for the resource. For example, the ID for your network is google_compute_network.vpc_network.

Initialize the directory

When you create a new configuration — or check out an existing configuration from version control — you need to initialize the directory with terraform init. This step downloads the providers defined in the configuration.

Initialize the directory.

$ terraform init

The terraform init command prints the provider version Terraform installed. Terraform also creates a lock file named .terraform.lock.hcl, which specifies the exact provider versions used to ensure that every Terraform run is consistent. This also allows you to control when you want to upgrade the providers used in your configuration.

Format and validate the configuration

The terraform fmt command automatically updates configurations in the current directory for readability and consistency.

Format your configuration. Terraform will print out the names of the files it modified, if any. In this case, your configuration file was already formatted correctly, so Terraform won’t return any file names.

$ terraform fmt

You can also make sure your configuration is syntactically valid and internally consistent by using the terraform validate command.

Validate your configuration. The example configuration provided above is valid, so Terraform will return a success message.

$ terraform validateSuccess! The configuration is valid.

Create infrastructure

Apply the configuration now with the terraform apply command. Terraform will print output similar to what is shown below. We have truncated some of the output for brevity.

$ terraform apply
  
Enter a value: yes

You have now created infrastructure using Terraform! Visit the GCP console to see the network you provisioned. Make sure you are looking at the same region and project that you configured in the provider configuration.

Inspect state

When you applied your configuration, Terraform wrote data into a file called terraform.tfstate. Terraform stores the IDs and properties of the resources it manages in this file, so that it can update or destroy those resources going forward.

Inspect the current state using terraform show.

$ terraform show

When Terraform created this network, it also gathered its metadata from the Google provider and recorded it in the state file.

I hope you got a quick overview about Build Infrastructure – Terraform GCP Example with this blog. If you have any doubt, feel free to contact me navdeep.parash@knoldus.com. In next blog we will look the Change Infrastructure.

Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and if you feel, give me suggestions on scope of improvements.

References :
https://learn.hashicorp.com/