Deploying an Apache Server with Terraform- GCP
Overview:

Today I will be showing you how to create an Apache web server using the IaC tool called, Terraform, inside a GCP cloud environment.

This walk-though has the expectation that you already have Terraform installed, gone through the how-to tutorial they provide, and have already created a service account with the appropriate json key in GCP.

What is IaC?

Infrastructure as code (IaC) has been steadily increasing as companies are adopting the devops culture into their environments. IBM’s definitions of IaC “…automates the provisioning of infrastructure, enabling your organization to develop, deploy, and scale cloud applications with greater speed, less risk, and reduced cost.” Popular cloud providers like GCP/AWS/Azure all provide their own in-house version of IaC tools.

Vender specific IaC:

Cloud Deployment Manager-GCP

Cloud Formation- AWS

Azure Resource Manager- Azure

What is Terraform?

Terraform is a popular IaC tool from HashiCorp that works with most public cloud providers. It is a up and coming tool for companies, who can manage multi-cloud environments with a single tool.

Terraform uses its own configuration language, (Terraform HashiCorp Configuration Language (HCL)) This is designed to allow concise descriptions of infrastructure. The Terraform language is declarative, describing an intended goal rather than the steps to reach that goal.

This model allows devops teams to add the to the code base and run Terraform apply which terraform will only deploy the desired state.

Example: You have two VMs (EC2/GCE) deployed with Terraform. If you want to add a storage bucket (S3/GCS) you can add the bucket to the same configuration file as the VMs and run, terraform apply, and Terraform would only create the bucket since that is the only change in the desired state)

Main Terraforms commands are:

terraform init

terraform plan

terraform apply

*You can learn more about how to use Terraform here*

What is Apache?

Apache Foundations defines itself as, “Founded in 1995 The Apache HTTP Server Project is an effort to develop and maintain an open-source HTTP server for modern operating systems including UNIX and Windows. The goal of this project is to provide a secure, efficient and extensible server that provides HTTP services in sync with the current HTTP standards.”

Apache is very popular, powering around 36% of websites around the world! It is still considered the leading web server; however, nginx is starting to make its way to the top

Overview of code:

Here is what is looks like when you have Terraform build a Debian based VM with an attached startup script that will update the VM and install an Apache web server.

provider "google" {



credentials = file("/Users/testuser/Desktop/gpsvc.json")



project = "googleproject"

region = "us-central1"

zone = "us-central1-c"

}







resource "google_compute_instance" "apache_test" {

name = "apacheserver"

machine_type = "f1-micro"



tags = ["http-server"]



boot_disk {

initialize_params {

image = "debian-cloud/debian-9"

}

}



metadata_startup_script = file("/Users/testuser/Desktop/apache2.sh")



scheduling {

preemptible = true

automatic_restart = false

}



network_interface {

network ="default"

access_config {



}





}

}

Terraform Code breakdown:

provider: Google (since we are using GCP)

credentials: GCP service account key and the file path location

project: GCP project ID, you can run gcloud projects list to find it

region: setting the default region for resources being built with terraform

zone: setting the default zone for resources being built with terraform

provider "google" {



credentials = file("/Users/testuser/Desktop/gpsvc.json")



project = "googleproject"

region = "us-central1"

zone = "us-central1-c"

}

resource “google_compute_intance” “apache_test”: this lets Terraform know what resource to query and the name of the resource inside Terraform, not to be confused with the name of the VM in GCP

name: the name of the VM inside GCP

machine_type: the machine type to use, a full list can be found here

tags: add network tags

boot_disk: select the image to use

metadata_startup_script: add your startup script, I will go into more detail about the script next.

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: defines the network the VM should use

network: what VPC your VM should use

access_config: leaving this blank will allow an external ephemeral ip be created

resource "google_compute_instance" "apache_test" {

name = "apacheserver"

machine_type = "f1-micro"



tags = ["http-server"]



boot_disk {

initialize_params {

image = "debian-cloud/debian-9"

}

}



metadata_startup_script = file("/Users/testuser/Desktop/apache2.sh")



scheduling {

preemptible = true

automatic_restart = false

}



network_interface {

network ="default"

access_config {



}

Startup script

The start up script is very simple and is written in bash. It allows the VM to update out of date packages and install Apache. I overwrote the default Apache HTML code with my own (feel free to adjust as you see fit), then I pushed the changes to the appropriate file. Once done, add the file path to the Terraform configuration file like the example above.

!/bin/bash



sudo apt-get update && sudo apt -y install apache2





echo '<!doctype html><html><body><h1>Hello if you see this than you have apache running!</h1></body></html>' | sudo tee /var/www/html/index.html

Deploying the VM with Terraform

Run the terraform init command in the directory you have Terraform installed. If successful, you should see something similar.

terraform init

Once you have done the terraform int command you will want to follow up with terraform plan, which will show you what Terraform will be building.

If there are no errors when you run terraform plan then you’re free to run terraform apply. This is where Terraform actually builds the VM with the service account. You should get prompted with “Do you want to perform these actions?”, which you will need to type in yes. Terraform will then create the VM with Apache 2 installed!

Viewing our Apache server in GCP

Navigate to your GCP project and verify that the VM was created either by the GUI, or running the gcloud command gcloud compute instances list — filter “instances_name”.

gcloud command

Confirm Apache was installed

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

Deleting the VM with Terraform

Simply delete the VM with Terraform by running the terraform destroy command, this will delete the VM from your project and stop any and all charges. You will also be required to confirm these changes like you had to with terraform apply.

FIN!

Key considerations

Ensure you have a service account created in your project that Terraform can use. If you’re having issues, here is a video that shows how to create one along with the JSON key.

Make sure your service account has the appropriate IAM permissions to perform the actions you’re looking to do.

Store your .tf files in Github.

Your web server is http only, so if you have the network tag “https-server” you will get an error.

Bio: Jake has been in IT for 4 years and is currently an IT Engineer for Aunt Bertha. He currently holds a CompTIA S+ and Google ACE certification and is currently working on Google’s Professional Cloud Architect certification.

LinkedIn: https://www.linkedin.com/in/jacob-c-luna/
['Jacob Luna']
['Apache', 'Terraform', 'Gcp', 'Google Cloud Platform']