diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3211b33..f6341bb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -68,6 +68,34 @@ jobs: tofu init -backend=false -input=false -lockfile=readonly tofu validate -no-color + # Validate the STAGE network stack. + - name: Validate STAGE network + working-directory: environments/stage/network + run: | + tofu init -backend=false -input=false -lockfile=readonly + tofu validate -no-color + + # Validate the STAGE frontend stack. + - name: Validate STAGE frontend + working-directory: environments/stage/frontend + run: | + tofu init -backend=false -input=false -lockfile=readonly + tofu validate -no-color + + # Validate the PROD network stack. + - name: Validate PROD network + working-directory: environments/prod/network + run: | + tofu init -backend=false -input=false -lockfile=readonly + tofu validate -no-color + + # Validate the PROD frontend stack. + - name: Validate PROD frontend + working-directory: environments/prod/frontend + run: | + tofu init -backend=false -input=false -lockfile=readonly + tofu validate -no-color + # Install a pinned TFLint version for static code analysis. - name: Setup TFLint uses: terraform-linters/setup-tflint@v6 diff --git a/environments/prod/frontend/.terraform.lock.hcl b/environments/prod/frontend/.terraform.lock.hcl new file mode 100644 index 0000000..e5c5f89 --- /dev/null +++ b/environments/prod/frontend/.terraform.lock.hcl @@ -0,0 +1,75 @@ +# This file is maintained automatically by "tofu init". +# Manual edits may be lost in future updates. + +provider "registry.opentofu.org/hashicorp/cloudinit" { + version = "2.4.1" + constraints = "~> 2.0" + hashes = [ + "h1:+W0yP9aNwhxG6GkkvAvXAzR3c6oHFrsn0qKzCcd+Pt4=", + "h1:5//sVDkThzeCAUR3avaWF5QkFGoGNHQVohk/03XlwDY=", + "h1:ALxQFbm3LDD9sM9hnJpml3vjEgwwHlfWkynA3QULKzs=", + "h1:IglLKQ1PLLykMGHI5kJgs1KRkQGBHFbQGgwB8d2Evmc=", + "h1:Jp8AyBJHKvSuYUmzImcUJIPasR/dWpPFKHbYYrBkZIk=", + "h1:Q1wbRg2gy6rfc+02mx3am1X4XiQ3t3ohWrD0HhGps3U=", + "h1:QCJhqLabUHdUfcFCxT5RyKrIVyMH20vhvmcwlA3LKnM=", + "h1:SaRx0UjptcMTQe7RAscgvL4yWlniE9fozAY1n8rkWl0=", + "h1:U/s/xR8Qye5WW1Cktz8+J++rHRJCXy29EFXPLSGnxSQ=", + "h1:a3EYZ6Sk+eVf/AuEm8P/vXzHPLr8TfZoVJl2QLaULBQ=", + "h1:m+a2xKJJoawR7H3FF+gFOlBMt80VGnUIDBBCU5Fcp/E=", + "h1:tdO7350ZjYClrjddqhEzXEsX3duMOeKbxWGpRroqDNk=", + "h1:vBTV7h6otZu+5CUa5Mom6qRbGBol1KTgUSW48Z8LnY4=", + "h1:yELihyb80B8udu3NsTq9OYOirr+8+eToSe1ubKecDf8=", + "h1:zKTS0SR/2LWKBAIcUl/yTZulOzKsaYf7qajYARh+yns=", + "zh:1d4e8e1a6c9e87d2f481950d44d907353d8407c74fad3effd1e340a96b2a8ebf", + "zh:1e6070731a6b27d336d67a0a89f954e91efa410d6e309f871e1eb119068852ed", + "zh:2dc72816f28d66de9c7d36f9245dae11810c94321ec9ab54958e94cb077b3129", + "zh:375b260afb24a068e8ea3f9711c995d61eb90eafd8c2f1622a2792c098c67c0c", + "zh:3d7aa8f836560393e3e45243fbcf0a6eb1de429794897439cc9e174c89d2c515", + "zh:3eabd95f6cec88e4190dc1b4c5c1b437343da85633b5803722a08d870d2a203f", + "zh:44672aa5eb610f846af5dfa705b6a8339aea82648b64a4247c0e55a8be590695", + "zh:491020b29c7efdb2288b61babdbd8467315cf3b4e9cf903335cf6f6095d80898", + "zh:520cd99f2c25d0aa22846e650c96f7800837a7e4e666a47e1e42c8e99d3423bd", + "zh:8342d7961404864b0fa4bb36dc686b5e04cbf8771e407cba961c44b8ca3158f0", + "zh:9330950100da6c4a04a6d4806407ab9ab686e079bd7475cff08f7f646cfbd440", + "zh:e12f3bfbee3534b9b0f3310fa80697ef0cca0ff473b6a5c99e457758a366c786", + "zh:eebe47e46ccbff9acf987c653032460e4e572248b1aa7eafce8e340f2120217a", + "zh:ef75fe7352b12b5e5fc968cb9993a94f81101b12766ada290cf1509fb202823f", + "zh:f3b5a1d9fae6e42d8a40cdbb31d6dbbf59a837a7e351b049dfe38474fc26a0ac", + ] +} + +provider "registry.opentofu.org/terraform-provider-openstack/openstack" { + version = "3.4.0" + constraints = "~> 3.0" + hashes = [ + "h1:BB0YFgWM3KZTFY16pCYIzBqM4IpmDpT03m29VZEV2OU=", + "h1:CalEmc/Tn9xv1Xx2bX92EUdsF8z6Lldmb4/gwT5lSRw=", + "h1:DwSYyG1VNcEMUVY1GE9gbo2C3UPS8W2bFGk4WGVJnDY=", + "h1:FpAT7j83xnW4SkiUL6A0eXo9V1bYbABt1Eh467INBgk=", + "h1:FqJxdGUazKAfnZa11qT7h5PstlSTeE4xTKPUiWEgPmg=", + "h1:GgJWG1fXiDuRBcdDuzOWRW0TMW1Lx5N8O6+80v17LZc=", + "h1:HsPiqtbovSqh8jHAuMDR3tNeRNCOBEeHCvRbkgAHiaY=", + "h1:IwC35geJo3X96yLwIqcjSzoLL1Nt+6qJA6BoBMlGgMw=", + "h1:MVSoVvhjbu7s1pfYfsiYED8A++XfAoyOlSX1x9PW68E=", + "h1:YMwKmPy+svf02jWyflqaPaQVX3eIXciwGeA8uwYgRBw=", + "h1:n1yIddr5lsLoYBhlfUmCwIYiCsynDkLvzAE98Tceo/A=", + "h1:nON9j1AYtfbOAmaml0lcPCfEQcb0OLB0K693HjdFggQ=", + "h1:rqRC/cidHw8t2vQb85cjMoWThGSCWnkEmTmmNSaGBVs=", + "h1:tzU+JfFe/RN+7swp0k5V5wUmuZtRNOjKX+spIGcRuKM=", + "zh:11b3c88e24197a29b13cf5ab41771944bd16707b561645323e8cbb4f1da00b7b", + "zh:1c3e89cf19118fc07d7b04257251fc9897e722c16e0a0df7b07fcd261f8c12e7", + "zh:2dc60dbbbb822fbb1e7b17e3b89e3e6a7a8fe54a716d4477d2fa40f836c4de0f", + "zh:68f851e333f0ab2367e9b2680739d47e4151ca9f803934ef265f6fa70a28e6e0", + "zh:6995b0bcae5a291d7b0a4d3a5df109eb97f8d1d34be6eb27768fe29b5e0d8def", + "zh:7b68bf81891a0445c543629d4d160226a6f2d5e8e5ee4baf21e0221412437d17", + "zh:8f117b8b17280598a92c6202b183a77168fe573397cfb597b9c2028da281846d", + "zh:b0a23b990eb9260e9616fd8471f11b37485f92df0e5eef46cd286f87396b0e0a", + "zh:baa5052ab3207f3a996c1fd715935d79a4e1c57d3d24e3c549b6c20ea9307e38", + "zh:c012cbda058aa7b76b718074fd73829ecd4009ad98e3dafe496e5f26099625c0", + "zh:c1c95e5dbe546a935a62c63814aef04718b58dd4258e5da40d681b43322924e9", + "zh:c720bdbdf432041f99572bac40d24659239b989cbf1540f92ebb441900ac8bae", + "zh:c92ac802a3dd1abbf9c9ccf0aba1f261fe9f8c2e7fafc196c7119b73c25a0772", + "zh:fd4faf25ee72acef4ab12c93ea9c7a5ccb41315e8851609e08b37d3c91ea6584", + "zh:ff347392412fc7950635e2c1c5bb878d88b661c1c5ba052edd290d971a14028f", + ] +} diff --git a/environments/prod/frontend/cloud_init/nginx.yaml b/environments/prod/frontend/cloud_init/nginx.yaml new file mode 100644 index 0000000..0d5e3f6 --- /dev/null +++ b/environments/prod/frontend/cloud_init/nginx.yaml @@ -0,0 +1,8 @@ +#cloud-config +package_update: true + +packages: + - nginx + +runcmd: + - systemctl enable --now nginx \ No newline at end of file diff --git a/environments/prod/frontend/data.tf b/environments/prod/frontend/data.tf new file mode 100644 index 0000000..1398568 --- /dev/null +++ b/environments/prod/frontend/data.tf @@ -0,0 +1,8 @@ +data "terraform_remote_state" "network" { + backend = "kubernetes" + + config = { + namespace = "iac" + secret_suffix = "assignment2-prod-network" + } +} \ No newline at end of file diff --git a/environments/prod/frontend/main.tf b/environments/prod/frontend/main.tf new file mode 100644 index 0000000..af3ffa1 --- /dev/null +++ b/environments/prod/frontend/main.tf @@ -0,0 +1,31 @@ + +module "frontend_vm" { + source = "../../../modules/vm" + count = var.frontend_count + + name = format("%s-%02d", var.frontend_name, count.index + 1) + distro = var.frontend_image + flavor = var.frontend_flavor + ssh_key_name = var.ssh_key_name + + network_id = data.terraform_remote_state.network.outputs.network_id + subnet_id = data.terraform_remote_state.network.outputs.frontend_subnet_id + + template = "${path.module}/cloud_init/nginx.yaml" + + enable_fip = true + ntnu_internal_network = var.external_network_name + + security_group_rules = [ + { + port = 22 + protocol = "tcp" + remote_ip_prefix = var.admin_cidr + }, + { + port = 80 + protocol = "tcp" + remote_ip_prefix = var.http_cidr + } + ] +} \ No newline at end of file diff --git a/environments/prod/frontend/outputs.tf b/environments/prod/frontend/outputs.tf new file mode 100644 index 0000000..208b393 --- /dev/null +++ b/environments/prod/frontend/outputs.tf @@ -0,0 +1,24 @@ +output "frontend_vm_ids" { + description = "IDs of the PROD frontend VMs" + value = module.frontend_vm[*].vm_id +} + +output "frontend_vm_names" { + description = "Names of the PROD frontend VMs" + value = module.frontend_vm[*].vm_name +} + +output "frontend_private_ips" { + description = "Private IP addresses of the PROD frontend VMs" + value = module.frontend_vm[*].private_ip +} + +output "frontend_floating_ips" { + description = "Floating IP addresses of the PROD frontend VMs" + value = module.frontend_vm[*].floating_ip +} + +output "frontend_security_group_ids" { + description = "Security group IDs of the PROD frontend VMs" + value = module.frontend_vm[*].security_group_id +} \ No newline at end of file diff --git a/environments/prod/frontend/providers.tf b/environments/prod/frontend/providers.tf new file mode 100644 index 0000000..f34559b --- /dev/null +++ b/environments/prod/frontend/providers.tf @@ -0,0 +1,20 @@ +terraform { + required_version = ">= 1.12.0" + + required_providers { + openstack = { + source = "terraform-provider-openstack/openstack" + version = "~> 3" + } + + cloudinit = { + source = "hashicorp/cloudinit" + version = "~> 2" + } + } + + backend "kubernetes" { + namespace = "iac" + secret_suffix = "assignment2-prod-frontend" + } +} \ No newline at end of file diff --git a/environments/prod/frontend/terraform.tfvars.example b/environments/prod/frontend/terraform.tfvars.example new file mode 100644 index 0000000..e1bc45b --- /dev/null +++ b/environments/prod/frontend/terraform.tfvars.example @@ -0,0 +1,17 @@ +# Frontend VM +frontend_name = "assignment2-prod-frontend" +frontend_count = 1 + +# Image and flavor +frontend_image = "Debian 13 (Trixie) stable amd64" +frontend_flavor = "gx1.1c1r" + +# SSH +ssh_key_name = "YOUR_SSH_KEYPAIR_NAME" + +# Security group rules +admin_cidr = "YOUR_ADMIN_CIDR" +http_cidr = "YOUR_HTTP_CIDR" + +# External network +external_network_name = "ntnu-internal" \ No newline at end of file diff --git a/environments/prod/frontend/variables.tf b/environments/prod/frontend/variables.tf new file mode 100644 index 0000000..ab87701 --- /dev/null +++ b/environments/prod/frontend/variables.tf @@ -0,0 +1,58 @@ +variable "frontend_name" { + description = "Name of the frontend VM" + type = string +} + +variable "frontend_image" { + description = "OpenStack image used for the frontend VM" + type = string + default = "Debian 13 (Trixie) stable amd64" +} + +variable "frontend_flavor" { + description = "OpenStack flavor used for the frontend VM" + type = string + default = "gx1.1c1r" +} + +variable "ssh_key_name" { + description = "SSH keypair available in SkyHiGh" + type = string +} + +variable "admin_cidr" { + description = "CIDR range allowed to access the frontend VM via SSH" + type = string + + validation { + condition = can(cidrhost(var.admin_cidr, 0)) + error_message = "admin_cidr must be a valid CIDR block." + } +} + +variable "http_cidr" { + description = "CIDR range allowed to access the frontend VM via HTTP" + type = string + + validation { + condition = can(cidrhost(var.http_cidr, 0)) + error_message = "http_cidr must be a valid CIDR block." + } +} + +variable "external_network_name" { + description = "Name of the external network used for floating IP allocation" + type = string + default = "ntnu-internal" +} + +variable "frontend_count" { + description = "Number of frontend VMs to create" + type = number + default = 1 + + validation { + condition = var.frontend_count >= 1 && floor(var.frontend_count) == var.frontend_count + error_message = "frontend_count must be a positive integer." + } +} \ No newline at end of file diff --git a/environments/prod/network/.terraform.lock.hcl b/environments/prod/network/.terraform.lock.hcl new file mode 100644 index 0000000..5de7e39 --- /dev/null +++ b/environments/prod/network/.terraform.lock.hcl @@ -0,0 +1,38 @@ +# This file is maintained automatically by "tofu init". +# Manual edits may be lost in future updates. + +provider "registry.opentofu.org/terraform-provider-openstack/openstack" { + version = "3.4.0" + constraints = "~> 3.0" + hashes = [ + "h1:BB0YFgWM3KZTFY16pCYIzBqM4IpmDpT03m29VZEV2OU=", + "h1:CalEmc/Tn9xv1Xx2bX92EUdsF8z6Lldmb4/gwT5lSRw=", + "h1:DwSYyG1VNcEMUVY1GE9gbo2C3UPS8W2bFGk4WGVJnDY=", + "h1:FpAT7j83xnW4SkiUL6A0eXo9V1bYbABt1Eh467INBgk=", + "h1:FqJxdGUazKAfnZa11qT7h5PstlSTeE4xTKPUiWEgPmg=", + "h1:GgJWG1fXiDuRBcdDuzOWRW0TMW1Lx5N8O6+80v17LZc=", + "h1:HsPiqtbovSqh8jHAuMDR3tNeRNCOBEeHCvRbkgAHiaY=", + "h1:IwC35geJo3X96yLwIqcjSzoLL1Nt+6qJA6BoBMlGgMw=", + "h1:MVSoVvhjbu7s1pfYfsiYED8A++XfAoyOlSX1x9PW68E=", + "h1:YMwKmPy+svf02jWyflqaPaQVX3eIXciwGeA8uwYgRBw=", + "h1:n1yIddr5lsLoYBhlfUmCwIYiCsynDkLvzAE98Tceo/A=", + "h1:nON9j1AYtfbOAmaml0lcPCfEQcb0OLB0K693HjdFggQ=", + "h1:rqRC/cidHw8t2vQb85cjMoWThGSCWnkEmTmmNSaGBVs=", + "h1:tzU+JfFe/RN+7swp0k5V5wUmuZtRNOjKX+spIGcRuKM=", + "zh:11b3c88e24197a29b13cf5ab41771944bd16707b561645323e8cbb4f1da00b7b", + "zh:1c3e89cf19118fc07d7b04257251fc9897e722c16e0a0df7b07fcd261f8c12e7", + "zh:2dc60dbbbb822fbb1e7b17e3b89e3e6a7a8fe54a716d4477d2fa40f836c4de0f", + "zh:68f851e333f0ab2367e9b2680739d47e4151ca9f803934ef265f6fa70a28e6e0", + "zh:6995b0bcae5a291d7b0a4d3a5df109eb97f8d1d34be6eb27768fe29b5e0d8def", + "zh:7b68bf81891a0445c543629d4d160226a6f2d5e8e5ee4baf21e0221412437d17", + "zh:8f117b8b17280598a92c6202b183a77168fe573397cfb597b9c2028da281846d", + "zh:b0a23b990eb9260e9616fd8471f11b37485f92df0e5eef46cd286f87396b0e0a", + "zh:baa5052ab3207f3a996c1fd715935d79a4e1c57d3d24e3c549b6c20ea9307e38", + "zh:c012cbda058aa7b76b718074fd73829ecd4009ad98e3dafe496e5f26099625c0", + "zh:c1c95e5dbe546a935a62c63814aef04718b58dd4258e5da40d681b43322924e9", + "zh:c720bdbdf432041f99572bac40d24659239b989cbf1540f92ebb441900ac8bae", + "zh:c92ac802a3dd1abbf9c9ccf0aba1f261fe9f8c2e7fafc196c7119b73c25a0772", + "zh:fd4faf25ee72acef4ab12c93ea9c7a5ccb41315e8851609e08b37d3c91ea6584", + "zh:ff347392412fc7950635e2c1c5bb878d88b661c1c5ba052edd290d971a14028f", + ] +} diff --git a/environments/prod/network/main.tf b/environments/prod/network/main.tf new file mode 100644 index 0000000..2f85eef --- /dev/null +++ b/environments/prod/network/main.tf @@ -0,0 +1,17 @@ +module "network" { + source = "../../../modules/network" + + network_name = var.network_name + + frontend_subnet_name = var.frontend_subnet_name + frontend_subnet_cidr = var.frontend_subnet_cidr + + backend_subnet_name = var.backend_subnet_name + backend_subnet_cidr = var.backend_subnet_cidr + + database_subnet_name = var.database_subnet_name + database_subnet_cidr = var.database_subnet_cidr + + router_name = var.router_name + external_network_name = var.external_network_name +} \ No newline at end of file diff --git a/environments/prod/network/outputs.tf b/environments/prod/network/outputs.tf new file mode 100644 index 0000000..d697299 --- /dev/null +++ b/environments/prod/network/outputs.tf @@ -0,0 +1,24 @@ +output "network_id" { + description = "ID of the PROD network" + value = module.network.network_id +} + +output "frontend_subnet_id" { + description = "ID of the PROD frontend subnet" + value = module.network.frontend_subnet_id +} + +output "backend_subnet_id" { + description = "ID of the PROD backend subnet" + value = module.network.backend_subnet_id +} + +output "database_subnet_id" { + description = "ID of the PROD database subnet" + value = module.network.database_subnet_id +} + +output "router_id" { + description = "ID of the PROD router" + value = module.network.router_id +} \ No newline at end of file diff --git a/environments/prod/network/providers.tf b/environments/prod/network/providers.tf new file mode 100644 index 0000000..763b0dd --- /dev/null +++ b/environments/prod/network/providers.tf @@ -0,0 +1,17 @@ +terraform { + required_version = ">= 1.12.0" + + required_providers { + openstack = { + source = "terraform-provider-openstack/openstack" + version = "~> 3" + } + } + + backend "kubernetes" { + namespace = "iac" + secret_suffix = "assignment2-prod-network" + } +} + +provider "openstack" {} \ No newline at end of file diff --git a/environments/prod/network/terraform.tfvars.example b/environments/prod/network/terraform.tfvars.example new file mode 100644 index 0000000..68ae360 --- /dev/null +++ b/environments/prod/network/terraform.tfvars.example @@ -0,0 +1,18 @@ +# Network +network_name = "assignment2-prod-network" + +# Frontend subnet +frontend_subnet_name = "assignment2-prod-frontend-subnet" +frontend_subnet_cidr = "10.80.10.0/24" + +# Backend subnet +backend_subnet_name = "assignment2-prod-backend-subnet" +backend_subnet_cidr = "10.80.20.0/24" + +# Database subnet +database_subnet_name = "assignment2-prod-database-subnet" +database_subnet_cidr = "10.80.30.0/24" + +# Router +router_name = "assignment2-prod-router" +external_network_name = "ntnu-internal" \ No newline at end of file diff --git a/environments/prod/network/variables.tf b/environments/prod/network/variables.tf new file mode 100644 index 0000000..ce21774 --- /dev/null +++ b/environments/prod/network/variables.tf @@ -0,0 +1,60 @@ +variable "network_name" { + description = "Name of the network" + type = string +} + +variable "frontend_subnet_name" { + description = "Name of the frontend subnet" + type = string +} + +variable "frontend_subnet_cidr" { + description = "CIDR block for the frontend subnet" + type = string + + validation { + condition = can(cidrhost(var.frontend_subnet_cidr, 0)) + error_message = "frontend_subnet_cidr must be a valid CIDR block." + } +} + +variable "backend_subnet_name" { + description = "Name of the backend subnet" + type = string +} + +variable "backend_subnet_cidr" { + description = "CIDR block for the backend subnet" + type = string + + validation { + condition = can(cidrhost(var.backend_subnet_cidr, 0)) + error_message = "backend_subnet_cidr must be a valid CIDR block." + } +} + +variable "database_subnet_name" { + description = "Name of the database subnet" + type = string +} + +variable "database_subnet_cidr" { + description = "CIDR block for the database subnet" + type = string + + validation { + condition = can(cidrhost(var.database_subnet_cidr, 0)) + error_message = "database_subnet_cidr must be a valid CIDR block." + } +} + +variable "router_name" { + description = "Name of the router" + type = string +} + +variable "external_network_name" { + description = "Name of the external network" + type = string + default = "ntnu-internal" +} \ No newline at end of file diff --git a/environments/stage/frontend/.terraform.lock.hcl b/environments/stage/frontend/.terraform.lock.hcl new file mode 100644 index 0000000..e5c5f89 --- /dev/null +++ b/environments/stage/frontend/.terraform.lock.hcl @@ -0,0 +1,75 @@ +# This file is maintained automatically by "tofu init". +# Manual edits may be lost in future updates. + +provider "registry.opentofu.org/hashicorp/cloudinit" { + version = "2.4.1" + constraints = "~> 2.0" + hashes = [ + "h1:+W0yP9aNwhxG6GkkvAvXAzR3c6oHFrsn0qKzCcd+Pt4=", + "h1:5//sVDkThzeCAUR3avaWF5QkFGoGNHQVohk/03XlwDY=", + "h1:ALxQFbm3LDD9sM9hnJpml3vjEgwwHlfWkynA3QULKzs=", + "h1:IglLKQ1PLLykMGHI5kJgs1KRkQGBHFbQGgwB8d2Evmc=", + "h1:Jp8AyBJHKvSuYUmzImcUJIPasR/dWpPFKHbYYrBkZIk=", + "h1:Q1wbRg2gy6rfc+02mx3am1X4XiQ3t3ohWrD0HhGps3U=", + "h1:QCJhqLabUHdUfcFCxT5RyKrIVyMH20vhvmcwlA3LKnM=", + "h1:SaRx0UjptcMTQe7RAscgvL4yWlniE9fozAY1n8rkWl0=", + "h1:U/s/xR8Qye5WW1Cktz8+J++rHRJCXy29EFXPLSGnxSQ=", + "h1:a3EYZ6Sk+eVf/AuEm8P/vXzHPLr8TfZoVJl2QLaULBQ=", + "h1:m+a2xKJJoawR7H3FF+gFOlBMt80VGnUIDBBCU5Fcp/E=", + "h1:tdO7350ZjYClrjddqhEzXEsX3duMOeKbxWGpRroqDNk=", + "h1:vBTV7h6otZu+5CUa5Mom6qRbGBol1KTgUSW48Z8LnY4=", + "h1:yELihyb80B8udu3NsTq9OYOirr+8+eToSe1ubKecDf8=", + "h1:zKTS0SR/2LWKBAIcUl/yTZulOzKsaYf7qajYARh+yns=", + "zh:1d4e8e1a6c9e87d2f481950d44d907353d8407c74fad3effd1e340a96b2a8ebf", + "zh:1e6070731a6b27d336d67a0a89f954e91efa410d6e309f871e1eb119068852ed", + "zh:2dc72816f28d66de9c7d36f9245dae11810c94321ec9ab54958e94cb077b3129", + "zh:375b260afb24a068e8ea3f9711c995d61eb90eafd8c2f1622a2792c098c67c0c", + "zh:3d7aa8f836560393e3e45243fbcf0a6eb1de429794897439cc9e174c89d2c515", + "zh:3eabd95f6cec88e4190dc1b4c5c1b437343da85633b5803722a08d870d2a203f", + "zh:44672aa5eb610f846af5dfa705b6a8339aea82648b64a4247c0e55a8be590695", + "zh:491020b29c7efdb2288b61babdbd8467315cf3b4e9cf903335cf6f6095d80898", + "zh:520cd99f2c25d0aa22846e650c96f7800837a7e4e666a47e1e42c8e99d3423bd", + "zh:8342d7961404864b0fa4bb36dc686b5e04cbf8771e407cba961c44b8ca3158f0", + "zh:9330950100da6c4a04a6d4806407ab9ab686e079bd7475cff08f7f646cfbd440", + "zh:e12f3bfbee3534b9b0f3310fa80697ef0cca0ff473b6a5c99e457758a366c786", + "zh:eebe47e46ccbff9acf987c653032460e4e572248b1aa7eafce8e340f2120217a", + "zh:ef75fe7352b12b5e5fc968cb9993a94f81101b12766ada290cf1509fb202823f", + "zh:f3b5a1d9fae6e42d8a40cdbb31d6dbbf59a837a7e351b049dfe38474fc26a0ac", + ] +} + +provider "registry.opentofu.org/terraform-provider-openstack/openstack" { + version = "3.4.0" + constraints = "~> 3.0" + hashes = [ + "h1:BB0YFgWM3KZTFY16pCYIzBqM4IpmDpT03m29VZEV2OU=", + "h1:CalEmc/Tn9xv1Xx2bX92EUdsF8z6Lldmb4/gwT5lSRw=", + "h1:DwSYyG1VNcEMUVY1GE9gbo2C3UPS8W2bFGk4WGVJnDY=", + "h1:FpAT7j83xnW4SkiUL6A0eXo9V1bYbABt1Eh467INBgk=", + "h1:FqJxdGUazKAfnZa11qT7h5PstlSTeE4xTKPUiWEgPmg=", + "h1:GgJWG1fXiDuRBcdDuzOWRW0TMW1Lx5N8O6+80v17LZc=", + "h1:HsPiqtbovSqh8jHAuMDR3tNeRNCOBEeHCvRbkgAHiaY=", + "h1:IwC35geJo3X96yLwIqcjSzoLL1Nt+6qJA6BoBMlGgMw=", + "h1:MVSoVvhjbu7s1pfYfsiYED8A++XfAoyOlSX1x9PW68E=", + "h1:YMwKmPy+svf02jWyflqaPaQVX3eIXciwGeA8uwYgRBw=", + "h1:n1yIddr5lsLoYBhlfUmCwIYiCsynDkLvzAE98Tceo/A=", + "h1:nON9j1AYtfbOAmaml0lcPCfEQcb0OLB0K693HjdFggQ=", + "h1:rqRC/cidHw8t2vQb85cjMoWThGSCWnkEmTmmNSaGBVs=", + "h1:tzU+JfFe/RN+7swp0k5V5wUmuZtRNOjKX+spIGcRuKM=", + "zh:11b3c88e24197a29b13cf5ab41771944bd16707b561645323e8cbb4f1da00b7b", + "zh:1c3e89cf19118fc07d7b04257251fc9897e722c16e0a0df7b07fcd261f8c12e7", + "zh:2dc60dbbbb822fbb1e7b17e3b89e3e6a7a8fe54a716d4477d2fa40f836c4de0f", + "zh:68f851e333f0ab2367e9b2680739d47e4151ca9f803934ef265f6fa70a28e6e0", + "zh:6995b0bcae5a291d7b0a4d3a5df109eb97f8d1d34be6eb27768fe29b5e0d8def", + "zh:7b68bf81891a0445c543629d4d160226a6f2d5e8e5ee4baf21e0221412437d17", + "zh:8f117b8b17280598a92c6202b183a77168fe573397cfb597b9c2028da281846d", + "zh:b0a23b990eb9260e9616fd8471f11b37485f92df0e5eef46cd286f87396b0e0a", + "zh:baa5052ab3207f3a996c1fd715935d79a4e1c57d3d24e3c549b6c20ea9307e38", + "zh:c012cbda058aa7b76b718074fd73829ecd4009ad98e3dafe496e5f26099625c0", + "zh:c1c95e5dbe546a935a62c63814aef04718b58dd4258e5da40d681b43322924e9", + "zh:c720bdbdf432041f99572bac40d24659239b989cbf1540f92ebb441900ac8bae", + "zh:c92ac802a3dd1abbf9c9ccf0aba1f261fe9f8c2e7fafc196c7119b73c25a0772", + "zh:fd4faf25ee72acef4ab12c93ea9c7a5ccb41315e8851609e08b37d3c91ea6584", + "zh:ff347392412fc7950635e2c1c5bb878d88b661c1c5ba052edd290d971a14028f", + ] +} diff --git a/environments/stage/frontend/cloud_init/nginx.yaml b/environments/stage/frontend/cloud_init/nginx.yaml new file mode 100644 index 0000000..0d5e3f6 --- /dev/null +++ b/environments/stage/frontend/cloud_init/nginx.yaml @@ -0,0 +1,8 @@ +#cloud-config +package_update: true + +packages: + - nginx + +runcmd: + - systemctl enable --now nginx \ No newline at end of file diff --git a/environments/stage/frontend/data.tf b/environments/stage/frontend/data.tf new file mode 100644 index 0000000..3659694 --- /dev/null +++ b/environments/stage/frontend/data.tf @@ -0,0 +1,8 @@ +data "terraform_remote_state" "network" { + backend = "kubernetes" + + config = { + namespace = "iac" + secret_suffix = "assignment2-stage-network" + } +} \ No newline at end of file diff --git a/environments/stage/frontend/main.tf b/environments/stage/frontend/main.tf new file mode 100644 index 0000000..af3ffa1 --- /dev/null +++ b/environments/stage/frontend/main.tf @@ -0,0 +1,31 @@ + +module "frontend_vm" { + source = "../../../modules/vm" + count = var.frontend_count + + name = format("%s-%02d", var.frontend_name, count.index + 1) + distro = var.frontend_image + flavor = var.frontend_flavor + ssh_key_name = var.ssh_key_name + + network_id = data.terraform_remote_state.network.outputs.network_id + subnet_id = data.terraform_remote_state.network.outputs.frontend_subnet_id + + template = "${path.module}/cloud_init/nginx.yaml" + + enable_fip = true + ntnu_internal_network = var.external_network_name + + security_group_rules = [ + { + port = 22 + protocol = "tcp" + remote_ip_prefix = var.admin_cidr + }, + { + port = 80 + protocol = "tcp" + remote_ip_prefix = var.http_cidr + } + ] +} \ No newline at end of file diff --git a/environments/stage/frontend/outputs.tf b/environments/stage/frontend/outputs.tf new file mode 100644 index 0000000..f110b26 --- /dev/null +++ b/environments/stage/frontend/outputs.tf @@ -0,0 +1,24 @@ +output "frontend_vm_ids" { + description = "IDs of the STAGE frontend VMs" + value = module.frontend_vm[*].vm_id +} + +output "frontend_vm_names" { + description = "Names of the STAGE frontend VMs" + value = module.frontend_vm[*].vm_name +} + +output "frontend_private_ips" { + description = "Private IP addresses of the STAGE frontend VMs" + value = module.frontend_vm[*].private_ip +} + +output "frontend_floating_ips" { + description = "Floating IP addresses of the STAGE frontend VMs" + value = module.frontend_vm[*].floating_ip +} + +output "frontend_security_group_ids" { + description = "Security group IDs of the STAGE frontend VMs" + value = module.frontend_vm[*].security_group_id +} \ No newline at end of file diff --git a/environments/stage/frontend/providers.tf b/environments/stage/frontend/providers.tf new file mode 100644 index 0000000..336a656 --- /dev/null +++ b/environments/stage/frontend/providers.tf @@ -0,0 +1,20 @@ +terraform { + required_version = ">= 1.12.0" + + required_providers { + openstack = { + source = "terraform-provider-openstack/openstack" + version = "~> 3" + } + + cloudinit = { + source = "hashicorp/cloudinit" + version = "~> 2" + } + } + + backend "kubernetes" { + namespace = "iac" + secret_suffix = "assignment2-stage-frontend" + } +} \ No newline at end of file diff --git a/environments/stage/frontend/terraform.tfvars.example b/environments/stage/frontend/terraform.tfvars.example new file mode 100644 index 0000000..c8883d9 --- /dev/null +++ b/environments/stage/frontend/terraform.tfvars.example @@ -0,0 +1,17 @@ +# Frontend VM +frontend_name = "assignment2-stage-frontend" +frontend_count = 1 + +# Image and flavor +frontend_image = "Debian 13 (Trixie) stable amd64" +frontend_flavor = "gx1.1c1r" + +# SSH +ssh_key_name = "YOUR_SSH_KEYPAIR_NAME" + +# Security group rules +admin_cidr = "YOUR_ADMIN_CIDR" +http_cidr = "YOUR_HTTP_CIDR" + +# External network +external_network_name = "ntnu-internal" \ No newline at end of file diff --git a/environments/stage/frontend/variables.tf b/environments/stage/frontend/variables.tf new file mode 100644 index 0000000..ab87701 --- /dev/null +++ b/environments/stage/frontend/variables.tf @@ -0,0 +1,58 @@ +variable "frontend_name" { + description = "Name of the frontend VM" + type = string +} + +variable "frontend_image" { + description = "OpenStack image used for the frontend VM" + type = string + default = "Debian 13 (Trixie) stable amd64" +} + +variable "frontend_flavor" { + description = "OpenStack flavor used for the frontend VM" + type = string + default = "gx1.1c1r" +} + +variable "ssh_key_name" { + description = "SSH keypair available in SkyHiGh" + type = string +} + +variable "admin_cidr" { + description = "CIDR range allowed to access the frontend VM via SSH" + type = string + + validation { + condition = can(cidrhost(var.admin_cidr, 0)) + error_message = "admin_cidr must be a valid CIDR block." + } +} + +variable "http_cidr" { + description = "CIDR range allowed to access the frontend VM via HTTP" + type = string + + validation { + condition = can(cidrhost(var.http_cidr, 0)) + error_message = "http_cidr must be a valid CIDR block." + } +} + +variable "external_network_name" { + description = "Name of the external network used for floating IP allocation" + type = string + default = "ntnu-internal" +} + +variable "frontend_count" { + description = "Number of frontend VMs to create" + type = number + default = 1 + + validation { + condition = var.frontend_count >= 1 && floor(var.frontend_count) == var.frontend_count + error_message = "frontend_count must be a positive integer." + } +} \ No newline at end of file diff --git a/environments/stage/network/.terraform.lock.hcl b/environments/stage/network/.terraform.lock.hcl new file mode 100644 index 0000000..5de7e39 --- /dev/null +++ b/environments/stage/network/.terraform.lock.hcl @@ -0,0 +1,38 @@ +# This file is maintained automatically by "tofu init". +# Manual edits may be lost in future updates. + +provider "registry.opentofu.org/terraform-provider-openstack/openstack" { + version = "3.4.0" + constraints = "~> 3.0" + hashes = [ + "h1:BB0YFgWM3KZTFY16pCYIzBqM4IpmDpT03m29VZEV2OU=", + "h1:CalEmc/Tn9xv1Xx2bX92EUdsF8z6Lldmb4/gwT5lSRw=", + "h1:DwSYyG1VNcEMUVY1GE9gbo2C3UPS8W2bFGk4WGVJnDY=", + "h1:FpAT7j83xnW4SkiUL6A0eXo9V1bYbABt1Eh467INBgk=", + "h1:FqJxdGUazKAfnZa11qT7h5PstlSTeE4xTKPUiWEgPmg=", + "h1:GgJWG1fXiDuRBcdDuzOWRW0TMW1Lx5N8O6+80v17LZc=", + "h1:HsPiqtbovSqh8jHAuMDR3tNeRNCOBEeHCvRbkgAHiaY=", + "h1:IwC35geJo3X96yLwIqcjSzoLL1Nt+6qJA6BoBMlGgMw=", + "h1:MVSoVvhjbu7s1pfYfsiYED8A++XfAoyOlSX1x9PW68E=", + "h1:YMwKmPy+svf02jWyflqaPaQVX3eIXciwGeA8uwYgRBw=", + "h1:n1yIddr5lsLoYBhlfUmCwIYiCsynDkLvzAE98Tceo/A=", + "h1:nON9j1AYtfbOAmaml0lcPCfEQcb0OLB0K693HjdFggQ=", + "h1:rqRC/cidHw8t2vQb85cjMoWThGSCWnkEmTmmNSaGBVs=", + "h1:tzU+JfFe/RN+7swp0k5V5wUmuZtRNOjKX+spIGcRuKM=", + "zh:11b3c88e24197a29b13cf5ab41771944bd16707b561645323e8cbb4f1da00b7b", + "zh:1c3e89cf19118fc07d7b04257251fc9897e722c16e0a0df7b07fcd261f8c12e7", + "zh:2dc60dbbbb822fbb1e7b17e3b89e3e6a7a8fe54a716d4477d2fa40f836c4de0f", + "zh:68f851e333f0ab2367e9b2680739d47e4151ca9f803934ef265f6fa70a28e6e0", + "zh:6995b0bcae5a291d7b0a4d3a5df109eb97f8d1d34be6eb27768fe29b5e0d8def", + "zh:7b68bf81891a0445c543629d4d160226a6f2d5e8e5ee4baf21e0221412437d17", + "zh:8f117b8b17280598a92c6202b183a77168fe573397cfb597b9c2028da281846d", + "zh:b0a23b990eb9260e9616fd8471f11b37485f92df0e5eef46cd286f87396b0e0a", + "zh:baa5052ab3207f3a996c1fd715935d79a4e1c57d3d24e3c549b6c20ea9307e38", + "zh:c012cbda058aa7b76b718074fd73829ecd4009ad98e3dafe496e5f26099625c0", + "zh:c1c95e5dbe546a935a62c63814aef04718b58dd4258e5da40d681b43322924e9", + "zh:c720bdbdf432041f99572bac40d24659239b989cbf1540f92ebb441900ac8bae", + "zh:c92ac802a3dd1abbf9c9ccf0aba1f261fe9f8c2e7fafc196c7119b73c25a0772", + "zh:fd4faf25ee72acef4ab12c93ea9c7a5ccb41315e8851609e08b37d3c91ea6584", + "zh:ff347392412fc7950635e2c1c5bb878d88b661c1c5ba052edd290d971a14028f", + ] +} diff --git a/environments/stage/network/main.tf b/environments/stage/network/main.tf new file mode 100644 index 0000000..2f85eef --- /dev/null +++ b/environments/stage/network/main.tf @@ -0,0 +1,17 @@ +module "network" { + source = "../../../modules/network" + + network_name = var.network_name + + frontend_subnet_name = var.frontend_subnet_name + frontend_subnet_cidr = var.frontend_subnet_cidr + + backend_subnet_name = var.backend_subnet_name + backend_subnet_cidr = var.backend_subnet_cidr + + database_subnet_name = var.database_subnet_name + database_subnet_cidr = var.database_subnet_cidr + + router_name = var.router_name + external_network_name = var.external_network_name +} \ No newline at end of file diff --git a/environments/stage/network/outputs.tf b/environments/stage/network/outputs.tf new file mode 100644 index 0000000..15b6c07 --- /dev/null +++ b/environments/stage/network/outputs.tf @@ -0,0 +1,24 @@ +output "network_id" { + description = "ID of the STAGE network" + value = module.network.network_id +} + +output "frontend_subnet_id" { + description = "ID of the STAGE frontend subnet" + value = module.network.frontend_subnet_id +} + +output "backend_subnet_id" { + description = "ID of the STAGE backend subnet" + value = module.network.backend_subnet_id +} + +output "database_subnet_id" { + description = "ID of the STAGE database subnet" + value = module.network.database_subnet_id +} + +output "router_id" { + description = "ID of the STAGE router" + value = module.network.router_id +} \ No newline at end of file diff --git a/environments/stage/network/providers.tf b/environments/stage/network/providers.tf new file mode 100644 index 0000000..e590b22 --- /dev/null +++ b/environments/stage/network/providers.tf @@ -0,0 +1,17 @@ +terraform { + required_version = ">= 1.12.0" + + required_providers { + openstack = { + source = "terraform-provider-openstack/openstack" + version = "~> 3" + } + } + + backend "kubernetes" { + namespace = "iac" + secret_suffix = "assignment2-stage-network" + } +} + +provider "openstack" {} \ No newline at end of file diff --git a/environments/stage/network/terraform.tfvars.example b/environments/stage/network/terraform.tfvars.example new file mode 100644 index 0000000..7b05527 --- /dev/null +++ b/environments/stage/network/terraform.tfvars.example @@ -0,0 +1,18 @@ +# Network +network_name = "assignment2-stage-network" + +# Frontend subnet +frontend_subnet_name = "assignment2-stage-frontend-subnet" +frontend_subnet_cidr = "10.70.10.0/24" + +# Backend subnet +backend_subnet_name = "assignment2-stage-backend-subnet" +backend_subnet_cidr = "10.70.20.0/24" + +# Database subnet +database_subnet_name = "assignment2-stage-database-subnet" +database_subnet_cidr = "10.70.30.0/24" + +# Router +router_name = "assignment2-stage-router" +external_network_name = "ntnu-internal" \ No newline at end of file diff --git a/environments/stage/network/variables.tf b/environments/stage/network/variables.tf new file mode 100644 index 0000000..ce21774 --- /dev/null +++ b/environments/stage/network/variables.tf @@ -0,0 +1,60 @@ +variable "network_name" { + description = "Name of the network" + type = string +} + +variable "frontend_subnet_name" { + description = "Name of the frontend subnet" + type = string +} + +variable "frontend_subnet_cidr" { + description = "CIDR block for the frontend subnet" + type = string + + validation { + condition = can(cidrhost(var.frontend_subnet_cidr, 0)) + error_message = "frontend_subnet_cidr must be a valid CIDR block." + } +} + +variable "backend_subnet_name" { + description = "Name of the backend subnet" + type = string +} + +variable "backend_subnet_cidr" { + description = "CIDR block for the backend subnet" + type = string + + validation { + condition = can(cidrhost(var.backend_subnet_cidr, 0)) + error_message = "backend_subnet_cidr must be a valid CIDR block." + } +} + +variable "database_subnet_name" { + description = "Name of the database subnet" + type = string +} + +variable "database_subnet_cidr" { + description = "CIDR block for the database subnet" + type = string + + validation { + condition = can(cidrhost(var.database_subnet_cidr, 0)) + error_message = "database_subnet_cidr must be a valid CIDR block." + } +} + +variable "router_name" { + description = "Name of the router" + type = string +} + +variable "external_network_name" { + description = "Name of the external network" + type = string + default = "ntnu-internal" +} \ No newline at end of file