We have the answers to your questions! - Don't miss our next open house about the data universe!

Terraform: Using GCP to deploy your infrastructure

 m de lecture
terraform gcp

Terraform simplifies the management of cloud infrastructures, including Google Cloud Platform. In this article, we will explore how to build a GCP environment using Terraform.

What is Terraform ?

Terraform is an open-source Infrastructure as Code (IaaS) tool developed by HashiCorp. This solution allows you to manage cloud infrastructure efficiently.

You can centralize various cloud services (such as Google Platform, Amazon Web Services, GitHub, Microsoft Azure, etc.) and describe their configurations through a single unified solution.

ūüí°Important to note: In Terraform’s language, all these cloud services are referred to as “Providers.”

In this article, we will focus specifically on one provider: Google Cloud Platform. The goal is to understand how to deploy GCP infrastructure using Terraform.

How does Terraform work?

To run Terraform on GCP, you need to understand how it works.

First and foremost, it’s a declarative coding tool that uses the HashiCorp Configuration Language (HCL). This language allows you to precisely describe an existing infrastructure and its desired final state.

Terraform operates in three phases:

  1. Plan: This phase visualizes the execution plan in a dry run mode. Terraform uses the principle of idempotence, meaning it compares the current state to the desired state. During this planning phase, no actions are implemented.

  2. Apply: If Terraform detects no difference between the initial state of the infrastructure and the target state, it takes no action. However, if a delta is detected, the IaaS tool takes action. For example, it creates a new resource, updates certain elements, or removes others. The newly created or modified resources are organized into “topologies,” which are sets of coherent resources (related to an instance, a network, a region, etc.).

  3. Destroy: Terraform can also be used to manage the destruction of infrastructure elements.

All these actions are recorded in the tfstate file. This will retrieve the technical IDs of each element. In the event of an update, developers will be able to refresh the various attributes.

How do you deploy your GCP infrastructure with Terraform?

Tool installation

The first step to deploy your GCP infrastructure with Terraform is to install and configure the necessary tools (Terraform and Google Cloud Platform) if you haven’t already.

To establish a connection between the two, you’ll also need to use Google Cloud SDK to manage your GCP resources. This will allow you to authenticate with your Google Cloud Platform account and manage the infrastructure with Terraform.

Terraform's structure

Terraform organizes all GCP files within a working directory. Whether you have a single file or multiple files, they will all appear in Terraform. However, it’s advisable to organize your resources effectively to streamline your work and collaboration with other teams.

The Terraform structure comprises several key elements:

  1. GCP: This is the top-level that appears in Terraform. This file encompasses all your resources stored on Google Cloud Platform.

  2. Main.tf: This directory is used to execute Terraform. It contains several sections (Provider = GCP, Module = your resources, Output = final results).

  3. Variable.tf: You should define all your variables here, such as GCP regions. This is the only file you can modify.

  4. Backend.tf: This allows you to store various actions in a tfstate file. Typically, it is stored in Google Cloud Storage, but you can keep it locally. You can also share it with other developers.

Creating modules

To deploy the GCP infrastructure with Terraform, you must use modules. These modules enable you to:

  1. Create Subnetworks: You can create subnetworks either in a custom or automatic mode. By default, subnetwork creation is automatic, meaning when a new region appears, a new subnetwork is created. If you don’t want this, you can disable the automatic mode and create subnetworks only when needed.

  2. Define Firewall Rules: These modules allow you to specify rules for allowing or blocking connections to your resources.

  3. Configure Subnetworks: While Google Cloud Platform is a public network, it also hosts Virtual Private Clouds (VPCs). With modules, you can define which subnetwork is public and which one is private.

  4. Create Compute Engine Virtual Machine Instances: This allows you to run virtual machines on the Google infrastructure.

There are many other GCP features that you can configure in Terraform modules. If you want to learn more about their utility and how they work, consider joining our training programs at Datascientest.

Executing the Terraform plan

To execute the Terraform plan, you should use the Google Cloud SDK tool you’ve previously installed.

This connection allows Terraform to compare the current state of GCP with the desired state (defined through various modules) and implement the necessary changes.

What are the advantages of Terraform for Google cloud platform?

While Terraform is a relatively new tool (2014), it has already gained popularity among many developers. Here are the main reasons explaining its success:

  1. Rapid Infrastructure Management: With various configuration files, it’s easy to provision, define, or configure your resources.

  2. Collaboration: Terraform’s multiple features, such as state management and modules, make it suitable for teams of all sizes, no matter where they are located.

  3. Community: Terraform is an open-source tool that continuously improves thanks to contributions from its users.

Additionally, Terraform is cross-platform. You can use it not only for Google Cloud but also for AWS, Oracle Cloud Infrastructure, Alibaba Cloud, Azure, and more.

Key facts

  • Terraform empowers you to manage and deploy cloud infrastructures quickly and efficiently. As a declarative coding tool, Terraform compares an initial state with a target state and applies the necessary changes to reach the target state.

  • Using Terraform for GCP not only saves you time in deployment and management but also enables efficient collaboration with other DevOps professionals.

Now that you have a good understanding of Terraform for GCP, you can begin training to master this valuable tool in Data Science. Sign up today at DataScientest.


DataScientest News

Sign up for our Newsletter to receive our guides, tutorials, events, and the latest news directly in your inbox.

You are not available?

Leave us your e-mail, so that we can send you your new articles when they are published!
icon newsletter


Get monthly insider insights from experts directly in your mailbox