Deploying a 3 tier app in GCP from Terraform

Deploying a three-tier application using Terraform is a popular approach because it provides infrastructure-as-code benefits. Terraform is an open-source infrastructure-as-code tool that allows you to define, configure, and manage infrastructure in a declarative language. In this blog post, we will explore how to deploy a three-tier application in Google Cloud Platform (GCP) using Terraform.

GCP Overview

Google Cloud Platform (GCP) is a suite of cloud computing services offered by Google. It provides a wide range of services such as virtual machines, storage, and networking, among others, that can be used to deploy and run applications in the cloud. One of the advantages of using GCP is its high availability and scalability, which makes it an excellent option for deploying enterprise-grade applications.

Terraform Overview

Terraform is an open-source infrastructure-as-code tool that enables you to define, configure, and manage infrastructure in a declarative language. It supports many cloud providers, including GCP, and enables you to automate infrastructure provisioning, configuration, and management.

Deploying a Three-Tier Application using Terraform in GCP

To deploy a three-tier application using Terraform in GCP, we will use a module. A module is a self-contained Terraform configuration that encapsulates a set of resources and their dependencies. In this example, we will use a module to deploy a simple three-tier application that consists of a web server, application server, and database server.

The following are the steps required to deploy the application:

  1. Set up a GCP project and enable the Compute Engine API.
  2. Create a service account in GCP and assign it the necessary roles.
  3. Write a Terraform module that defines the components and dependencies of the application.
  4. Initialize the Terraform module and run the Terraform plan command.
  5. Apply the Terraform configuration to create the infrastructure.
  6. Monitor the infrastructure in GCP.

Let’s go through each step in detail.

Step 1: Set up a GCP project and enable the Compute Engine API. To set up a GCP project and enable the Compute Engine API, follow these steps:

  1. Go to the GCP Console (console.cloud.google.com).
  2. Click on the project drop-down menu in the top left corner of the screen.
  3. Click on the “New Project” button.
  4. Enter a name for the project and click on the “Create” button.
  5. Once the project is created, select it from the project drop-down menu.
  6. Enable the Compute Engine API by navigating to APIs & Services > Library and searching for Compute Engine. Click on the “Enable” button.

Step 2: Create a service account in GCP and assign it the necessary roles. To create a service account in GCP, follow these steps:

  1. Go to the GCP Console (console.cloud.google.com).
  2. Navigate to the IAM & Admin > Service Accounts page.
  3. Click on the “Create Service Account” button.
  4. Enter a name for the service account and click on the “Create” button.
  5. On the “Create Key” tab, select “JSON” as the key type and click on the “Create” button. This will download a JSON file containing the service account key.
  6. Assign the service account the necessary roles by navigating to IAM & Admin > IAM and adding the roles “Compute Instance Admin (v1)” and “Service Account User” to the service account.

Step 3: Write a Terraform module that defines the components and dependencies of the application. To write a Terraform module that defines the components and dependencies of the application, follow these steps:

  1. Create a new directory for the Terraform module.
  2. Create a main.tf file in the directory and define the necessary resources for the three-tier application, such as Compute Engine instances, disks, and networking components.
  3. Define any necessary dependencies between the resources, such as making the application server depend on the database server.
  4. Define any necessary variables and outputs for the module.
  5. Use the Google Cloud Platform provider in Terraform to manage the GCP resources.

Here’s an example Terraform module that deploys a three-tier application:

provider "google" {
  credentials = file("path/to/credentials.json")
  project     = var.project_id
  region      = var.region
}

resource "google_compute_network" "my_network" {
  name                    = "my-network"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "my_subnetwork" {
  name          = "my-subnetwork"
  ip_cidr_range = "10.0.0.0/24"
  network       = google_compute_network.my_network.self_link
  region        = var.region
}

resource "google_compute_instance" "web_server" {
  name         = "web-server"
  machine_type = "f1-micro"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "centos-7"
    }
  }

  network_interface {
    network = google_compute_network.my_network.self_link
  }

  metadata_startup_script = <<-EOF
    #!/bin/bash
    yum install -y httpd
    systemctl enable httpd
    systemctl start httpd
  EOF
}

resource "google_compute_instance" "app_server" {
  name         = "app-server"
  machine_type = "n1-standard-1"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "centos-7"
    }
  }

  network_interface {
    network = google_compute_network.my_network.self_link
  }

  metadata_startup_script = <<-EOF
    #!/bin/bash
    yum install -y java-1.8.0-openjdk
    curl -L https://tomcat.apache.org/download-80.cgi#8.5.38 -o /tmp/tomcat.tar.gz
    mkdir /opt/tomcat
    tar xzvf /tmp/tomcat.tar.gz -C /opt/tomcat --strip-components=1
    systemctl enable tomcat
    systemctl start tomcat
  EOF
}

resource "google_compute_instance" "database_server" {
  name         = "database-server"
  machine_type = "n1-standard-2"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "centos-7"
    }
  }

  network_interface {
    network = google_compute_network.my_network.self_link
  }

  metadata_startup_script = <<-EOF
    #!/bin/bash
    yum install -y mariadb-server
    systemctl enable mariadb
    systemctl start mariadb
  EOF
}

Step 4: Initialize the Terraform module and run the Terraform plan command. To initialize the Terraform module and run the Terraform plan command, follow these steps:

  1. Open a terminal window and navigate to the directory where you saved the Terraform module.
  2. Run the command “terraform init” to initialize the module and download the necessary provider plugins.
  3. Define any necessary variables in a “variables.tf” file.
  4. Run the command “terraform plan” to generate a plan of the changes that will be made to the infrastructure.

Step 5: Apply the Terraform configuration to create the infrastructure. To apply the Terraform configuration to create the infrastructure, follow these steps:

  1. Run the command “terraform apply” to create the infrastructure.
  2. Review the plan that Terraform generates to ensure that the changes are correct.
  3. Type “yes” when prompted to confirm the changes.

Step 6: Monitor the infrastructure in GCP. To monitor the infrastructure in GCP, follow these steps:

  1. In GCP, navigate to the Compute Engine > Instances page to view the deployed VM instances.
  2. Monitor the VM instance status and any associated logs or metrics.

Conclusion Deploying a three-tier application using Terraform in GCP can be a powerful and flexible way to provision infrastructure. By using Terraform, you can automate the deployment of infrastructure and manage it as code. GCP provides many services that can be used to deploy and run applications in the cloud, and by combining Terraform and GCP, you can create a robust and scalable application infrastructure.


Leave a Reply