GCP Infrastructure using Terraform

Aman Jhagrolia
5 min readAug 28, 2020
Google Cloud Platform

Google Cloud Platform, offered by Google, is a suite of cloud computing services that runs on the same infrastructure that Google uses internally for its end-user products, such as Google Search, Gmail, file storage, and YouTube.

Today, we will use Google Cloud Platform to Deploy our WordPress with MySQL Database. We'll use Terraform to automate everything.

Terraform is an open-source infrastructure as code software tool. It is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform manages external resources with “providers”.

Here is my terraform code:- https://github.com/jhagdu/terraform-gcp-wp-infrastructure.git

My Terraform Registry:- https://registry.terraform.io/modules/jhagdu/wp-infrastructure/gcp/1.0.0

First, Let's understand our complete terraform code which is divided into two files variables.tf and main.tf

variables.tf :-

This Terraform file contains all the variables that are used

variables.tf

main.tf :-

This is the main terraform code that builds infrastructure on GCP. It will create different Resources on GCP like VPC Network, Subnetworks, Firewalls, SQL Database Instance, GKE Cluster, and finally, deploy WordPress on it. Let's understand each part of main.tf file

Setting up Providers - First of all, we need to set up providers in our code. Here we are creating Infrastructure on GCP, so we need “google” Provider and Finally, we need to deploy WordPress on GKE Cluster (Google Kubernetes Engine), so we also need “Kubernetes” Provider.

To set up a “google” provider we need our Project ID and Credentials. To get credentials go-to service accounts in your GCP dashboard and create key there, this will download a credential file for you.

Providers part of main.tf

VPC Network - This code will create Two VPC Networks and Subnetworks in them and also create Firewalls for both VPC Networks and allow some specific Ports.

Networking part of main.tf

VPC Network Peering - This code will create peering between both VPC Networks that are created.

VPC Peering part of main.tf

SQL Database Instance - This code will configure SQL Database Instance in GCP. After that, it will also create a database with name “wpdb” and the user with name “wpuser” in that SQL Instance.

SQL part of main.tf

GKE Cluster - Now we will create Kubernetes Cluster and For this, we will use the GKE Service of GCP. The code below will create a GKE Cluster and NodePool for us and after that, it will also configure our local kubectl with that GKE Cluster. Now we are able to use that GKE Cluster from our local system using kubectl command.

GKE part of main.tf

Deploy WordPress - Now we are ready to deploy our WordPress on GKE Cluster. For this, we will use the Kubernetes deployment resource. The following Code will create a deployment for our WordPress Pod. Our WordPress will use GCP SQL Database Instance to store the data. So this code will also configure database host, user, password, database name for our WordPress Pod.

WP Deployment part of main.tf

LoadBalancer Service - Now we will create LoadBalancer Service for our WordPress Pod. This will create a LoadBalancer in GCP. After that this code will also show External IP of LoadBalancer in Output.

LB Service part of main.tf

Open WordPress - Finally our WordPress is deployed on GCP Infrastructure and Now we are ready to use it. So this is our Code which will automatically open our WordPress in Browser. Along with this will also give us database host, database name, user, and password in output.

Output part of main.tf

Now after writing the Terraform code, we are just one step behind to create complete GCP Infrastructure and deploy WordPress on it. We just need to apply our terraform code. To apply our terraform code we just need to run the “terraform apply” command.

As soon as we apply, our GCP Infrastructure starts creating, and just after a few minutes on completion, our terraform code will give us the IP of WordPress LoadBalancer Service and some SQL database info.

Terraform Apply

VPC Networks - Two VPC Networks are created and also a subnetwork is created in each along with firewalls. One VPC Network is created in asia-south1 and other in the us-central1 region.

VPC Networks

VPC Peering - Both are VPC are peered with each other i.e. both are connected to each other using Google own private network

VPC Peering

SQL Database Instance - One SQL Database Instance in region asia-south1 is also created which is used by our WordPress Pods to store data

SQL Database Instance

SQL Database and Users - Inside that SQL Instance, a database with name “wpdb” and user with name “wpuser” is also created.

Database and User in SQL

GKE Cluster - GKE Cluster is created in the us-central1 region.

GKE Cluster

NodePool - One NodePool is also created

NodePool

Workloads and Services - As soon our WordPress Deployment is created in GKE Cluster, we can see that one Deployment type workload is created. And on the creation of LoadBalancer service one External Load Balancer type service is created.

Workloads and Services in GKE

LoadBalancer - LoadBalancer is created in GCP for WordPress Pod when we create a service of type LoadBalancer.

LoadBalancer

WordPress Connected - Finally, Our WordPress is ready to use and here we are successfully connected with it.

WordPress Connected

Thanks, For reaching the end. Hope this is helpful to you 🙂

--

--