From 566a8a1e26eb92a5721583cd328769583067a778 Mon Sep 17 00:00:00 2001 From: greglebreton Date: Sun, 19 Mar 2023 16:22:22 +0100 Subject: [PATCH] push --- app.py | 8 ++++++ main.tf | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ terraform.tfvars | 4 +++ variables.tf | 17 +++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 app.py create mode 100644 main.tf create mode 100644 terraform.tfvars create mode 100644 variables.tf diff --git a/app.py b/app.py new file mode 100644 index 0000000..bb8243d --- /dev/null +++ b/app.py @@ -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') diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..91a831f --- /dev/null +++ b/main.tf @@ -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"] +} \ No newline at end of file diff --git a/terraform.tfvars b/terraform.tfvars new file mode 100644 index 0000000..0ab279c --- /dev/null +++ b/terraform.tfvars @@ -0,0 +1,4 @@ +# GCP Settings +gcp_project = "terraform-demo-381114" +gcp_region = "europe-west9" +gcp_auth_file = "./auth/terraform-demo-381114-158cfce10778.json" \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..8b3d807 --- /dev/null +++ b/variables.tf @@ -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" +} \ No newline at end of file