diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 00000000..25c3ad0b --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,59 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/google" { + version = "3.68.0" + constraints = "3.68.0" + hashes = [ + "h1:bkeUlWc289dNsjFID2c1MyyuzPJVl9DY4Q5OET5m5EY=", + "zh:1cc538c8aebbf90bf25a667ddce50f9b06ffd8aaa0910e593e9bda7ad4554a6e", + "zh:2db9ac90c377b3bf131cffe93b15b2376a919cfd96464bdf22f67e42534103c8", + "zh:2fd7ada3adeec07aea071a859a34d02a15ecac872ebaadfa320cf308559e6f5f", + "zh:61dd1dcad202508936da07a8810a99c0baafd052e630aff975036e86e3078537", + "zh:84cc28abfabc76d830f187ed3e66957950fd330e3de6f3b8a85bb099d743d7d2", + "zh:9aa9eb2544cd1b763a3e4cf143c1b630f5126fdd9193e9c10eee79d328a0d574", + "zh:abba9c4cad0d02eb434970f37d0eab6722cb81c84de14f2682366cf20125ba5d", + "zh:b94a148a7da8432e75af53c19f61f169859d9d8c5204497779930e825ba76ba8", + "zh:cba5276db512f16aaa90925d60010c434027083067ab0dd9a477f03442b65d66", + "zh:cc5a7109b12d065082c0bf19053afa33d03919c3fd701d77e809522fb4df9291", + "zh:ed3b127c255cf3f819c8892ea283bf866c91f6806db0eda9b67b38c092c2b5ce", + ] +} + +provider "registry.terraform.io/hashicorp/null" { + version = "3.2.2" + hashes = [ + "h1:zT1ZbegaAYHwQa+QwIFugArWikRJI9dqohj8xb0GY88=", + "zh:3248aae6a2198f3ec8394218d05bd5e42be59f43a3a7c0b71c66ec0df08b69e7", + "zh:32b1aaa1c3013d33c245493f4a65465eab9436b454d250102729321a44c8ab9a", + "zh:38eff7e470acb48f66380a73a5c7cdd76cc9b9c9ba9a7249c7991488abe22fe3", + "zh:4c2f1faee67af104f5f9e711c4574ff4d298afaa8a420680b0cb55d7bbc65606", + "zh:544b33b757c0b954dbb87db83a5ad921edd61f02f1dc86c6186a5ea86465b546", + "zh:696cf785090e1e8cf1587499516b0494f47413b43cb99877ad97f5d0de3dc539", + "zh:6e301f34757b5d265ae44467d95306d61bef5e41930be1365f5a8dcf80f59452", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:913a929070c819e59e94bb37a2a253c228f83921136ff4a7aa1a178c7cce5422", + "zh:aa9015926cd152425dbf86d1abdbc74bfe0e1ba3d26b3db35051d7b9ca9f72ae", + "zh:bb04798b016e1e1d49bcc76d62c53b56c88c63d6f2dfe38821afef17c416a0e1", + "zh:c23084e1b23577de22603cff752e59128d83cfecc2e6819edadd8cf7a10af11e", + ] +} + +provider "registry.terraform.io/hashicorp/tls" { + version = "4.0.5" + hashes = [ + "h1:e4LBdJoZJNOQXPWgOAG0UuPBVhCStu98PieNlqJTmeU=", + "zh:01cfb11cb74654c003f6d4e32bbef8f5969ee2856394a96d127da4949c65153e", + "zh:0472ea1574026aa1e8ca82bb6df2c40cd0478e9336b7a8a64e652119a2fa4f32", + "zh:1a8ddba2b1550c5d02003ea5d6cdda2eef6870ece86c5619f33edd699c9dc14b", + "zh:1e3bb505c000adb12cdf60af5b08f0ed68bc3955b0d4d4a126db5ca4d429eb4a", + "zh:6636401b2463c25e03e68a6b786acf91a311c78444b1dc4f97c539f9f78de22a", + "zh:76858f9d8b460e7b2a338c477671d07286b0d287fd2d2e3214030ae8f61dd56e", + "zh:a13b69fb43cb8746793b3069c4d897bb18f454290b496f19d03c3387d1c9a2dc", + "zh:a90ca81bb9bb509063b736842250ecff0f886a91baae8de65c8430168001dad9", + "zh:c4de401395936e41234f1956ebadbd2ed9f414e6908f27d578614aaa529870d4", + "zh:c657e121af8fde19964482997f0de2d5173217274f6997e16389e7707ed8ece8", + "zh:d68b07a67fbd604c38ec9733069fbf23441436fecf554de6c75c032f82e1ef19", + "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", + ] +} diff --git a/files/deploy_app.sh b/files/deploy_app.sh index 9115b73f..9b09415d 100644 --- a/files/deploy_app.sh +++ b/files/deploy_app.sh @@ -14,7 +14,7 @@ cat << EOM > /var/www/html/index.html

Meow World!

- Welcome to ${PREFIX}'s app. Replace this text with your own. + Welcome to sjh's app. Replace this text with your own. diff --git a/main.tf b/main.tf index ac39299d..e540669c 100644 --- a/main.tf +++ b/main.tf @@ -1,121 +1,121 @@ -# Copyright (c) HashiCorp, Inc. -# SPDX-License-Identifier: Apache-2.0 - -terraform { - required_providers { - google = { - source = "hashicorp/google" - version = "=3.68.0" - } - } -} - -provider "google" { - 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 = "${var.prefix}-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 = "ED25519" -} - -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-2204-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 apt -y update", - "sleep 15", - "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 - } - } -} +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: Apache-2.0 + +terraform { + required_providers { + google = { + source = "hashicorp/google" + } + } +} + +provider "google" { + 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 = "${var.prefix}-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 = "ED25519" +} + +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-2204-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" + department = "devops" + } + +} + +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 apt -y update", + "sleep 15", + "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 + } + } +} diff --git a/remote_backend.tf b/remote_backend.tf new file mode 100644 index 00000000..3730d11e --- /dev/null +++ b/remote_backend.tf @@ -0,0 +1,9 @@ +terraform { + backend "remote" { + hostname = "app.terraform.io" + organization = "sjh" + workspaces { + name = "hashicat-gcp" + } + } +} diff --git a/terraform.tfvars.example b/terraform.tfvars.example deleted file mode 100644 index fb073c66..00000000 --- a/terraform.tfvars.example +++ /dev/null @@ -1,5 +0,0 @@ -# Rename or copy this file to terraform.tfvars -# Prefix must be all lowercase letters, digits, and hyphens. -# Make sure it is at least 5 characters long. - -# prefix = "yourname" diff --git a/vpc.tf b/vpc.tf new file mode 100644 index 00000000..1d948e03 --- /dev/null +++ b/vpc.tf @@ -0,0 +1,16 @@ + module "network" { + source = "terraform-google-modules/network/google" + version = "3.4.0" + # insert required variables here + + network_name = "sjh" + project_id = "p-gti8ayw9nnlr-0" + subnets = [ + { + subnet_name = "gaurav-subnet" + subnet_ip = "10.100.10.0/24" + subnet_region = var.region + } + ] + +}