Deploying a Webserver with Terraform on GCP

Sameed Uddin Mohammed
Geek Culture
Published in
5 min readJun 28, 2021

--

Picture by David Aquino

In this project, we will learn how to use Terraform with GCP but also create a Apache Webserver on the instance (f1-micro) using bash script.

Overview of the following products & services being used in this project:

Terraform

Terraform is an open-source cloud agnostic infrastructure as code tool that provides a consistent CLI workflow to manage hundreds of cloud vendor-specific services. Terraform codifies cloud APIs into declarative configuration files.Most Popular cloud providers like GCP|AWS|Azure all provide their own version of Infra-as-code tools:

  • Cloud Deployment Manager-GCP
  • Cloud Formation- AWS
  • Azure Resource Manager- Azure

Its Benefits:

  • It provisions infrastructure seamlessly
  • It updates infrastructure with ease
  • It destroys infrastructure without hassle
  • It checks codes errors with just click of a button
  • It plans out infrastructures prior to deployment
  • It tracks records from state file

You can find all the related Terraform-GCP Modules here.

Step 1:-

There are a few out package managers you can use to install Terraform on Windows. My favorite is Chocolatey. It makes installing, removing and updating software as simple as a one-line command, and Terraform is no exception to that.

To install Terraform with Chocolatey, do the following steps:

  1. Open a CMD/PowerShell prompt as an administrator and install Chocolatey using the command from their install page.
  2. Once that is complete, run choco install terraform -y on the end to auto-agree to installing it on your device.

After that command runs, you will get something like this:

Chocolatey v0.10.13
2 validations performed. 1 success(es), 1 warning(s), and 0 error(s).
Installing the following packages:
terraform
By installing you accept licenses for the packages.
Progress: Downloading terraform 0.12.6... 100%
terraform v0.12.6 [Approved]
Downloading terraform 64 bit from 'https://releases.hashicorp.com/terraform/0.12.6/terraform_0.12.6_windows
Download of terraform_0.12.6_windows_amd64.zip (15.32 MB) completed.
--SNIP--

Step 2:-

We need a IDE to write Terraform configuration and modules for GCP. Im going to use my favourite Visual Studio Code & open Ctrl + ~ to open the terminal inside it and create a Folder called “Terraform on GCP”

Prerequisites :

  • GCP service account key and the file in path location
  • GCP project ID, you can run gcloud projects list to find it
  • Setting the default region for resources being built with terraform
  • Setting the default zone for resources being built with terraform

GCP Service Account

We will need a GCP Service Account with Compute Admin (roles/compute.admin) permissions and the JSON file of the Service Account inside the “Terraform on GCP” folder.

Bucket for Terraform Backend

We need to create a Storage Bucket inside the GCP in order to use it as remote-backend & store our state file in it.

Cloud Storage API enabled

We select our root project, type Cloud Storage API on the search box and select Cloud Storage API & Compute Engine API

If the API is disabled, we click on the ENABLE button to enable it.

Inside “Terraform on GCP” Folder:

Firstly we will define the configuration of remote backend:

Lets create provider.tf which helps you define which provider you wanna use with your terraform, also we will define default region & zone, In our case we add the following:

We also need

This lets Terraform know what resources to query and the name of the resource inside Terraform.

Name: Custom name of the VM inside GCP.

Machine_types: This is basically a combination of CPU | RAM & GPU helps us choose different classes of instances available on GCP, a full list can be found here.

Tags: Add network tags.

Boot_disk: Select the image to use (in our case we will use default)

metadata_startup_script: Add your startup script to custom install dependencies.

Scheduling: Since this is going to be temporary machine, I made the VM a preemptible. If you decide to do the same, automatic_restart must equal false.

Network_interface: This helps us defines the network the VM should use.

Network: What VPC your VM should use.

access_config: Leave this blank as it will automatically create an external ephemeral ip.

Finally we need a startup script

The start up script is very simple bash script. It allows the instance to update packages and install Apache Web-Server, then I pushed the changes to the startup.sh script. Once done, add the file path to the Terraform configuration file like the example above:

Time to deploy our resources using Terraform commands:

$ terraform init

This will initialize and install necessary plugins to make terraform run.

Makes sure to validate the syntax of the all terraform files in your directory

$ terraform validate
Success! The configuration is valid.

Then, we will plan our terraform infrastructure to make sure what resources will be deployed using this code, shown below:

After reviewing the code we will finally do terraform apply — auto-approve to accept and deploy resources declared in terraform files above on GCP.

(Optional) Confirm it by going to GCP console.

Confirm Apache was installed

Navigate to the “EXTERNAL_IP” and if the startup script was successful you will see your web server!

Time to Destroy everything we have build using :

$ terraform destroy
.
.
Enter a value: yes

Thank you for spending time on my post, Please do Clap! Comment & Share if you like. Im a DevOps Enthusiast practicing my skills, learning, trying to help and grow together & You can follow me on: https://www.linkedin.com/in/mynameisameed/

--

--

Sameed Uddin Mohammed
Geek Culture

Terraform|3x-AWS|2x-Azure|2x-GCP|CertifiedCloud/DevOpsEngineerLooking for better opportunities- Remote| Join me https://chat.whatsapp.com/EiCi7XYnCSD7BQnPz2mJIa