Merge pull request #5917 from mcalthrop/enhancement/google-two-tier-example

Address #5912: google-two-tier example incomplete
This commit is contained in:
Dave Cunningham 2016-04-01 01:11:17 -04:00
commit c19d84bce7
5 changed files with 72 additions and 16 deletions

View File

@ -11,6 +11,17 @@ getting your application onto the servers. However, you could do so either via
management tool, or by pre-baking configured images with
[Packer](https://packer.io/docs/builders/googlecompute.html).
You will need to generate SSH keys as follows:
```sh
$ ssh-keygen -f ~/.ssh/gcloud_id_rsa
# press <Enter> when asked (twice) for a pass-phrase
```
Then [download your credentials from Google Cloud Console](https://www.terraform.io/docs/providers/google/#credentials); suggested path for downloaded file is `~/.gcloud/Terraform.json`.
Optionally update `variables.tf` to specify a default value for the `project_name` variable, and check other variables.
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:
@ -33,7 +44,7 @@ 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="credentials_file_path=~/.gcloud/Terraform.json" \
-var="public_key_path=~/.ssh/gcloud_id_rsa.pub" \
-var="private_key_path=~/.ssh/gcloud_id_rsa"
```

View File

@ -3,7 +3,7 @@
provider "google" {
region = "${var.region}"
project = "${var.project_name}"
account_file = "${file(var.account_file_path)}"
credentials = "${file("${var.credentials_file_path}")}"
}
resource "google_compute_http_health_check" "default" {
@ -31,31 +31,47 @@ resource "google_compute_instance" "www" {
count = 3
name = "tf-www-${count.index}"
machine_type = "n1-standard-1"
machine_type = "f1-micro"
zone = "${var.region_zone}"
tags = ["www-node"]
disk {
image = "ubuntu-os-cloud/ubuntu-1204-precise-v20150625"
image = "ubuntu-os-cloud/ubuntu-1404-trusty-v20160314"
}
network_interface {
network = "default"
access_config {
# Ephemeral
# Ephemeral
}
}
metadata {
sshKeys = "ubuntu:${file("~/.ssh/gcloud_id_rsa.pub")}"
startup-script = <<SCRIPT
apt-get -y update
apt-get -y install nginx
HOSTNAME=$(hostname | tr -d "\n")
IP=$(curl -s -H "Metadata-Flavor:Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/ip)
echo "Welcome to ${count.index} - $HOSTNAME ($IP)" > /usr/share/nginx/www/index.html
service nginx start
SCRIPT
ssh-keys = "root:${file("${var.public_key_path}")}"
}
provisioner "file" {
source = "${var.install_script_src_path}"
destination = "${var.install_script_dest_path}"
connection {
type = "ssh"
user = "root"
private_key = "${file("${var.private_key_path}")}"
agent = false
}
}
provisioner "remote-exec" {
connection {
type = "ssh"
user = "root"
private_key = "${file("${var.private_key_path}")}"
agent = false
}
inline = [
"chmod +x ${var.install_script_dest_path}",
"${var.install_script_dest_path} ${count.index}"
]
}
service_account {

View File

@ -3,5 +3,5 @@ output "pool_public_ip" {
}
output "instance_ips" {
value = "${join(" ", google_compute_instance.www.*.network_interface.0.access_config.0.nat_ip)}"
value = "${join(" ", google_compute_instance.www.*.network_interface.0.access_config.0.assigned_nat_ip)}"
}

View File

@ -0,0 +1,8 @@
#!/bin/bash -xe
RESOURCE_INDEX=$1
apt-get -y update
apt-get -y install nginx
IP=$(curl -s -H "Metadata-Flavor:Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/ip)
echo "Welcome to Resource ${RESOURCE_INDEX} - ${HOSTNAME} (${IP})" > /usr/share/nginx/html/index.html
service nginx start

View File

@ -10,6 +10,27 @@ variable "project_name" {
description = "The ID of the Google Cloud project"
}
variable "account_file_path" {
variable "credentials_file_path" {
description = "Path to the JSON file used to describe your account credentials"
default = "~/.gcloud/Terraform.json"
}
variable "public_key_path" {
description = "Path to file containing public key"
default = "~/.ssh/gcloud_id_rsa.pub"
}
variable "private_key_path" {
description = "Path to file containing private key"
default = "~/.ssh/gcloud_id_rsa"
}
variable "install_script_src_path" {
description = "Path to install script within this repository"
default = "scripts/install.sh"
}
variable "install_script_dest_path" {
description = "Path to put the install script on each destination resource"
default = "/tmp/install.sh"
}