google cloud terraform examples

Terraform on Google Cloud: Top 5 CNN Examples

By Jessica Thompson
AD

Google Cloud's extensive suite of products and services, combined with Terraform's powerful infrastructure-as-code capabilities, allows for seamless management and automation of complex cloud environments. Terraceform is an open-source tool that uses a declarative configuration language to provision and manage resources, regardless of the provider. Here, we'll explore how to leverage Terraform for Google Cloud with detailed examples.

With Terraform, you can not only create and manage virtual machines, networks, and storage on Google Cloud, but also configure load balancers, manage database instances, and much more. Let's dive into some practical examples of provisioning Google Cloud resources using Terraform.

Setting Up Google Cloud and Terraform

Before getting started with Terraform, ensure you have properly configured your Google Cloud account. Create a new project, enable the necessary APIs, and download the JSON key file for authentication. Store this file in a secure location, as you'll need it during Terraform setup.

AD
Effortlessly Deploy Your GCP Cloud Run App Using Terraform
Effortlessly Deploy Your GCP Cloud Run App Using Terraform
a cloud with the google logo on it in front of a blue background filled with data
a cloud with the google logo on it in front of a blue background filled with data
The Definitive Guide to Cloud Cost Optimization with Terraform
The Definitive Guide to Cloud Cost Optimization with Terraform

Next, initialize Terraform and provide your Google Cloud credentials. Here's how to do it:

```bash terraform init export GOOGLE_CLOUD_PROJECT="your-project-id" export GOOGLE_CLOUD_KEY_FILE="$HOME/Downloads/keyfile.json" terraform plan ```

Terraform Basics: Provisioning a Compute Instance

One of the primary use cases for Terraform and Google Cloud is provisioning virtual machines (compute instances). Let's create a simple Terraform configuration that spins up a new instance.

How to Earn with Google Cloud (2026) | Complete Beginner Roadmap
How to Earn with Google Cloud (2026) | Complete Beginner Roadmap
Terraform For Google Cloud Essential Guide: Learn How To Provision Infrastructure In Google Cloud Securely And Efficiently
Terraform For Google Cloud Essential Guide: Learn How To Provision Infrastructure In Google Cloud Securely And Efficiently
Google Cloud - NERDO Studio
Google Cloud - NERDO Studio

Create a new file called `main.tf` and add the following content:

```hcl provider "google" { project = var.project_id region = var.region zone = var.zone } resource "google_compute_instance" "default" { name = "terraform-instance" machine_type = "n1-standard-1" boot_disk { initialize_params { image = "debian-cloud/debian-9" } } network_interface { network = "default" access_config { } } } ```

In this example, Terraform provisions a new instance using the Debian 9 image and associates it with the default network. When you apply this configuration (`terraform apply`), Terraform creates the instance in your Google Cloud project.

AD

Terraform Modules: Simplifying Infrastructure Management

Terraform modules are reusable, modular components that encapsulate functionality and remove complexity. They allow you to quickly provision common resource configurations, such as load balancers, VPCs, or managed database instances.

the google cloud logo is shown on top of a white poster with colorful lines and dots
the google cloud logo is shown on top of a white poster with colorful lines and dots
Complete Google Cloud Tech Stack for AI/ML & Generative AI 2025 #aiconcepts
Complete Google Cloud Tech Stack for AI/ML & Generative AI 2025 #aiconcepts
the google cloud logo is shown in front of a white background with green lines and dots
the google cloud logo is shown in front of a white background with green lines and dots
Google Cloud Platform
Google Cloud Platform
Your First Step in Google Cloud Platform : Building a Landing Zone with Terraform
Your First Step in Google Cloud Platform : Building a Landing Zone with Terraform
Google Cloud AI Platforms
Google Cloud AI Platforms

For instance, you can use Google's official Terraform module for app engine to deploy and manage your applications:

```bash module "appengine" { source = "../../../modules/appengine" project_id = var.project_id service_account_id = google_service_account.appengine.email https_health_check_path = "/pinger" } ```

Managing Networking Resources with Terraform

Terraform also simplifies the provisioning and management of networking resources on Google Cloud, such as VPC networks, subnets, and Routes. Let's explore an example of creating a custom VPC network.

AD

Create a new file called `networking.tf` and add the following content:

```hcl resource "google_compute_network" "custom_vpc" { name = "custom-vpc-network" } resource "google_compute_subnetwork" "custom_subnet" { namepoonet = google_compute_network.custom_vpc.id ip_cidr_range = "10.0.0.0/16" } resource "google_compute_router" "custom_router" { name = "custom-router" network = google_compute_network.custom_vpc.id } ```

Terraform initializes, plans, and applies this configuration to create a new VPC network with a custom subnet and router. This example demonstrates how Terraform allows you to manage networking resources declaratively and programmatically.

Using Terraform for Continuous Deployment

Beyond infrastructure management, Terraform integrates well with other tools for continuous deployment. By combining Terraform with services like Jenkins or Google Cloud Build, you can automate the deployment of your applications and infrastructure.

AD

To deploy a simple Node.js application using Terraform, first create an `index.js` file with the following content:

```javascript const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); }); ```

Next, create a new file called `deployment.tf` and add the following content:

```hcl resource "google_compute_instance" "default" { name = "terraform-instance" machine_type = "n1-standard-1" boot_disk { initialize_params { image = "debian-cloud/debian-9" } } network_interface { network = "custom-vpc-network" access_config { } } provisioner "remote-exec" { inline = [ "sudo apt-get update", "sudo apt-get install -y nodejs", "git clone https://github.com/yourrepository/your-node-js-app.git", "cd your-node-js-app", "npm install", "PM2 start /your-node-js-app/index.js", ] } } ```

In this example, the `remote-exec` provisioner is used to clone your Node.js application's repository, install dependencies, and start the application using PM2. When you apply this configuration (`terraform apply`), Terraform provisions the instance and deploys your application.

The power of leveraging Google Cloud and Terraform together is clear - with Terraform, you can create and manage complex, highly available infrastructure effortlessly. Start experimenting with Terraform and Google Cloud today, and watch as your cloud environments become more manageable, dynamic, and reliable.

Why not give it a try now? Get started with Google Cloud's free tier and explore the endless possibilities that Terraform offers.

AD