Deploying an Apache Server with Terraform- GCP

Overview:

What is IaC?

What is Terraform?

terraform init
terraform plan
terraform apply

What is Apache?

Overview of code:

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" {

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

!/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

terraform init

Viewing our Apache server in GCP

gcloud command

Confirm Apache was installed

Deleting the VM with Terraform

FIN!

Key considerations

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store