Skip to content

Commit

Permalink
Merge pull request #66 from rosera/gke-tf
Browse files Browse the repository at this point in the history
Update: Terraform GKE definition
  • Loading branch information
Dylan Peck authored Aug 5, 2022
2 parents b9642cb + 4e1d667 commit 3729824
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 54 deletions.
129 changes: 77 additions & 52 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018 Google LLC
Copyright 2022 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -78,20 +78,20 @@ data "template_file" "startup_script" {
EOF


vars = {
vars {
project = var.project
version = var.ver
version = var.version
}
}

// https://www.terraform.io/docs/providers/google/r/compute_instance.html
// The ContainerOS deployment instance definition which will
// run the container instead of as the interpreted python code.
resource "google_compute_instance" "container_server" {
name = "cos-vm"
name = "cos-vm"
machine_type = var.machine_type
zone = var.zone
project = var.project
zone = var.zone
project = var.project

tags = ["flask-web"]

Expand All @@ -101,7 +101,7 @@ resource "google_compute_instance" "container_server" {
}
}

metadata = {
metadata {
user-data = data.template_file.startup_script.rendered
}

Expand All @@ -120,13 +120,30 @@ resource "google_compute_instance" "container_server" {
}

// The Kubernetes Engine cluster used to deploy the application
// https://www.terraform.io/docs/providers/google/r/container_cluster.html
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/container_cluster
resource "google_container_cluster" "prime_cluster" {
name = var.cluster_name
zone = var.zone
project = var.project
min_master_version = data.google_container_engine_versions.on-prem.latest_master_version
initial_node_count = 2
name = var.cluster_name
location = var.zone
project = var.project
remove_default_node_pool = true
initial_node_count = 1
}

resource "google_container_node_pool" "primary_preemptible_nodes" {
name = "my-node-pool"
location = var.zone
cluster = google_container_cluster.prime_cluster.name
node_count = 1

node_config {
preemptible = true
machine_type = "e2-standard-2"

# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
oauth_scopes = [
"https://www.googleapis.com/auth/cloud-platform"
]
}
}

// Create a deployment manifest with the appropriate values
Expand Down Expand Up @@ -168,20 +185,26 @@ data "template_file" "deployment_manifest" {
initialDelaySeconds: 10
EOF

vars {
project = var.project
version = var.version
replicas = var.replicas
}

vars = {
project = var.project
version = var.ver
replicas = var.replicas
}
}

// Render the deployment manifest on the local filesystem using a null resource
// https://www.terraform.io/docs/provisioners/null_resource.html
resource "null_resource" "deployment_manifest" {
triggers = {
template = data.template_file.deployment_manifest.rendered
}

triggers {
template = data.template_file.deployment_manifest.rendered
}

provisioner "local-exec" {
command = "echo \"${data.template_file.deployment_manifest.rendered}\" > ${path.module}/manifests/prime-server-deployment.yaml"
}


provisioner "local-exec" {
command = "echo \"${data.template_file.deployment_manifest.rendered}\" > ${path.module}/manifests/prime-server-deployment.yaml"
Expand All @@ -199,41 +222,41 @@ depends_on = [google_container_cluster.prime_cluster]
// prime-server
//
resource "google_storage_bucket" "artifact_store" {
name = "${var.project}-vm-artifacts"
project = var.project
# force_destroy = true
name = "${var.project}-vm-artifacts"
project = var.project
# force_destroy = true
}

// https://www.terraform.io/docs/providers/google/r/storage_bucket_object.html
resource "google_storage_bucket_object" "artifact" {
name = "${var.ver}/flask-prime.tgz"
source = "../build/flask-prime.tgz"
bucket = google_storage_bucket.artifact_store.name
// TODO: ignore lifecycle something so old versions don't get deleted
name = "${var.version}/flask-prime.tgz"
source = "../build/flask-prime.tgz"
bucket = google_storage_bucket.artifact_store.name
// TODO: ignore lifecycle something so old versions don't get deleted
}

data "template_file" "web_init" {
template = file("${path.module}/web-init.sh.tmpl")
vars = {
bucket = "${var.project}-vm-artifacts"
version = var.ver
}
template = file("${path.module}/web-init.sh.tmpl")
vars {
bucket = "${var.project}-vm-artifacts"
version = var.version
}
}

// https://www.terraform.io/docs/providers/google/r/compute_instance.html
resource "google_compute_instance" "web_server" {
project = var.project
name = "vm-webserver"
machine_type = var.machine_type
zone = var.zone
project = var.project
name = "vm-webserver"
machine_type = var.machine_type
zone = var.zone

tags = ["flask-web"]
tags = ["flask-web"]

boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}

network_interface {
network = "default"
Expand All @@ -243,8 +266,10 @@ access_config {
}
}

// install pip and flask
metadata_startup_script = data.template_file.web_init.rendered

// install pip and flask
metadata_startup_script = data.template_file.web_init.rendered


service_account {
scopes = ["storage-ro", "compute-rw"]
Expand All @@ -258,13 +283,13 @@ google_storage_bucket_object.artifact,

// https://www.terraform.io/docs/providers/google/r/compute_firewall.html
resource "google_compute_firewall" "flask_web" {
name = "flask-web"
network = "default"
project = var.project
allow {
protocol = "tcp"
ports = ["8080"]
}
name = "flask-web"
network = "default"
project = var.project
allow {
protocol = "tcp"
ports = ["8080"]
}

source_ranges = ["0.0.0.0/0"]
source_tags = ["flask-web"]
Expand Down
4 changes: 2 additions & 2 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018 Google LLC
Copyright 2022 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,7 +35,7 @@ variable "replicas" {
default = "1"
}

variable "ver" {
variable "version" {
type = string
}

Expand Down

0 comments on commit 3729824

Please sign in to comment.