Skip to content

Commit

Permalink
Migrated MIG example to TF 0.12 syntax and compatible modules
Browse files Browse the repository at this point in the history
- Migrated to TF 0.12 syntax
- Added provider version restrictions
- Migrated to TF 0.12 compatible modules
- Added provision of required resources:
  - network
  - submentwork
  - router
  - cloud-nat
- Optimized tag assigment with local variable
- Added new required variables:
  - service_account
  - network
  • Loading branch information
ivankorn committed Oct 8, 2019
1 parent eea19bd commit b6d0f96
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 53 deletions.
112 changes: 79 additions & 33 deletions examples/managed_instance_group/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@ locals {
"130.211.0.0/22",
"35.191.0.0/16",
]
target_tags = [
"container-vm-test-mig"
]
}

provider "google" {
region = var.region
project = var.project_id
version = "~> 2.7.0"
}

provider "google-beta" {
project = var.project_id
version = "~> 2.7.0"
}

module "gce-container" {
Expand All @@ -33,43 +42,78 @@ module "gce-container" {
}
}

module "mig" {
source = "GoogleCloudPlatform/managed-instance-group/google"
version = "1.1.14"
project = var.project_id
region = var.region
zone = var.zone
name = var.mig_name
machine_type = var.machine_type
compute_image = module.gce-container.source_image
size = var.mig_instance_count
service_port = var.image_port
service_port_name = "http"
http_health_check = "true"
subnetwork = var.subnetwork
subnetwork_project = var.subnetwork_project
ssh_source_ranges = ["0.0.0.0/0"]
target_tags = ["container-vm-test-mig"]

metadata = merge(var.additional_metadata, map("gce-container-declaration", module.gce-container.metadata_value))

instance_labels = {
"container-vm" = module.gce-container.vm_container_label
}
resource "google_compute_network" "default" {
name = var.network
auto_create_subnetworks = "false"
}

resource "google_compute_subnetwork" "default" {
name = var.network
ip_cidr_range = "10.125.0.0/20"
network = google_compute_network.default.self_link
region = var.region
private_ip_google_access = true
}

service_account_scopes = [
"https://www.googleapis.com/auth/cloud-platform",
# Router and Cloud NAT are required for installing packages from repos (apache, php etc)
resource "google_compute_router" "default" {
name = "${var.network}-gw-group1"
network = google_compute_network.default.self_link
region = var.region
}

module "cloud-nat" {
source = "terraform-google-modules/cloud-nat/google"
version = "~> 1.0.0"
router = google_compute_router.default.name
project_id = var.project_id
region = var.region
name = "${var.network}-cloud-nat-group1"
}

module "mig_template" {
source = "terraform-google-modules/vm/google//modules/instance_template"
version = "~> 1.0.0"
network = google_compute_network.default.self_link
subnetwork = google_compute_subnetwork.default.self_link
service_account = var.service_account
name_prefix = var.network
source_image_family = "cos-stable"
source_image_project = "cos-cloud"
source_image = reverse(split("/", module.gce-container.source_image))[0]
metadata = merge(var.additional_metadata, map("gce-container-declaration", module.gce-container.metadata_value))
tags = [
"container-vm-test-mig"
]
labels = {
"container-vm" = module.gce-container.vm_container_label
}
}

wait_for_instances = true
module "mig" {
source = "terraform-google-modules/vm/google//modules/mig"
version = "~> 1.0.0"
instance_template = module.mig_template.self_link
region = var.region
hostname = var.network
target_size = var.mig_instance_count
named_ports = [
{
name = "http",
port = var.image_port
}]
network = google_compute_network.default.self_link
subnetwork = var.subnetwork
}

module "http-lb" {
source = "github.com/GoogleCloudPlatform/terraform-google-lb-http"
source = "../../../terraform-google-lb-http/"
project = var.project_id
name = "${var.mig_name}-lb"
firewall_networks = []
target_tags = [module.mig.target_tags]
firewall_networks = [
google_compute_network.default.self_link
]
target_tags = local.target_tags

backends = {
"0" = [
Expand All @@ -91,9 +135,11 @@ resource "google_compute_firewall" "lb-to-instances" {

allow {
protocol = "tcp"
ports = [var.image_port]
ports = [
var.image_port
]
}

source_ranges = [local.google_load_balancer_ip_ranges]
target_tags = [module.mig.target_tags]
source_ranges = local.google_load_balancer_ip_ranges
target_tags = local.target_tags
}
5 changes: 0 additions & 5 deletions examples/managed_instance_group/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ output "project_id" {
value = var.project_id
}

output "zone" {
description = "The zone the GCE instance was deployed into"
value = var.zone
}

output "vm_container_label" {
description = "The instance label containing container configuration"
value = module.gce-container.vm_container_label
Expand Down
42 changes: 27 additions & 15 deletions examples/managed_instance_group/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,64 @@

variable "project_id" {
description = "The project ID to deploy resource into"
}

variable "subnetwork_project" {
description = "The project ID where the desired subnetwork is provisioned"
type = string
}

variable "subnetwork" {
description = "The name of the subnetwork to deploy instances into"
type = string
default = "mig-subnet"
}

variable "mig_name" {
description = "The desired name to assign to the deployed managed instance group"
type = string
default = "mig-test"
}

variable "mig_instance_count" {
description = "The number of instances to place in the managed instance group"
type = string
default = "2"
}

variable "image" {
description = "The Docker image to deploy to GCE instances"
type = string
default = "gcr.io/google-samples/hello-app:1.0"
}

variable "image_port" {
description = "The port the image exposes for HTTP requests"
}

variable "restart_policy" {
description = "The desired Docker restart policy for the deployed image"
}

variable "machine_type" {
description = "The GCP machine type to deploy"
type = number
default = 8080
}

variable "region" {
description = "The GCP region to deploy instances into"
type = string
}

variable "zone" {
description = "The GCP zone to deploy instances into"
variable "network" {
description = "The GCP network"
type = string
default = "mig-net"
}

variable "additional_metadata" {
type = "map"
type = map
description = "Additional metadata to attach to the instance"
default = {}
}

variable "service_account" {
type = object({
email = string,
scopes = list(string)
})
default = {
email = ""
scopes = [
"cloud-platform"]
}
}

0 comments on commit b6d0f96

Please sign in to comment.