Last active
August 6, 2020 21:57
-
-
Save timcondit/c5c1227cef1e9e49315a32d574427ef9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# BYO | |
## CREDENTIALS file | |
## PROJECT-ID | |
## REGION | |
## USERNAME | |
## SSH-PUBLIC-KEY-PATH | |
# Configure the Google Cloud provider | |
provider "google" { | |
credentials = file("CREDENTIALS.json") | |
project = "PROJECT-ID" | |
region = "REGION" | |
} | |
# Terraform plugin for creating random ids | |
resource "random_id" "instance_id" { | |
byte_length = 8 | |
} | |
# A single Compute Engine instance | |
resource "google_compute_instance" "default" { | |
name = "flask-vm-${random_id.instance_id.hex}" | |
machine_type = "f1-micro" | |
zone = "REGION-a" | |
boot_disk { | |
initialize_params { | |
image = "debian-cloud/debian-9" | |
} | |
} | |
# Make sure flask is installed on all new instances for later steps | |
metadata_startup_script = <<-EOT | |
sudo apt update | |
sudo apt upgrade | |
sudo apt install -yq build-essential python-pip rsync | |
pip install flask | |
cat <<EOF > /app.py | |
from flask import Flask | |
app = Flask(__name__) | |
@app.route('/') | |
def hello_cloud(): | |
return 'Hello Cloud!' | |
app.run(host='0.0.0.0') | |
EOF | |
/usr/bin/env python /app.py | |
EOT | |
# Add SSH access to the Compute Engine instance | |
metadata = { | |
ssh-keys = "USERNAME:${file("SSH-PUBLIC-KEY-PATH")}" | |
} | |
network_interface { | |
network = "default" | |
access_config { | |
# Include this section to give the VM an external ip address | |
} | |
} | |
} | |
resource "google_compute_firewall" "default" { | |
name = "flask-app-firewall" | |
network = "default" | |
allow { | |
protocol = "tcp" | |
ports = ["5000"] | |
} | |
} | |
output "ip" { | |
value = google_compute_instance.default.network_interface.0.access_config.0.nat_ip | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment