├── google_cloud ├── env.sh.sample ├── .gitignore ├── app-base.tf ├── provider.tf ├── scripts │ ├── letsencrypt-init │ ├── cloud-init.app │ └── setup-nat-routing.sh ├── variables.tf ├── network-base.tf ├── app-region1-zone1.tf ├── app-region1-zone2.tf ├── letsencrypt.tf ├── network-region1-zone1.tf ├── network-region1-zone2.tf ├── README.md └── loadbalancer.tf ├── minecraft-server ├── digital-ocean │ ├── .gitignore │ ├── provider.tf │ ├── env.sh.sample │ ├── domain.tf │ ├── variables.tf │ ├── README.md │ └── maincraft.tf └── google_cloud │ ├── provider.tf │ ├── Makefile │ ├── terraform.tfstate │ ├── dns.tf.disabled │ ├── minecraft.init │ ├── README.md │ └── main.tf ├── README.md └── LICENSE /google_cloud/env.sh.sample: -------------------------------------------------------------------------------- 1 | export TF_VAR_gce_project="my-project-name" 2 | -------------------------------------------------------------------------------- /google_cloud/.gitignore: -------------------------------------------------------------------------------- 1 | account.json 2 | env.sh 3 | terraform.tfstate 4 | terraform.tfstate.backup 5 | -------------------------------------------------------------------------------- /minecraft-server/digital-ocean/.gitignore: -------------------------------------------------------------------------------- 1 | env.sh 2 | terraform.tfstate 3 | terraform.tfstate.backup 4 | -------------------------------------------------------------------------------- /minecraft-server/digital-ocean/provider.tf: -------------------------------------------------------------------------------- 1 | # Initialize the provider 2 | provider "digitalocean" { 3 | token = "${var.do_token}" 4 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Terraform (terraform.io) Plans 2 | ============================== 3 | 4 | This directory structure contains terraform plans I find useful. -------------------------------------------------------------------------------- /google_cloud/app-base.tf: -------------------------------------------------------------------------------- 1 | variable "gce_machine_type_fantomtest" { 2 | description = "The machine type to use for the Fantomtest" 3 | default = "f1-micro" 4 | } 5 | -------------------------------------------------------------------------------- /google_cloud/provider.tf: -------------------------------------------------------------------------------- 1 | provider "google" { 2 | credentials = "${file("account.json")}" 3 | project = "${var.gce_project}" 4 | region = "${var.gce_region1}" 5 | } 6 | 7 | -------------------------------------------------------------------------------- /minecraft-server/google_cloud/provider.tf: -------------------------------------------------------------------------------- 1 | provider "google" { 2 | credentials = "${file("account.json")}" 3 | project = "${var.gce_project}" 4 | region = "${var.gce_region}" 5 | } 6 | -------------------------------------------------------------------------------- /minecraft-server/digital-ocean/env.sh.sample: -------------------------------------------------------------------------------- 1 | export TF_VAR_do_token="" 2 | # Location of the private key 3 | export TF_VAR_pvt_key="" 4 | # Location of the public key 5 | export TF_VAR_pub_key="" 6 | -------------------------------------------------------------------------------- /minecraft-server/google_cloud/Makefile: -------------------------------------------------------------------------------- 1 | plan: 2 | terraform plan 3 | 4 | create: 5 | terraform apply 6 | 7 | destroy: 8 | terraform destroy -target google_compute_instance.minecraft-server -force 9 | -------------------------------------------------------------------------------- /minecraft-server/google_cloud/terraform.tfstate: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "serial": 0, 4 | "modules": [ 5 | { 6 | "path": [ 7 | "root" 8 | ], 9 | "outputs": {}, 10 | "resources": {} 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /google_cloud/scripts/letsencrypt-init: -------------------------------------------------------------------------------- 1 | # This is only to save time 2 | apt-get update 3 | apt-get install -y apache2 4 | rm -f /var/www/index.html 5 | touch /var/www/index.html 6 | docker pull quay.io/letsencrypt/letsencrypt:latest 7 | 8 | mkdir /root/ssl-keys 9 | echo "email = vlemp@vuksan.com" > /root/ssl-keys/cli.ini 10 | -------------------------------------------------------------------------------- /minecraft-server/digital-ocean/domain.tf: -------------------------------------------------------------------------------- 1 | # You can also update your DNS record assuming you are using Digital Ocean 2 | # for handling DNS. Change the entries here then uncomment this section 3 | #resource "digitalocean_record" "minecraft-domain-com" { 4 | # domain = "domain.com" 5 | # type = "A" 6 | # name = "minecraft" 7 | # value = "${digitalocean_droplet.minecraft.ipv4_address}" 8 | #} 9 | -------------------------------------------------------------------------------- /minecraft-server/google_cloud/dns.tf.disabled: -------------------------------------------------------------------------------- 1 | variable "domain_name" { 2 | description = "Domain Name" 3 | default = "testing12345.xyz" 4 | } 5 | 6 | resource "google_dns_managed_zone" "mydomain" { 7 | name = "mydomain" 8 | dns_name = "${var.domain_name}." 9 | description = "${var.domain_name} DNS zone" 10 | } 11 | 12 | resource "google_dns_record_set" "minecraft-mydomain" { 13 | managed_zone = "${google_dns_managed_zone.mydomain.name}" 14 | name = "minecraft.${google_dns_managed_zone.mydomain.dns_name}" 15 | type = "A" 16 | ttl = 300 17 | rrdatas = ["${google_compute_instance.minecraft-server.network_interface.0.access_config.0.assigned_nat_ip}"] 18 | } 19 | -------------------------------------------------------------------------------- /minecraft-server/digital-ocean/variables.tf: -------------------------------------------------------------------------------- 1 | # API token 2 | variable "do_token" {} 3 | 4 | # Location of the private key 5 | variable "pvt_key" {} 6 | 7 | # Location of the public key (it will be uploaded 8 | variable "pub_key" {} 9 | 10 | # Default image name to use 11 | variable "do_image_name" { 12 | default = "ubuntu-14-04-x64" 13 | } 14 | 15 | # Default image name to use 16 | variable "do_default_size" { 17 | default = "2gb" 18 | } 19 | 20 | # There to launch the instance 21 | variable "do_default_region" { 22 | default = "nyc3" 23 | } 24 | 25 | 26 | # Where to download the Minecraft server JAR 27 | variable "minecraft_server_url" { 28 | default = "https://s3.amazonaws.com/Minecraft.Download/versions/1.9.2/minecraft_server.1.9.2.jar" 29 | } -------------------------------------------------------------------------------- /google_cloud/scripts/cloud-init.app: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | apt-get update 3 | apt-get install -y apache2 libapache2-mod-php7.0 php7.0-curl mtr-tiny 4 | a2enmod php7.0 5 | cd /var/www/html 6 | wget https://github.com/vvuksan/fantomTest/archive/v1.0.tar.gz 7 | tar zxf v1.0.tar.gz 8 | ln -sf fantomTest-1.0 fantomtest 9 | cat < /var/www/html/index.html 10 |

