Skip to content

Instantly share code, notes, and snippets.

@eilgin
Last active November 23, 2017 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eilgin/2439f42ebb33de7aa9a0e7726619fd5c to your computer and use it in GitHub Desktop.
Save eilgin/2439f42ebb33de7aa9a0e7726619fd5c to your computer and use it in GitHub Desktop.
variable "project" {
type = "string"
}
variable "name" {
type = "string"
}
variable "network" {
type = "string"
}
variable "subnet" {
type = "string"
}
variable "env" {
type = "string"
default = ""
}
variable "zone" {
type = "string"
default = "europe-west1-b"
}
variable "service_port" {
type = "string"
description = "Port the service is listening on."
default = "80"
}
variable "service_port_name" {
type = "string"
description = "Name of the port the service is listening on."
default = "http"
}
variable "size" {
type = "string"
description = "Target size of the managed instance group."
default = 3
}
variable "type" {
type = "string"
default = "n1-standard-1"
}
variable "disk_size" {
type = "string"
// in GB
default = "50"
}
variable "disk_type" {
type = "string"
// default to mechanical disk
default = "pd-standard"
}
variable "disk_image" {
type = "string"
// make use of debian stretch
default = "debian-9"
}
variable "can_ip_forward" {
type = "string"
default = false
}
// simulate a "depends_on" since we can't use it atm for module-to-module dependencies
variable "depends_id" {
type = "list"
default = [""]
}
locals {
full_name = "${var.env == "" ? "${var.name}" : "${var.name}-${var.env}"}"
}
resource "google_compute_instance" "private_instance" {
count = "${var.size}"
name = "instance-${random_id.suffix.dec}-${count.index + 1}"
machine_type = "${var.type}"
zone = "${var.zone}"
project = "${var.project}"
tags = ["private-instance"]
boot_disk {
initialize_params {
size = "${var.disk_size}"
type = "${var.disk_type}"
image = "${var.disk_image}"
}
}
network_interface {
subnetwork = "${var.subnet}"
}
can_ip_forward = "${var.can_ip_forward}"
lifecycle {
create_before_destroy = true
}
}
resource "google_compute_instance_group" "default" {
project = "${var.project}"
zone = "${var.zone}"
name = "ig-${local.full_name}"
description = "manages ${var.size} instances in the \"${var.zone}\" zone"
instances = ["${google_compute_instance.private_instance.*.self_link}"]
named_port {
name = "${var.service_port_name}"
port = "${var.service_port}"
}
}
output name {
description = "Pass through of input `name`."
value = "${var.name}"
}
output self_link {
description = "Link to the `instance_group` property of the instance group manager resource."
value = "${google_compute_instance_group.default.self_link}"
}
output service_port {
description = "Pass through of input `service_port`."
value = "${var.service_port}"
}
output service_port_name {
description = "Pass through of input `service_port_name`."
value = "${var.service_port_name}"
}
output instance_ips {
value = "${google_compute_instance.private_instance.*.network_interface.0.address}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment