Simple Terraform demo for GCP to deploy a Flask app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
terraform-demo/main.tf

64 lines
1.4 KiB

1 year ago
## 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"]
}