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.

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.

  • Cloud Formation- AWS
  • Azure Resource Manager- Azure

What is Terraform?

terraform init
terraform plan
terraform apply

What is Apache?

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)

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 {

}

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

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.