Skip to content

Commit 8be06a0

Browse files
committed
Merge pull request hashicorp#2914 from TimeIncOSS/google-cloud-2-tier-example
google: Add an example of a two-tier app
2 parents c5e8da2 + f8d4a44 commit 8be06a0

6 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terraform.tfvars

examples/google-two-tier/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Basic Two-Tier Architecture in Google Cloud
2+
3+
This provides a template for running a simple two-tier architecture on Google Cloud.
4+
The premise is that you have stateless app servers running behind
5+
a load balancer serving traffic.
6+
7+
To simplify the example, this intentionally ignores deploying and
8+
getting your application onto the servers. However, you could do so either via
9+
[startup script](http://terraform.io/docs/providers/google/r/compute_instance.html#metadata_startup_script) or
10+
[provisioners](https://www.terraform.io/docs/provisioners/) and a configuration
11+
management tool, or by pre-baking configured images with
12+
[Packer](https://packer.io/docs/builders/googlecompute.html).
13+
14+
After you run `terraform apply` on this configuration, it will
15+
automatically output the public IP address of the load balancer.
16+
After your instance registers, the LB should respond with a simple header:
17+
18+
```html
19+
<h1>Welcome to instance 0</h1>
20+
```
21+
22+
The index may differ once you increase `count` of `google_compute_instance`
23+
(i.e. provision more instances).
24+
25+
To run, configure your Google Cloud provider as described in
26+
27+
https://www.terraform.io/docs/providers/google/index.html
28+
29+
Run with a command like this:
30+
31+
```
32+
terraform apply \
33+
-var="region=us-central1" \
34+
-var="region_zone=us-central1-f" \
35+
-var="project_name=my-project-id-123" \
36+
-var="account_file_path=~/.gcloud/Terraform.json" \
37+
-var="public_key_path=~/.ssh/gcloud_id_rsa.pub" \
38+
-var="private_key_path=~/.ssh/gcloud_id_rsa"
39+
```

examples/google-two-tier/main.tf

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# See https://cloud.google.com/compute/docs/load-balancing/network/example
2+
3+
provider "google" {
4+
region = "${var.region}"
5+
project = "${var.project_name}"
6+
account_file = "${file(var.account_file_path)}"
7+
}
8+
9+
resource "google_compute_http_health_check" "default" {
10+
name = "tf-www-basic-check"
11+
request_path = "/"
12+
check_interval_sec = 1
13+
healthy_threshold = 1
14+
unhealthy_threshold = 10
15+
timeout_sec = 1
16+
}
17+
18+
resource "google_compute_target_pool" "default" {
19+
name = "tf-www-target-pool"
20+
instances = ["${google_compute_instance.www.*.self_link}"]
21+
health_checks = ["${google_compute_http_health_check.default.name}"]
22+
}
23+
24+
resource "google_compute_forwarding_rule" "default" {
25+
name = "tf-www-forwarding-rule"
26+
target = "${google_compute_target_pool.default.self_link}"
27+
port_range = "80"
28+
}
29+
30+
resource "google_compute_instance" "www" {
31+
count = 3
32+
33+
name = "tf-www-${count.index}"
34+
machine_type = "n1-standard-1"
35+
zone = "${var.region_zone}"
36+
tags = ["www-node"]
37+
38+
disk {
39+
image = "ubuntu-os-cloud/ubuntu-1204-precise-v20150625"
40+
}
41+
42+
network_interface {
43+
network = "default"
44+
access_config {
45+
# Ephemeral
46+
}
47+
}
48+
49+
metadata {
50+
sshKeys = "ubuntu:${file("~/.ssh/gcloud_id_rsa.pub")}"
51+
startup-script = <<SCRIPT
52+
apt-get -y update
53+
apt-get -y install nginx
54+
HOSTNAME=$(hostname | tr -d "\n")
55+
IP=$(curl -s -H "Metadata-Flavor:Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/ip)
56+
echo "Welcome to ${count.index} - $HOSTNAME ($IP)" > /usr/share/nginx/www/index.html
57+
service nginx start
58+
SCRIPT
59+
}
60+
61+
service_account {
62+
scopes = ["https://www.googleapis.com/auth/compute.readonly"]
63+
}
64+
}
65+
66+
resource "google_compute_firewall" "default" {
67+
name = "tf-www-firewall"
68+
network = "default"
69+
70+
allow {
71+
protocol = "tcp"
72+
ports = ["80"]
73+
}
74+
75+
source_ranges = ["0.0.0.0/0"]
76+
target_tags = ["www-node"]
77+
}

examples/google-two-tier/output.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "pool_public_ip" {
2+
value = "${google_compute_forwarding_rule.default.ip_address}"
3+
}
4+
5+
output "instance_ips" {
6+
value = "${join(" ", google_compute_instance.www.*.network_interface.0.access_config.0.nat_ip)}"
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
region = "us-central1"
2+
region_zone = "us-central1-a"
3+
project_name = "my-project-id-123"
4+
account_file_path = "~/.gcloud/Terraform.json"
5+
public_key_path = "~/.ssh/gcloud_id_rsa.pub"
6+
private_key_path = "~/.ssh/gcloud_id_rsa"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
variable "region" {
2+
default = "us-central1"
3+
}
4+
5+
variable "region_zone" {
6+
default = "us-central1-f"
7+
}
8+
9+
variable "project_name" {
10+
description = "The ID of the Google Cloud project"
11+
}
12+
13+
variable "account_file_path" {
14+
description = "Path to the JSON file used to describe your account credentials"
15+
}

0 commit comments

Comments
 (0)