provider "google" { version = "~> 2.0" project = var.project region = var.region } resource "google_compute_network" "hashicat" { name = "${var.prefix}-vpc-${var.region}" auto_create_subnetworks = false } resource "google_compute_subnetwork" "hashicat" { name = "${var.prefix}-subnet" region = var.region network = google_compute_network.hashicat.self_link ip_cidr_range = var.subnet_prefix } resource "google_compute_firewall" "http-server" { name = "default-allow-ssh-http" network = google_compute_network.hashicat.self_link allow { protocol = "tcp" ports = ["22", "80"] } // Allow traffic from everywhere to instances with an http-server tag source_ranges = ["0.0.0.0/0"] target_tags = ["http-server"] } resource "tls_private_key" "ssh-key" { algorithm = "RSA" rsa_bits = "4096" } resource "google_compute_instance" "hashicat" { name = "${var.prefix}-hashicat" zone = "${var.region}-b" machine_type = var.machine_type boot_disk { initialize_params { image = "ubuntu-os-cloud/ubuntu-1804-lts" } } network_interface { subnetwork = google_compute_subnetwork.hashicat.self_link access_config { } } metadata = { ssh-keys = "ubuntu:${chomp(tls_private_key.ssh-key.public_key_openssh)} terraform" } tags = ["http-server"] labels = { name = "hashicat" } } resource "null_resource" "configure-cat-app" { depends_on = [ google_compute_instance.hashicat, ] triggers = { build_number = timestamp() } provisioner "file" { source = "files/" destination = "/home/ubuntu/" connection { type = "ssh" user = "ubuntu" timeout = "300s" private_key = tls_private_key.ssh-key.private_key_pem host = google_compute_instance.hashicat.network_interface.0.access_config.0.nat_ip } } provisioner "remote-exec" { inline = [ "sudo add-apt-repository universe", "sudo apt -y update", "sudo apt -y install apache2", "sudo systemctl start apache2", "sudo chown -R ubuntu:ubuntu /var/www/html", "chmod +x *.sh", "PLACEHOLDER=${var.placeholder} WIDTH=${var.width} HEIGHT=${var.height} PREFIX=${var.prefix} ./deploy_app.sh", "sudo apt -y install cowsay", "cowsay Mooooooooooo!", ] connection { type = "ssh" user = "ubuntu" timeout = "300s" private_key = tls_private_key.ssh-key.private_key_pem host = google_compute_instance.hashicat.network_interface.0.access_config.0.nat_ip } } }