Hello World

11 |
12 | EOF
13 | 
14 | ZONE=`curl metadata/0.1/meta-data/zone | cut -f4 -d/`
15 | MYHOST=`curl metadata/0.1/meta-data/hostname | cut -f1 -d.`
16 | 
17 | echo "$MYHOST-$ZONE" >> /var/www/html/index.html
18 | 
19 | 
20 | cat < /var/www/html/ping.php
21 | 
26 | $MYHOST
27 | EOF
28 | 
29 | 
30 | sudo systemctl restart  apache2
31 | 


--------------------------------------------------------------------------------
/google_cloud/variables.tf:
--------------------------------------------------------------------------------
 1 | # Name of the Google Cloud project to use
 2 | variable "gce_project" { }
 3 | 
 4 | variable "gce_network" {
 5 |   description   = "GCE network to use"
 6 |   default       = "production"
 7 | }
 8 | 
 9 | ############################################################################
10 | # Specify the name of a region and two zones you want your app deployed in
11 | ############################################################################
12 | variable "gce_region1" {
13 |   description   = "GCE Region 1"
14 |   default       = "us-central1"
15 | }
16 | 
17 | # Specify the two zones in region 1
18 | variable "gce_region1_zone1" {
19 |     default     = "us-central1-f"
20 | }
21 | 
22 | variable "gce_region1_zone2" {
23 |     default     = "us-central1-a"
24 | }
25 | 
26 | variable "gce_image" {
27 |   description   = "OS image to boot VMs using"
28 |   default       = "ubuntu-1604-xenial-v20160420c"
29 | }
30 | 


--------------------------------------------------------------------------------
/minecraft-server/digital-ocean/README.md:
--------------------------------------------------------------------------------
 1 | This terraform file creates a minecraft server on Digital Ocean. It creates
 2 | a completely new Minecraft server
 3 | To launch it you will need to define 3 variables
 4 | 
 5 | * Your Digital Ocean API token (do_token)
 6 | * Location of your SSH private key (pvt_key)
 7 | * Location of the matching SSH public key (pub_key)
 8 | 
 9 | There are two ways of doing it.
10 | 
11 | * Rename env.sh.sample to env.sh and configure the variables in there. Then source the file with
12 | 
13 | ```source env.sh```
14 | 
15 | * You can also add them directly into the variables.tf file
16 | 
17 | Once you are done configuring you should type
18 | 
19 | ```terraform plan```
20 | 
21 | to see the plan of execution. It should not error out. If that is looking good type
22 | 
23 | ```terraform apply```
24 | 
25 | to execute it. Once it's all done you should have an instance running in the cloud. To find out 
26 | what IP address it got assigned type
27 | 
28 | ```terraform show | grep ipv4```
29 | 
30 | To destroy it type
31 | 
32 | ```terraform destroy```
33 | 
34 | 
35 | TODO:
36 | 
37 | * Import minecraft config from elsewhere


--------------------------------------------------------------------------------
/google_cloud/scripts/setup-nat-routing.sh:
--------------------------------------------------------------------------------
 1 | #!/bin/bash
 2 | 
 3 | if [ `grep -c 16.04 /etc/issue` -gt 0 ]; then
 4 | 	NAT_DEVICE="ens4"
 5 | else
 6 | 	NAT_DEVICE="eth0"
 7 | fi
 8 | 
 9 | 
10 | # Redirect stdout ( > ) into a log file.
11 | exec > /tmp/setup-nat-routing.log
12 | exec 2>&1
13 | 
14 | set -e
15 | 
16 | echo "NAT: Enabling routing features in the kernel"
17 | echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
18 | echo 0 | sudo tee /proc/sys/net/ipv4/conf/$NAT_DEVICE/send_redirects
19 | sudo mkdir -p /etc/sysctl.d/
20 | echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/nat.conf
21 | echo "net.ipv4.conf.$NAT_DEVICE.send_redirects = 0" | sudo tee -a /etc/sysctl.d/nat.conf
22 | 
23 | echo "NAT: Setting up NAT-MASQUERADE"
24 | sudo iptables -t nat -A POSTROUTING -o $NAT_DEVICE -s 0.0.0.0/0 -j MASQUERADE
25 | 
26 | # Cloudinit changes the package repositories, which can make fail next steps.
27 | # Let's wait for it to finish.
28 | while pgrep cloud-init > /dev/null; do echo "Waiting for cloudinit to finish..."; sleep 1; done
29 | 
30 | echo "Save iptables state"
31 | sudo apt-get update
32 | sudo DEBIAN_FRONTEND=noninteractive apt-get install -y iptables-persistent
33 | 


--------------------------------------------------------------------------------
/google_cloud/network-base.tf:
--------------------------------------------------------------------------------
 1 | variable "gce_machine_type_micro" {
 2 |     description = "The Micro type instances"
 3 |     default     = "f1-micro"
 4 | }
 5 | 
 6 | resource "google_compute_network" "production" {
 7 |     name = "${var.gce_network}"
 8 |     auto_create_subnetworks = true    
 9 | }
