diff --git a/examples/google-two-tier/.gitignore b/examples/google-two-tier/.gitignore new file mode 100644 index 000000000..3f5ca68ad --- /dev/null +++ b/examples/google-two-tier/.gitignore @@ -0,0 +1 @@ +terraform.tfvars diff --git a/examples/google-two-tier/README.md b/examples/google-two-tier/README.md new file mode 100644 index 000000000..d29cf608e --- /dev/null +++ b/examples/google-two-tier/README.md @@ -0,0 +1,39 @@ +# Basic Two-Tier Architecture in Google Cloud + +This provides a template for running a simple two-tier architecture on Google Cloud. +The premise is that you have stateless app servers running behind +a load balancer serving traffic. + +To simplify the example, this intentionally ignores deploying and +getting your application onto the servers. However, you could do so either via +[startup script](http://terraform.io/docs/providers/google/r/compute_instance.html#metadata_startup_script) or +[provisioners](https://www.terraform.io/docs/provisioners/) and a configuration +management tool, or by pre-baking configured images with +[Packer](https://packer.io/docs/builders/googlecompute.html). + +After you run `terraform apply` on this configuration, it will +automatically output the public IP address of the load balancer. +After your instance registers, the LB should respond with a simple header: + +```html +

Welcome to instance 0

+``` + +The index may differ once you increase `count` of `google_compute_instance` +(i.e. provision more instances). + +To run, configure your Google Cloud provider as described in + +https://www.terraform.io/docs/providers/google/index.html + +Run with a command like this: + +``` +terraform apply \ + -var="region=us-central1" \ + -var="region_zone=us-central1-f" \ + -var="project_name=my-project-id-123" \ + -var="account_file_path=~/.gcloud/Terraform.json" \ + -var="public_key_path=~/.ssh/gcloud_id_rsa.pub" \ + -var="private_key_path=~/.ssh/gcloud_id_rsa" +``` diff --git a/examples/google-two-tier/main.tf b/examples/google-two-tier/main.tf new file mode 100644 index 000000000..bf0409e38 --- /dev/null +++ b/examples/google-two-tier/main.tf @@ -0,0 +1,77 @@ +# See https://cloud.google.com/compute/docs/load-balancing/network/example + +provider "google" { + region = "${var.region}" + project = "${var.project_name}" + account_file = "${file(var.account_file_path)}" +} + +resource "google_compute_http_health_check" "default" { + name = "tf-www-basic-check" + request_path = "/" + check_interval_sec = 1 + healthy_threshold = 1 + unhealthy_threshold = 10 + timeout_sec = 1 +} + +resource "google_compute_target_pool" "default" { + name = "tf-www-target-pool" + instances = ["${google_compute_instance.www.*.self_link}"] + health_checks = ["${google_compute_http_health_check.default.name}"] +} + +resource "google_compute_forwarding_rule" "default" { + name = "tf-www-forwarding-rule" + target = "${google_compute_target_pool.default.self_link}" + port_range = "80" +} + +resource "google_compute_instance" "www" { + count = 3 + + name = "tf-www-${count.index}" + machine_type = "n1-standard-1" + zone = "${var.region_zone}" + tags = ["www-node"] + + disk { + image = "ubuntu-os-cloud/ubuntu-1204-precise-v20150625" + } + + network_interface { + network = "default" + access_config { + # Ephemeral + } + } + + metadata { + sshKeys = "ubuntu:${file("~/.ssh/gcloud_id_rsa.pub")}" + startup-script = <