master
greglebreton 1 year ago
commit 566a8a1e26
  1. 8
      app.py
  2. 64
      main.tf
  3. 4
      terraform.tfvars
  4. 17
      variables.tf

@ -0,0 +1,8 @@
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_cloud():
return 'Hello Cloud!'
app.run(host='0.0.0.0')

@ -0,0 +1,64 @@
## TERRAFORM
terraform {
required_version = ">= 0.12"
}
## PROVIDER
provider "google" {
project = var.gcp_project
credentials = file(var.gcp_auth_file)
region = var.gcp_region
}
## NETWORK
resource "google_compute_network" "vpc_network" {
name = "my-custom-network"
auto_create_subnetworks = false
mtu = 1460
}
resource "google_compute_subnetwork" "default" {
name = "my-custom-subnet"
ip_cidr_range = "10.0.1.0/24"
region = var.gcp_region
network = google_compute_network.vpc_network.id
}
# VM
resource "google_compute_instance" "default" {
name = "flask-vm"
machine_type = "e2-micro"
zone = "europe-west9-a"
tags = ["ssh"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
## INSTALL FLASK
metadata_startup_script = "sudo apt-get update; sudo apt-get install -yq build-essential python3-pip rsync; pip install flask"
network_interface {
subnetwork = google_compute_subnetwork.default.id
access_config {
# Include this section to give the VM an external IP address
}
}
}
## SSH
resource "google_compute_firewall" "ssh" {
name = "allow-ssh"
allow {
ports = ["22"]
protocol = "tcp"
}
direction = "INGRESS"
network = google_compute_network.vpc_network.id
priority = 1000
source_ranges = ["0.0.0.0/0"]
target_tags = ["ssh"]
}

@ -0,0 +1,4 @@
# GCP Settings
gcp_project = "terraform-demo-381114"
gcp_region = "europe-west9"
gcp_auth_file = "./auth/terraform-demo-381114-158cfce10778.json"

@ -0,0 +1,17 @@
# GCP authentication file
variable "gcp_auth_file" {
type = string
description = "GCP authentication file"
}
# define GCP region
variable "gcp_region" {
type = string
description = "GCP region"
}
# define GCP project name
variable "gcp_project" {
type = string
description = "GCP project name"
}
Loading…
Cancel
Save