10 | 
11 | resource "google_compute_firewall" "inbound-bastion-ssh" {
12 |   name = "inbound-bastion-ssh"
13 |   network = "${google_compute_network.production.name}"
14 | 
15 |   source_ranges     = [ "0.0.0.0/0" ]
16 |   target_tags       = [ "bastion" ]
17 | 
18 |   allow {
19 |     protocol        = "tcp"
20 |     ports           = [ "22" ]
21 |   }
22 | }
23 | 
24 | 
25 | resource "google_compute_firewall" "allow-all-internal" {
26 |   name = "allow-all-internal-${google_compute_network.production.name}"
27 |   network = "${google_compute_network.production.name}"
28 | 
29 |   source_ranges     = [ "10.0.0.0/8" ]
30 |   target_tags       = [ "bastion", "no-ip-${var.gce_region1_zone1}", "no-ip-${var.gce_region1_zone2}", "nat" ]
31 | 
32 |   allow {
33 |     protocol        = "tcp"
34 |   }
35 | 
36 |   allow {
37 |     protocol        = "udp"
38 |   }
39 | 
40 |   allow {
41 |     protocol        = "icmp"
42 |   }
43 | 
44 | }
45 | 
46 | 
47 | 


--------------------------------------------------------------------------------
/minecraft-server/google_cloud/minecraft.init:
--------------------------------------------------------------------------------
 1 | #!/bin/bash
 2 | 
 3 | MINECRAFT_VERSION="1.9.2"
 4 | 
 5 | # Where to download the mine
 6 | MINECRAFT_URL="https://s3.amazonaws.com/Minecraft.Download/versions/$MINECRAFT_VERSION/minecraft_server.$MINECRAFT_VERSION.jar"
 7 | 
 8 | export PATH=$PATH:/usr/bin
 9 | 
10 | # 
11 | DISK="/dev/disk/by-id/google-data-permdisk"
12 | BASE_DIR="/minecraft"
13 | 
14 | RUN_DIR="$BASE_DIR/minecraft_server_1"
15 | 
16 | # Make sure mount point exists
17 | install -d $BASE_DIR
18 | 
19 | /usr/share/google/safe_format_and_mount $DISK $BASE_DIR
20 | 
21 | # Let's create the directory where to install the minecraft server
22 | install -o nobody -d $RUN_DIR
23 | 
24 | if [ ! -f $RUN_DIR/minecraft_server_$MINECRAFT_VERSION.jar ]; then
25 | 
26 |    wget -O $RUN_DIR/minecraft_server_$MINECRAFT_VERSION.jar $MINECRAFT_URL
27 | 
28 |    echo "eula=true" > $RUN_DIR/eula.txt
29 | 
30 | fi
31 | 
32 | apt-get update
33 | apt-get install -y openjdk-8-jre-headless supervisor
34 | 
35 | cat > /etc/supervisor/conf.d/minecraft.conf < /minecraft/eula.txt",
31 |         "echo '[program:minecraft]\nuser=nobody\ndirectory=/minecraft\ncommand=/usr/bin/java -jar /minecraft/minecraft_server.jar\nautostart=true\nautorestart=true\numask=002\npriority=2\nstartretries=3\nstopwaitsecs=10\nstdout_logfile=/minecraft/minecraft.log\nstdout_logfile_maxbytes=0\nstderr_logfile=/minecraft/minecraft_errors.log\nstderr_logfile_maxbytes=0' > /etc/supervisor/conf.d/minecraft.conf",
32 |         "service supervisor restart"
33 |         ]
34 |     }
35 | 
36 | 
37 | }
38 | 
39 | 


--------------------------------------------------------------------------------
/minecraft-server/google_cloud/README.md:
--------------------------------------------------------------------------------
 1 | This is a set of terraform templates to create a Minecraft server. It uses a persistent disk to store state of game play so that you can shut down your compute instance when you are not using it and save money
 2 | 
 3 | What it does
 4 | ============
 5 | 
 6 | * It will create a persistent disk called minecraft-permdisk
 7 | * Create inbound firewall rules that allow SSH access and access to a range of ports you can use for minecraft starting with the standard 25565
 8 | * It will create an compute instance and attach the minecraft-permdisk to it
 9 | * Once the instance is launched it will download the minecraft server JAR file and install the necessary pieces to kick off the server. Please look at minecraft.init file for more details. Inside it you will also be able to change minecraft server versions
10 | 
11 | Requirements
12 | ============
13 | 
14 | * Terraform binary - download it from [https://www.terraform.io/](https://www.terraform.io/)
15 | * Account credentials stored in the account.json file in this directory (you can override that by adjusting variables in provider.tf)
16 | * In main.tf you will need to specify the name of the GCP project you are creating resources in
17 | 
18 | Optional
19 | ========
20 | 
21 | * These is a dns.tf.disabled file that can also update your DNS settings
22 | 
23 | How to use
24 | ==========
25 | 
26 | There is a Makefile inside this directory that makes things easier to use
27 | 
28 | * Create - type make create - this will spin up all the resources.
29 | * Destroy Instance - type make destroy - this will only destroy your compute instance leaving your permanent disk untouched
30 | 


--------------------------------------------------------------------------------
/google_cloud/app-region1-zone1.tf:
--------------------------------------------------------------------------------
 1 | ########################################################################
 2 | # My Compute instance template
 3 | # 
 4 | # It uses the local cloud-init.web file as a startup-script
 5 | ########################################################################
 6 | resource "google_compute_instance_template" "fantomtest_region1_zone1" {
 7 |     name                = "fantomtest-${var.gce_region1_zone1}"
 8 |     machine_type        = "${var.gce_machine_type_fantomtest}"
 9 |     can_ip_forward      = false
10 |     tags                = [ "fantomtest", "no-ip-${var.gce_region1_zone1}" ]
11 | 
12 |     disk {
13 |         source_image    = "${var.gce_image}"
14 |         auto_delete     = true
15 |     }
16 | 
17 |     network_interface {
18 |         network         = "${google_compute_network.production.name}"
19 |     }
20 | 
21 |     metadata {
22 |         startup-script  = "${file("scripts/cloud-init.app")}"
23 |     }
24 | 
25 | }
26 | 
27 | resource "google_compute_instance_group_manager" "fantomtest_region1_zone1" {
28 |     name                = "fantomtest"
29 |     instance_template   = "${google_compute_instance_template.fantomtest_region1_zone1.self_link}"
30 |     target_pools        = ["${google_compute_target_pool.fantomtest_region1_zone1.self_link}"]
31 |     base_instance_name  = "fantomtest"
32 |     zone                = "${var.gce_region1_zone1}"
33 | 
34 |     named_port {
35 |         name            = "http"
36 |         port            = 80
37 |     }
38 | 
39 | }
40 | 
41 | resource "google_compute_autoscaler" "fantomtest_region1_zone1" {
42 |     name                = "fantomtest"
43 |     zone                = "${var.gce_region1_zone1}"
44 |     target              = "${google_compute_instance_group_manager.fantomtest_region1_zone1.self_link}"
45 |     autoscaling_policy = {
46 |         max_replicas    = 5
47 |         min_replicas    = 1
48 |         cooldown_period = 60
49 |         cpu_utilization = {
50 |             target = 0.5
51 |         }
52 |     }
53 | }
54 | 


--------------------------------------------------------------------------------
/google_cloud/app-region1-zone2.tf:
--------------------------------------------------------------------------------
 1 | ########################################################################
 2 | # My Compute instance template
 3 | # 
 4 | # It uses the local cloud-init.web file as a startup-script
 5 | ########################################################################
 6 | resource "google_compute_instance_template" "fantomtest_region1_zone2" {
 7 |     name                = "fantomtest-${var.gce_region1_zone2}"
 8 |     machine_type        = "${var.gce_machine_type_fantomtest}"
 9 |     can_ip_forward      = false
10 |     tags                = [ "fantomtest", "no-ip-${var.gce_region1_zone2}" ]
11 | 
12 |     disk {
13 |         source_image    = "${var.gce_image}"
14 |         auto_delete     = true
15 |     }
16 | 
17 |     network_interface {
18 |         network         = "${google_compute_network.production.name}"
19 |     }
20 | 
21 |     metadata {
22 |         startup-script  = "${file("scripts/cloud-init.app")}"
23 |     }
24 | 
25 | }
26 | 
27 | resource "google_compute_instance_group_manager" "fantomtest_region1_zone2" {
28 |     name                = "fantomtest"
29 |     instance_template   = "${google_compute_instance_template.fantomtest_region1_zone2.self_link}"
30 |     target_pools        = ["${google_compute_target_pool.fantomtest_region1_zone2.self_link}"]
31 |     base_instance_name  = "fantomtest"
32 |     zone                = "${var.gce_region1_zone2}"
33 | 
34 |     named_port {
35 |         name            = "http"
36 |         port            = 80
37 |     }
38 | 
39 | }
40 | 
41 | resource "google_compute_autoscaler" "fantomtest_region1_zone2" {
42 |     name                = "fantomtest"
43 |     zone                = "${var.gce_region1_zone2}"
44 |     target              = "${google_compute_instance_group_manager.fantomtest_region1_zone2.self_link}"
45 |     autoscaling_policy = {
46 |         max_replicas    = 5
47 |         min_replicas    = 1
48 |         cooldown_period = 60
49 |         cpu_utilization = {
50 |             target = 0.5
51 |         }
52 |     }
53 | }
54 | 


--------------------------------------------------------------------------------
/minecraft-server/google_cloud/main.tf:
--------------------------------------------------------------------------------
 1 | variable "gce_project" {
 2 |   description = "GCE Project Name to create machines inside of"
 3 |   default     = "my_project_name"
 4 | }
 5 | 
 6 | variable "gce_region" {
 7 |   description = "GCE Region to use"
 8 |   default     = "us-central1"
 9 | }
10 | 
11 | variable "gce_network" {
12 |   description = "GCE network to use"
13 |   default     = "vladtest-global"
14 | }
15 | 
16 | variable "gce_image_minecraft" {
17 |   description = "The name of the image to base the launched instances."
18 |   default     = "ubuntu-1604-xenial-v20160420c"
19 | }
20 | 
21 | variable "gce_region_zone" {
22 |   description = "GCE Region to use"
23 | 
24 |   # us-central1-[a,b,c,f], us-east1-[a,b,c]
25 |   default = "us-central1-f"
26 | }
27 | 
28 | variable "gce_machine_type_minecraft" {
29 |   description = "The machine type to use for minecraft"
30 |   default     = "g1-small"
31 | }
32 | 
33 | resource "google_compute_firewall" "inbound-minecraft" {
34 |   name    = "inbound-minecraft"
35 |   network = "${var.gce_network}"
36 | 
37 |   source_ranges = ["0.0.0.0/0"]
38 |   target_tags   = ["minecraft"]
39 | 
40 |   allow {
41 |     protocol = "tcp"
42 |     ports    = ["22", "25565-26000"]
43 |   }
44 | }
45 | 
46 | # Create a persistent disk to store Minecraft server data
47 | resource "google_compute_disk" "minecraft-permdisk" {
48 |   name = "minecraft-permdisk"
49 |   size = "10"
50 |   zone = "${var.gce_region_zone}"
51 | }
52 | 
53 | # Create a new minecraft server
54 | resource "google_compute_instance" "minecraft-server" {
55 |   zone         = "${var.gce_region_zone}"
56 |   name         = "minecraft"
57 |   tags         = ["minecraft"]
58 |   machine_type = "${var.gce_machine_type_minecraft}"
59 | 
60 |   disk {
61 |     image       = "${var.gce_image_minecraft}"
62 |     auto_delete = true
63 |   }
64 | 
65 |   # Attach the permanent disk defined above
66 |   disk {
67 |     disk        = "${google_compute_disk.minecraft-permdisk.name}"
68 |     device_name = "data-permdisk"
69 |     auto_delete = false
70 |   }
71 | 
72 |   network_interface {
73 |     network       = "${var.gce_network}"
74 |     access_config = {
75 |     }
76 |   }
77 | 
78 |   metadata_startup_script = "${file("minecraft.init")}"
79 | }
80 | 


--------------------------------------------------------------------------------
/google_cloud/letsencrypt.tf:
--------------------------------------------------------------------------------
 1 | variable "gce_image_le" {
 2 |     description         = "The name of the image for Let's Encrypt."
 3 |     default             = "google-containers/container-vm-v20160321"
 4 | }
 5 | 
 6 | # Create an instance template
 7 | resource "google_compute_instance_template" "lets-encrypt-instance-template" {
 8 |     name                = "lets-encrypt-instance-template"
 9 |     machine_type        = "${var.gce_machine_type_fantomtest}"
10 |     can_ip_forward      = false
11 |     tags                = [ "letsencrypt", "no-ip-${var.gce_region1_zone1}" ]
12 | 
13 |     disk {
14 |         source_image    = "${var.gce_image_le}"
15 |         auto_delete     = true
16 |     }
17 | 
18 |     network_interface {
19 |         network         = "${google_compute_network.production.name}"
20 |     }
21 | 
22 |     metadata {
23 |         startup-script  = "${file("scripts/letsencrypt-init")}"
24 |     }
25 | 
26 | }
27 | 
28 | resource "google_compute_instance_group_manager" "lets-encrypt-igm" {
29 |     name                = "lets-encrypt-igm"
30 |     instance_template   = "${google_compute_instance_template.lets-encrypt-instance-template.self_link}"
31 |     base_instance_name  = "letsencrypt"
32 |     zone                = "${var.gce_region1_zone1}"
33 | 
34 |     named_port {
35 |         name            = "http"
36 |         port            = 80
37 |     }
38 | 
39 | }
40 | 
41 | resource "google_compute_autoscaler" "lets-encrypt-as" {
42 |     name                = "lets-encrypt-as"
43 |     zone                = "${var.gce_region1_zone1}"
44 |     target              = "${google_compute_instance_group_manager.lets-encrypt-igm.self_link}"
45 |     autoscaling_policy = {
46 |         max_replicas    = 1
47 |         min_replicas    = 1
48 |         cooldown_period = 60
49 |         cpu_utilization = {
50 |             target = 0.5
51 |         }
52 |     }
53 | }
54 | 
55 | resource "google_compute_backend_service" "lets-encrypt-backend-service" {
56 |     name                = "lets-encrypt-backend-service"
57 |     port_name           = "http"
58 |     protocol            = "HTTP"
59 |     timeout_sec         = 10
60 |     region              = "${var.gce_region1}"
61 | 
62 |     backend {
63 |         group           = "${google_compute_instance_group_manager.lets-encrypt-igm.instance_group}"
64 |     }
65 | 
66 |     health_checks       = ["${google_compute_http_health_check.fantomtest.self_link}"]    
67 |     
68 | }


--------------------------------------------------------------------------------
/google_cloud/network-region1-zone1.tf:
--------------------------------------------------------------------------------
 1 | ##########################################################################
 2 | # Create a NAT instance - region 1 zone 1
 3 | ##########################################################################
 4 | resource "google_compute_instance" "nat-region1-zone1" {
 5 |     zone            = "${var.gce_region1_zone1}"
 6 |     name            = "nat-${var.gce_region1_zone1}"
 7 |     tags            = [ "${google_compute_network.production.name}", "nat", "public"]
 8 |     machine_type    = "${var.gce_machine_type_micro}"
 9 |     disk {
10 |         image       = "${var.gce_image}"
11 |         auto_delete = true
12 |     }
13 | 
14 |     network_interface {
15 |         network     = "${google_compute_network.production.name}"
16 |         access_config {
17 |             # Ephemeral IP
18 |         }
19 |     }
20 | 
21 |     metadata_startup_script = "${file("scripts/setup-nat-routing.sh")}"
22 |     
23 |     can_ip_forward = true
24 | 
25 | }
26 | 
27 | ##########################################################################
28 | # Create a Bastion instance - region 1 zone 1
29 | ##########################################################################
30 | resource "google_compute_instance" "bastion-region1-zone1" {
31 |     zone            = "${var.gce_region1_zone1}"
32 |     name            = "bastion-${var.gce_region1_zone1}"
33 |     tags            = [ "${google_compute_network.production.name}", "bastion", "public"]
34 |     machine_type    = "${var.gce_machine_type_micro}"
35 |     disk {
36 |         image       = "${var.gce_image}"
37 |         auto_delete = true
38 |     }
39 | 
40 |     network_interface {
41 |         network     = "${google_compute_network.production.name}"
42 |         access_config {
43 |             # Ephemeral IP
44 |         }
45 |     }
46 | 
47 | }
48 | 
49 | ########################################################################
50 | # Route
51 | ########################################################################
52 | resource "google_compute_route" "default-nat-region1-zone1" {
53 |     name            = "default-nat-route-${var.gce_region1_zone1}"
54 |     dest_range      = "0.0.0.0/0"
55 |     network         = "${google_compute_network.production.name}"
56 |     next_hop_ip     = "${google_compute_instance.nat-region1-zone1.network_interface.0.address}"
57 |     priority        = 100
58 |     tags            = [ "no-ip-${var.gce_region1_zone1}" ]
59 | }
60 | 


--------------------------------------------------------------------------------
/google_cloud/network-region1-zone2.tf:
--------------------------------------------------------------------------------
 1 | ##########################################################################
 2 | # Create a NAT instance - region 1 zone 1
 3 | ##########################################################################
 4 | resource "google_compute_instance" "nat-region1-zone2" {
 5 |     zone            = "${var.gce_region1_zone2}"
 6 |     name            = "nat-${var.gce_region1_zone2}"
 7 |     tags            = [ "${google_compute_network.production.name}", "nat", "public"]
 8 |     machine_type    = "${var.gce_machine_type_micro}"
 9 |     disk {
10 |         image       = "${var.gce_image}"
11 |         auto_delete = true
12 |     }
13 | 
14 |     network_interface {
15 |         network     = "${google_compute_network.production.name}"
16 |         access_config {
17 |             # Ephemeral IP
18 |         }
19 |     }
20 | 
21 |     metadata_startup_script = "${file("scripts/setup-nat-routing.sh")}"
22 |     
23 |     can_ip_forward = true
24 | 
25 | }
26 | 
27 | ##########################################################################
28 | # Create a Bastion instance - region 1 zone 1
29 | ##########################################################################
30 | resource "google_compute_instance" "bastion-region1-zone2" {
31 |     zone            = "${var.gce_region1_zone2}"
32 |     name            = "bastion-${var.gce_region1_zone2}"
33 |     tags            = [ "${google_compute_network.production.name}", "bastion", "public"]
34 |     machine_type    = "${var.gce_machine_type_micro}"
35 |     disk {
36 |         image       = "${var.gce_image}"
37 |         auto_delete = true
38 |     }
39 | 
40 |     network_interface {
41 |         network     = "${google_compute_network.production.name}"
42 |         access_config {
43 |             # Ephemeral IP
44 |         }
45 |     }
46 | 
47 | }
48 | 
49 | ########################################################################
50 | # Route
51 | ########################################################################
52 | resource "google_compute_route" "default-nat-region1-zone2" {
53 |     name            = "default-nat-route-${var.gce_region1_zone2}"
54 |     dest_range      = "0.0.0.0/0"
55 |     network         = "${google_compute_network.production.name}"
56 |     next_hop_ip     = "${google_compute_instance.nat-region1-zone2.network_interface.0.address}"
57 |     priority        = 100
58 |     tags            = [ "no-ip-${var.gce_region1_zone2}" ]
59 | }
60 | 


--------------------------------------------------------------------------------
/google_cloud/README.md:
--------------------------------------------------------------------------------
 1 | Google Cloud Terraform Scripts
 2 | ==============================
 3 | 
 4 | This is intended as a POC of deploying an application in two different availability zones
 5 | inside the Google Cloud. Due to the fact that terraform doesn't allow nested variables
 6 | and resource names cannot contain variables we'll use static naming where we'll refer
 7 | to zones and regions as region1 and region1_zone1 and slot the actual zone name in 
 8 | variables.tf file e.g. gce_region1_zone1 is us-central1-f whereas gce_region1_zone2 is
 9 | us-central1-a.
10 | 
11 | It will do following
12 | 
13 | * Create a network called production (defined as gce_network variable in variables.tf)
14 | * Create two separate bastion and NAT gateways in two separate availability 
15 |   These can be changed by changing gce_region1_zone1 and gce_region1_zone2 variables in variables.tf
16 | * Firewall rules we'll be added that enable access to SSH for both bastions from anywhere
17 | * Firewall rules that allow internal traffic as well as traffic from the load balancers to the App
18 | * it will create two different autoscalers in the two above defined zones and use a startup
19 |   script cloud-init.app to bootstrap them. Those two apps will have no public ephemeral
20 |   IPs but will use the NAT gateway in their appropriate availability zones
21 | * Load balancer and URL map will be created that balances traffic across both autoscalers
22 | * Another autoscaler will be created to handle Let's Encrypt [http://blog.vuksan.com/2016/04/18/google-compute-load-balancer-lets-encrypt-integration/](integration)
23 | 
24 | App that is being deployed is Fantomtest from https://github.com/vvuksan/fantomtest. Once it's all configured
25 | you should be able to access the app by using
26 | 
27 | http:///fantomtest/
28 | 
29 | To launch it you will need to define following variables
30 | 
31 | * gce_project needs to be set to name of your project in GCP
32 | 
33 | There are two ways of doing it.
34 | 
35 | * Rename env.sh.sample to env.sh and configure the variables in there. Then source the file with
36 | 
37 | ```source env.sh```
38 | 
39 | * You can also add them directly into the variables.tf file
40 | 
41 | Once you are done configuring you should type
42 | 
43 | ```terraform plan```
44 | 
45 | to see the plan of execution. It should not error out. If that is looking good type
46 | 
47 | ```terraform apply```
48 | 
49 | to execute it. Once it's all done you should have an instance running in the cloud. To find out 
50 | what IP address it got assigned type
51 | 
52 | ```terraform show | grep ipv4```
53 | 
54 | To destroy it all
55 | 
56 | ```terraform destroy```
57 | 
58 | 
59 | 


--------------------------------------------------------------------------------
/google_cloud/loadbalancer.tf:
--------------------------------------------------------------------------------
 1 | #######################################################################
 2 | # Allow Google LB to connect to our web instances
 3 | #######################################################################
 4 | resource "google_compute_firewall" "allow-google-lb" {
 5 |     name            = "allow-google-lb"
 6 |     network         = "${google_compute_network.production.name}"
 7 | 
 8 |     allow {
 9 |         protocol    = "tcp"
10 |         ports       = [ "80", "443" ]
11 |     }
12 | 
13 |     target_tags     = [ "fantomtest", "letsencrypt" ]
14 |     source_ranges   = [ "130.211.0.0/22" ]
15 | 
16 | }
17 | 
18 | resource "google_compute_target_pool" "fantomtest_region1_zone1" {
19 |     name                = "fantomtest-region1-zone1"
20 | }
21 | 
22 | resource "google_compute_target_pool" "fantomtest_region1_zone2" {
23 |     name                = "fantomtest-region1-zone2"
24 | }
25 | 
26 | ############################################################################
27 | # Create a health check
28 | ############################################################################
29 | resource "google_compute_http_health_check" "fantomtest" {
30 |     name                = "fantomtest"
31 |     request_path        = "/"
32 |     check_interval_sec  = 15
33 |     timeout_sec         = 1
34 | }
35 | 
36 | 
37 | resource "google_compute_backend_service" "fantomtest" {
38 |     name                = "fantomtest"
39 |     description         = "Fantomtest"
40 |     port_name           = "http"
41 |     protocol            = "HTTP"
42 |     timeout_sec         = 10
43 |     region              = "us-central1"
44 | 
45 |     # Specify all backend applicable
46 |     backend {
47 |         group           = "${google_compute_instance_group_manager.fantomtest_region1_zone1.instance_group}"
48 |     }
49 | 
50 |     backend {
51 |         group           = "${google_compute_instance_group_manager.fantomtest_region1_zone2.instance_group}"
52 |     }
53 | 
54 |     health_checks       = ["${google_compute_http_health_check.fantomtest.self_link}"]
55 | }
56 | 
57 | 
58 | resource "google_compute_url_map" "fantomtest" {
59 |     name                = "fantomtest-url-map"
60 |     description         = "Fantomtest URL map"
61 |     default_service     = "${google_compute_backend_service.fantomtest.self_link}"
62 |     
63 |     # Add Letsencrypt
64 |     host_rule {
65 |         hosts           = ["*"]
66 |         path_matcher    = "letsencrypt-paths"
67 |     }
68 | 
69 |     path_matcher {
70 |         default_service = "${google_compute_backend_service.fantomtest.self_link}"
71 |         name            = "letsencrypt-paths"
72 |         path_rule {
73 |             paths       = ["/.well-known/*"]
74 |             service     = "${google_compute_backend_service.lets-encrypt-backend-service.self_link}"
75 |         }
76 |     }    
77 |     
78 | }
79 | 
80 | resource "google_compute_target_http_proxy" "fantomtest" {
81 |     name                = "fantomtest-http-proxy"
82 |     description         = "Fantomtest HTTP proxy"
83 |     url_map             = "${google_compute_url_map.fantomtest.self_link}"
84 | }
85 | 
86 | #
87 | resource "google_compute_global_forwarding_rule" "fantomtest-http-forwarding-rule" {
88 |     name                = "fantomtest-http-forward-rule"
89 |     target              = "${google_compute_target_http_proxy.fantomtest.self_link}"
90 |     port_range          = "80"
91 | }
92 | 
93 | 


--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
  1 |                                  Apache License
  2 |                            Version 2.0, January 2004
  3 |                         http://www.apache.org/licenses/
  4 | 
  5 |    TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
  6 | 
  7 |    1. Definitions.
  8 | 
  9 |       "License" shall mean the terms and conditions for use, reproduction,
 10 |       and distribution as defined by Sections 1 through 9 of this document.
 11 | 
 12 |       "Licensor" shall mean the copyright owner or entity authorized by
 13 |       the copyright owner that is granting the License.
 14 | 
 15 |       "Legal Entity" shall mean the union of the acting entity and all
 16 |       other entities that control, are controlled by, or are under common
 17 |       control with that entity. For the purposes of this definition,
 18 |       "control" means (i) the power, direct or indirect, to cause the
 19 |       direction or management of such entity, whether by contract or
 20 |       otherwise, or (ii) ownership of fifty percent (50%) or more of the
 21 |       outstanding shares, or (iii) beneficial ownership of such entity.
 22 | 
 23 |       "You" (or "Your") shall mean an individual or Legal Entity
 24 |       exercising permissions granted by this License.
 25 | 
 26 |       "Source" form shall mean the preferred form for making modifications,
 27 |       including but not limited to software source code, documentation
 28 |       source, and configuration files.
 29 | 
 30 |       "Object" form shall mean any form resulting from mechanical
 31 |       transformation or translation of a Source form, including but
 32 |       not limited to compiled object code, generated documentation,
 33 |       and conversions to other media types.
 34 | 
 35 |       "Work" shall mean the work of authorship, whether in Source or
 36 |       Object form, made available under the License, as indicated by a
 37 |       copyright notice that is included in or attached to the work
 38 |       (an example is provided in the Appendix below).
 39 | 
 40 |       "Derivative Works" shall mean any work, whether in Source or Object
 41 |       form, that is based on (or derived from) the Work and for which the
 42 |       editorial revisions, annotations, elaborations, or other modifications
 43 |       represent, as a whole, an original work of authorship. For the purposes
 44 |       of this License, Derivative Works shall not include works that remain
 45 |       separable from, or merely link (or bind by name) to the interfaces of,
 46 |       the Work and Derivative Works thereof.
 47 | 
 48 |       "Contribution" shall mean any work of authorship, including
 49 |       the original version of the Work and any modifications or additions
 50 |       to that Work or Derivative Works thereof, that is intentionally
 51 |       submitted to Licensor for inclusion in the Work by the copyright owner
 52 |       or by an individual or Legal Entity authorized to submit on behalf of
 53 |       the copyright owner. For the purposes of this definition, "submitted"
 54 |       means any form of electronic, verbal, or written communication sent
 55 |       to the Licensor or its representatives, including but not limited to
 56 |       communication on electronic mailing lists, source code control systems,
 57 |       and issue tracking systems that are managed by, or on behalf of, the
 58 |       Licensor for the purpose of discussing and improving the Work, but
 59 |       excluding communication that is conspicuously marked or otherwise
 60 |       designated in writing by the copyright owner as "Not a Contribution."
 61 | 
 62 |       "Contributor" shall mean Licensor and any individual or Legal Entity
 63 |       on behalf of whom a Contribution has been received by Licensor and
 64 |       subsequently incorporated within the Work.
 65 | 
 66 |    2. Grant of Copyright License. Subject to the terms and conditions of
 67 |       this License, each Contributor hereby grants to You a perpetual,
 68 |       worldwide, non-exclusive, no-charge, royalty-free, irrevocable
 69 |       copyright license to reproduce, prepare Derivative Works of,
 70 |       publicly display, publicly perform, sublicense, and distribute the
 71 |       Work and such Derivative Works in Source or Object form.
 72 | 
 73 |    3. Grant of Patent License. Subject to the terms and conditions of
 74 |       this License, each Contributor hereby grants to You a perpetual,
 75 |       worldwide, non-exclusive, no-charge, royalty-free, irrevocable
 76 |       (except as stated in this section) patent license to make, have made,
 77 |       use, offer to sell, sell, import, and otherwise transfer the Work,
 78 |       where such license applies only to those patent claims licensable
 79 |       by such Contributor that are necessarily infringed by their
 80 |       Contribution(s) alone or by combination of their Contribution(s)
 81 |       with the Work to which such Contribution(s) was submitted. If You
 82 |       institute patent litigation against any entity (including a
 83 |       cross-claim or counterclaim in a lawsuit) alleging that the Work
 84 |       or a Contribution incorporated within the Work constitutes direct
 85 |       or contributory patent infringement, then any patent licenses
 86 |       granted to You under this License for that Work shall terminate
 87 |       as of the date such litigation is filed.
 88 | 
 89 |    4. Redistribution. You may reproduce and distribute copies of the
 90 |       Work or Derivative Works thereof in any medium, with or without
 91 |       modifications, and in Source or Object form, provided that You
 92 |       meet the following conditions:
 93 | 
 94 |       (a) You must give any other recipients of the Work or
 95 |           Derivative Works a copy of this License; and
 96 | 
 97 |       (b) You must cause any modified files to carry prominent notices
 98 |           stating that You changed the files; and
 99 | 
100 |       (c) You must retain, in the Source form of any Derivative Works
101 |           that You distribute, all copyright, patent, trademark, and
102 |           attribution notices from the Source form of the Work,
103 |           excluding those notices that do not pertain to any part of
104 |           the Derivative Works; and
105 | 
106 |       (d) If the Work includes a "NOTICE" text file as part of its
107 |           distribution, then any Derivative Works that You distribute must
108 |           include a readable copy of the attribution notices contained
109 |           within such NOTICE file, excluding those notices that do not
110 |           pertain to any part of the Derivative Works, in at least one
111 |           of the following places: within a NOTICE text file distributed
112 |           as part of the Derivative Works; within the Source form or
113 |           documentation, if provided along with the Derivative Works; or,
114 |           within a display generated by the Derivative Works, if and
115 |           wherever such third-party notices normally appear. The contents
116 |           of the NOTICE file are for informational purposes only and
117 |           do not modify the License. You may add Your own attribution
118 |           notices within Derivative Works that You distribute, alongside
119 |           or as an addendum to the NOTICE text from the Work, provided
120 |           that such additional attribution notices cannot be construed
121 |           as modifying the License.
122 | 
123 |       You may add Your own copyright statement to Your modifications and
124 |       may provide additional or different license terms and conditions
125 |       for use, reproduction, or distribution of Your modifications, or
126 |       for any such Derivative Works as a whole, provided Your use,
127 |       reproduction, and distribution of the Work otherwise complies with
128 |       the conditions stated in this License.
129 | 
130 |    5. Submission of Contributions. Unless You explicitly state otherwise,
131 |       any Contribution intentionally submitted for inclusion in the Work
132 |       by You to the Licensor shall be under the terms and conditions of
133 |       this License, without any additional terms or conditions.
134 |       Notwithstanding the above, nothing herein shall supersede or modify
135 |       the terms of any separate license agreement you may have executed
136 |       with Licensor regarding such Contributions.
137 | 
138 |    6. Trademarks. This License does not grant permission to use the trade
139 |       names, trademarks, service marks, or product names of the Licensor,
140 |       except as required for reasonable and customary use in describing the
141 |       origin of the Work and reproducing the content of the NOTICE file.
142 | 
143 |    7. Disclaimer of Warranty. Unless required by applicable law or
144 |       agreed to in writing, Licensor provides the Work (and each
145 |       Contributor provides its Contributions) on an "AS IS" BASIS,
146 |       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 |       implied, including, without limitation, any warranties or conditions
148 |       of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 |       PARTICULAR PURPOSE. You are solely responsible for determining the
150 |       appropriateness of using or redistributing the Work and assume any
151 |       risks associated with Your exercise of permissions under this License.
152 | 
153 |    8. Limitation of Liability. In no event and under no legal theory,
154 |       whether in tort (including negligence), contract, or otherwise,
155 |       unless required by applicable law (such as deliberate and grossly
156 |       negligent acts) or agreed to in writing, shall any Contributor be
157 |       liable to You for damages, including any direct, indirect, special,
158 |       incidental, or consequential damages of any character arising as a
159 |       result of this License or out of the use or inability to use the
160 |       Work (including but not limited to damages for loss of goodwill,
161 |       work stoppage, computer failure or malfunction, or any and all
162 |       other commercial damages or losses), even if such Contributor
163 |       has been advised of the possibility of such damages.
164 | 
165 |    9. Accepting Warranty or Additional Liability. While redistributing
166 |       the Work or Derivative Works thereof, You may choose to offer,
167 |       and charge a fee for, acceptance of support, warranty, indemnity,
168 |       or other liability obligations and/or rights consistent with this
169 |       License. However, in accepting such obligations, You may act only
170 |       on Your own behalf and on Your sole responsibility, not on behalf
171 |       of any other Contributor, and only if You agree to indemnify,
172 |       defend, and hold each Contributor harmless for any liability
173 |       incurred by, or claims asserted against, such Contributor by reason
174 |       of your accepting any such warranty or additional liability.
175 | 
176 |    END OF TERMS AND CONDITIONS
177 | 
178 |    APPENDIX: How to apply the Apache License to your work.
179 | 
180 |       To apply the Apache License to your work, attach the following
181 |       boilerplate notice, with the fields enclosed by brackets "{}"
182 |       replaced with your own identifying information. (Don't include
183 |       the brackets!)  The text should be enclosed in the appropriate
184 |       comment syntax for the file format. We also recommend that a
185 |       file or class name and description of purpose be included on the
186 |       same "printed page" as the copyright notice for easier
187 |       identification within third-party archives.
188 | 
189 |    Copyright {yyyy} {name of copyright owner}
190 | 
191 |    Licensed under the Apache License, Version 2.0 (the "License");
192 |    you may not use this file except in compliance with the License.
193 |    You may obtain a copy of the License at
194 | 
195 |        http://www.apache.org/licenses/LICENSE-2.0
196 | 
197 |    Unless required by applicable law or agreed to in writing, software
198 |    distributed under the License is distributed on an "AS IS" BASIS,
199 |    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 |    See the License for the specific language governing permissions and
201 |    limitations under the License.
202 | 


--------------------------------------------------------------------------------