From 0e5b2c511bef3729eec4f71e809664cbf0f3ba74 Mon Sep 17 00:00:00 2001 From: Tania Kolesnik Date: Tue, 19 May 2026 10:15:50 +0100 Subject: [PATCH 1/2] add solutuon --- .gitignore | 15 +++++++++ backend.tf | 8 +++++ install-app.sh | 12 +++---- main.tf | 40 +++++++++++++++++++++++ modules/compute/main.tf | 63 ++++++++++++++++++++++++++++++++++++ modules/compute/outputs.tf | 0 modules/compute/variables.tf | 34 +++++++++++++++++++ modules/network/main.tf | 43 ++++++++++++++++++++++++ modules/network/outputs.tf | 3 ++ modules/network/variables.tf | 43 ++++++++++++++++++++++++ modules/storage/main.tf | 13 ++++++++ modules/storage/outputs.tf | 0 modules/storage/variables.tf | 14 ++++++++ outputs.tf | 0 variables.tf | 9 ++++++ 15 files changed, 291 insertions(+), 6 deletions(-) create mode 100644 .gitignore create mode 100644 backend.tf create mode 100644 main.tf create mode 100644 modules/compute/main.tf create mode 100644 modules/compute/outputs.tf create mode 100644 modules/compute/variables.tf create mode 100644 modules/network/main.tf create mode 100644 modules/network/outputs.tf create mode 100644 modules/network/variables.tf create mode 100644 modules/storage/main.tf create mode 100644 modules/storage/outputs.tf create mode 100644 modules/storage/variables.tf create mode 100644 outputs.tf create mode 100644 variables.tf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eec46cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +.terraform/ +*.tfstate +*.tfstate.* +*.tfvars +*.tfvars.json +*.tfvars.* +override.tf +override.tf.json +*_override.tf +*_override.tf.json +crash.log +crash.log.* +.terraform.lock.hcl +terraform.tfplan +terraform.tfplan.* diff --git a/backend.tf b/backend.tf new file mode 100644 index 0000000..0bff629 --- /dev/null +++ b/backend.tf @@ -0,0 +1,8 @@ +# terraform { +# backend "azurerm" { +# resource_group_name = "mate-azure-task-12" +# storage_account_name = "storageacctk19052026" +# container_name = "tfstate" +# key = "terraform.tfstate" +# } +# } diff --git a/install-app.sh b/install-app.sh index 03706de..a06ecc7 100644 --- a/install-app.sh +++ b/install-app.sh @@ -1,19 +1,19 @@ #!/bin/bash -# Script to silently install and start the todo web app on the virtual machine. -# Note that all commands bellow are without sudo - that's because extention mechanism +# Script to silently install and start the todo web app on the virtual machine. +# Note that all commands bellow are without sudo - that's because extention mechanism # runs scripts under root user. # install system updates and isntall python3-pip package using apt. '-yq' flags are # used to suppress any interactive prompts - we won't be able to confirm operation -# when running the script as VM extention. +# when running the script as VM extention. apt-get update -yq apt-get install python3-pip -yq -# Create a directory for the app and download the files. -mkdir /app +# Create a directory for the app and download the files. +mkdir /app # make sure to uncomment the line bellow and update the link with your GitHub username -# git clone https://github.com//azure_task_12_deploy_app_with_vm_extention.git +git clone https://github.com/taniakolesnik/azure_task_12_deploy_app_with_vm_extention.git cp -r devops_todolist_terraform_task/app/* /app # create a service for the app via systemctl and start the app diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..e391611 --- /dev/null +++ b/main.tf @@ -0,0 +1,40 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~> 3.0" + } + } +} + +provider "azurerm" { + features {} +} + +module "network" { + source = "./modules/network" + location = var.location + azurerm_resource_group_name = var.azurerm_resource_group_name + azurerm_network_security_group_name = "defaultnsg" + azurerm_virtual_network_name = "vnet" + azurerm_virtual_network_address_space = ["10.0.0.0/16"] + subnet_name = "default" + subnet_prefix = ["10.0.0.0/24"] + azurerm_public_ip_name = "linuxboxpip" + dns_label = "matetask" +} + +module "compute" { + source = "./modules/compute" + location = var.location + subnet_id = module.network.subnet_id + vm_name = "matebox" + vm_size = "Standard_B1s" + public_ssh_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCbaCyW7Alj8Jkw6OYyGvY5kymGutM8gKfLRWC9zFN+N5D1sQQudJE5xTtOh0lTJuYNdSfl5kqtP80mtSe3sh5ZTKktPyKd+12oR4zw7xSy7yzEUgf5uFr5yBAWeQo905PLAseXh0zwMX8Bs34mHVN65tkvYxzwTh/2Es5IIUF+QBvCh4dfUnv8r+PkNnmtT6GCXN9iw8zMbRnmOpp7CYikIqDwiuCxioRqcjMYKBipJ5CLw5wZYOy4BKJL7CF1hG8pgZUQAoYWhpdaICwqqtBInhyMk0vbRS4aeZRt7XP2MlkEtgwDB9haWiJUVXMkWwAWaYQJ0Ge46xen2xPjYjhdked+DDZGikXStejXOqp6YNn/61nc4t5SMAmvrajZvn7nfJPdDqLtLbTgxO/CINV6gHON058yBraZziDVEaq8O8XnkVS1l/fwz3Vwl3mnrGN/PYWIsaxJXhf+3Pm+UzSoUDsX1Ic3e47EgOZAKHJGVdHjIHjtsnHDpI/m2c2zxFqjfl3F4pe6q/+oH98rKSUtOf6Ba0MN+roBuJtCJCa7jdvlBLebvtPVYONk58E404BS1zpiw+snbW+JK0SZJvRXvrLDGHA1dYVVeKdnqC/tPd3eRUWICdi6blpGbTBaK17jdpXj7BLRpsQrHSXraCbFYi/32t+ZadfmLiBdXyCWoQ== tetianakolesnik@Tetianas-MBP" +} + +module "storage" { + source = "./modules/storage" + azurerm_resource_group_name = var.azurerm_resource_group_name + location = var.location +} \ No newline at end of file diff --git a/modules/compute/main.tf b/modules/compute/main.tf new file mode 100644 index 0000000..69da32b --- /dev/null +++ b/modules/compute/main.tf @@ -0,0 +1,63 @@ +resource "azurerm_network_interface" "example" { + name = "${var.vm_name}-nic" + location = var.location + resource_group_name = var.azurerm_resource_group_name + + ip_configuration { + name = "internal" + subnet_id = var.subnet_id + private_ip_address_allocation = "Dynamic" + } +} + +resource "azurerm_linux_virtual_machine" "example" { + name = var.vm_name + resource_group_name = var.azurerm_resource_group_name + location = var.location + size = var.vm_size + admin_username = "adminuser" + network_interface_ids = [ + azurerm_network_interface.example.id, + ] + + admin_ssh_key { + username = "adminuser" + public_key = var.public_ssh_key + } + + os_disk { + caching = "ReadWrite" + storage_account_type = "Standard_LRS" + } + + source_image_reference { + publisher = "Canonical" + offer = "0001-com-ubuntu-server-jammy" + sku = "22_04-lts" + version = "latest" + } +} + +resource "azurerm_virtual_machine_extension" "example" { + name = "hostname" + virtual_machine_id = azurerm_linux_virtual_machine.example.id + publisher = "Microsoft.Azure.Extensions" + type = "CustomScript" + type_handler_version = "2.0" + + settings = jsonencode({ + commandToExecute = <<-EOT + apt-get update -yq + apt-get install python3-pip git -yq + + mkdir -p /app + git clone https://github.com/taniakolesnik/azure_task_12_deploy_app_with_vm_extention.git /tmp/apprepo + cp -r /tmp/apprepo/devops_todolist_terraform_task/app/* /app + + mv /app/todoapp.service /etc/systemd/system/ + systemctl daemon-reload + systemctl start todoapp + systemctl enable todoapp + EOT + }) +} \ No newline at end of file diff --git a/modules/compute/outputs.tf b/modules/compute/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/modules/compute/variables.tf b/modules/compute/variables.tf new file mode 100644 index 0000000..963351c --- /dev/null +++ b/modules/compute/variables.tf @@ -0,0 +1,34 @@ +variable "azurerm_resource_group_name" { + description = "The name of the resource group" + default = "mate-azure-task-12" + type = string +} + +variable "location" { + description = "Resource location" + default = "uksouth" + type = string +} + +variable "subnet_id" { + description = "subnet id" + type = string +} + +variable "vm_name" { + description = "The name of the virtual machine" + default = "matebox" + type = string +} + +variable "vm_size" { + description = "The size of the virtual machine" + default = "Standard_B1s" + type = string +} + +variable "public_ssh_key" { + default = "linuxboxsshkey" + type = string + description = "SSH key" +} \ No newline at end of file diff --git a/modules/network/main.tf b/modules/network/main.tf new file mode 100644 index 0000000..5e0536e --- /dev/null +++ b/modules/network/main.tf @@ -0,0 +1,43 @@ + +# network security group +resource "azurerm_network_security_group" "example" { + name = var.azurerm_network_security_group_name + location = var.location + resource_group_name = var.azurerm_resource_group_name +} + +# virtual network +resource "azurerm_virtual_network" "example" { + name = var.azurerm_virtual_network_name + location = var.location + resource_group_name = var.azurerm_resource_group_name + address_space = var.azurerm_virtual_network_address_space +} + +# subnet +resource "azurerm_subnet" "default" { + name = var.subnet_name + resource_group_name = var.azurerm_resource_group_name + virtual_network_name = azurerm_virtual_network.example.name + address_prefixes = var.subnet_prefix +} + +# assosiate subnet to network security group +resource "azurerm_subnet_network_security_group_association" "default" { + subnet_id = azurerm_subnet.default.id + network_security_group_id = azurerm_network_security_group.example.id +} + +resource "random_integer" "dns" { + min = 1000 + max = 9999 +} + +# public IP address +resource "azurerm_public_ip" "example" { + name = var.azurerm_public_ip_name + resource_group_name = var.azurerm_resource_group_name + location = var.location + allocation_method = "Dynamic" + domain_name_label = "${var.dns_label}${random_integer.dns.result}" +} \ No newline at end of file diff --git a/modules/network/outputs.tf b/modules/network/outputs.tf new file mode 100644 index 0000000..65574ac --- /dev/null +++ b/modules/network/outputs.tf @@ -0,0 +1,3 @@ +output "subnet_id" { + value = azurerm_subnet.default.id +} \ No newline at end of file diff --git a/modules/network/variables.tf b/modules/network/variables.tf new file mode 100644 index 0000000..6490216 --- /dev/null +++ b/modules/network/variables.tf @@ -0,0 +1,43 @@ +variable "azurerm_resource_group_name" { + default = "mate-azure-task-12" + type = string +} + +variable "location" { + default = "uksouth" + type = string +} + +variable "azurerm_network_security_group_name" { + default = "defaultnsg" + type = string +} + +variable "azurerm_virtual_network_name" { + default = "vnet" + type = string +} + +variable "azurerm_virtual_network_address_space" { + default = ["10.0.0.0/16"] + type = list(string) +} +variable "subnet_name" { + default = "default" + type = string +} + +variable "subnet_prefix" { + default = ["10.0.0.0/24"] + type = list(string) +} + +variable "azurerm_public_ip_name" { + default = "linuxboxpip" + type = string +} + +variable "dns_label" { + default = "matetask" + type = string +} \ No newline at end of file diff --git a/modules/storage/main.tf b/modules/storage/main.tf new file mode 100644 index 0000000..5fc50dc --- /dev/null +++ b/modules/storage/main.tf @@ -0,0 +1,13 @@ +resource "azurerm_storage_account" "example" { + name = "storageacctk19052026" + resource_group_name = var.azurerm_resource_group_name + location = var.location + account_tier = "Standard" + account_replication_type = "LRS" +} + +resource "azurerm_storage_container" "example" { + name = var.azurerm_storage_container_name + storage_account_name = azurerm_storage_account.example.name + container_access_type = "private" +} \ No newline at end of file diff --git a/modules/storage/outputs.tf b/modules/storage/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/modules/storage/variables.tf b/modules/storage/variables.tf new file mode 100644 index 0000000..91b2470 --- /dev/null +++ b/modules/storage/variables.tf @@ -0,0 +1,14 @@ +variable "azurerm_resource_group_name" { + default = "mate-azure-task-12" + type = string +} + +variable "location" { + default = "uksouth" + type = string +} + +variable "azurerm_storage_container_name" { + default = "task-artifacts" + type = string +} \ No newline at end of file diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..0147428 --- /dev/null +++ b/variables.tf @@ -0,0 +1,9 @@ +variable "azurerm_resource_group_name" { + default = "mate-azure-task-12" + type = string +} + +variable "location" { + default = "uksouth" + type = string +} \ No newline at end of file From da91fb83cbe6bf0c432cf1eeddebbfb2590882c1 Mon Sep 17 00:00:00 2001 From: Tania Kolesnik Date: Tue, 19 May 2026 10:33:30 +0100 Subject: [PATCH 2/2] add solutuon --- backend.tf | 16 ++++++------ main.tf | 25 ++++++++++-------- modules/compute/variables.tf | 2 +- modules/network/outputs.tf | 8 ++++++ outputs.tf | 7 ++++++ variables.tf | 49 ++++++++++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 19 deletions(-) diff --git a/backend.tf b/backend.tf index 0bff629..58c25ca 100644 --- a/backend.tf +++ b/backend.tf @@ -1,8 +1,8 @@ -# terraform { -# backend "azurerm" { -# resource_group_name = "mate-azure-task-12" -# storage_account_name = "storageacctk19052026" -# container_name = "tfstate" -# key = "terraform.tfstate" -# } -# } +terraform { + backend "azurerm" { + resource_group_name = "mate-azure-task-12" + storage_account_name = "storageacctk19052026" + container_name = "tfstate" + key = "terraform.tfstate" + } +} diff --git a/main.tf b/main.tf index e391611..07dc16c 100644 --- a/main.tf +++ b/main.tf @@ -11,26 +11,31 @@ provider "azurerm" { features {} } +resource "azurerm_resource_group" "name" { + name = var.azurerm_resource_group_name + location = var.location +} + module "network" { source = "./modules/network" location = var.location azurerm_resource_group_name = var.azurerm_resource_group_name - azurerm_network_security_group_name = "defaultnsg" - azurerm_virtual_network_name = "vnet" - azurerm_virtual_network_address_space = ["10.0.0.0/16"] - subnet_name = "default" - subnet_prefix = ["10.0.0.0/24"] - azurerm_public_ip_name = "linuxboxpip" - dns_label = "matetask" + azurerm_network_security_group_name = var.azurerm_network_security_group_name + azurerm_virtual_network_name = var.azurerm_virtual_network_name + azurerm_virtual_network_address_space = var.azurerm_virtual_network_address_space + subnet_name = var.subnet_name + subnet_prefix = var.subnet_prefix + azurerm_public_ip_name = var.azurerm_public_ip_name + dns_label = var.dns_label } module "compute" { source = "./modules/compute" location = var.location subnet_id = module.network.subnet_id - vm_name = "matebox" - vm_size = "Standard_B1s" - public_ssh_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCbaCyW7Alj8Jkw6OYyGvY5kymGutM8gKfLRWC9zFN+N5D1sQQudJE5xTtOh0lTJuYNdSfl5kqtP80mtSe3sh5ZTKktPyKd+12oR4zw7xSy7yzEUgf5uFr5yBAWeQo905PLAseXh0zwMX8Bs34mHVN65tkvYxzwTh/2Es5IIUF+QBvCh4dfUnv8r+PkNnmtT6GCXN9iw8zMbRnmOpp7CYikIqDwiuCxioRqcjMYKBipJ5CLw5wZYOy4BKJL7CF1hG8pgZUQAoYWhpdaICwqqtBInhyMk0vbRS4aeZRt7XP2MlkEtgwDB9haWiJUVXMkWwAWaYQJ0Ge46xen2xPjYjhdked+DDZGikXStejXOqp6YNn/61nc4t5SMAmvrajZvn7nfJPdDqLtLbTgxO/CINV6gHON058yBraZziDVEaq8O8XnkVS1l/fwz3Vwl3mnrGN/PYWIsaxJXhf+3Pm+UzSoUDsX1Ic3e47EgOZAKHJGVdHjIHjtsnHDpI/m2c2zxFqjfl3F4pe6q/+oH98rKSUtOf6Ba0MN+roBuJtCJCa7jdvlBLebvtPVYONk58E404BS1zpiw+snbW+JK0SZJvRXvrLDGHA1dYVVeKdnqC/tPd3eRUWICdi6blpGbTBaK17jdpXj7BLRpsQrHSXraCbFYi/32t+ZadfmLiBdXyCWoQ== tetianakolesnik@Tetianas-MBP" + vm_name = var.vm_name + vm_size = var.vm_size + public_ssh_key = var.public_ssh_key } module "storage" { diff --git a/modules/compute/variables.tf b/modules/compute/variables.tf index 963351c..793211f 100644 --- a/modules/compute/variables.tf +++ b/modules/compute/variables.tf @@ -31,4 +31,4 @@ variable "public_ssh_key" { default = "linuxboxsshkey" type = string description = "SSH key" -} \ No newline at end of file +} diff --git a/modules/network/outputs.tf b/modules/network/outputs.tf index 65574ac..ee7eaee 100644 --- a/modules/network/outputs.tf +++ b/modules/network/outputs.tf @@ -1,3 +1,11 @@ output "subnet_id" { value = azurerm_subnet.default.id +} + +output "public_ip_address" { + value = azurerm_public_ip.example.ip_address +} + +output "public_ip_fqdn" { + value = azurerm_public_ip.example.fqdn } \ No newline at end of file diff --git a/outputs.tf b/outputs.tf index e69de29..6e0a8ab 100644 --- a/outputs.tf +++ b/outputs.tf @@ -0,0 +1,7 @@ +output "public_ip_address_name" { + value = module.network.public_ip_address +} + +output "public_ip_fqdn" { + value = module.network.public_ip_fqdn +} \ No newline at end of file diff --git a/variables.tf b/variables.tf index 0147428..0e4dd03 100644 --- a/variables.tf +++ b/variables.tf @@ -6,4 +6,53 @@ variable "azurerm_resource_group_name" { variable "location" { default = "uksouth" type = string +} + +variable "public_ssh_key" { + type = string +} + +variable "azurerm_network_security_group_name" { + default = "defaultnsg" + type = string +} + +variable "azurerm_virtual_network_name" { + default = "vnet" + type = string +} + +variable "azurerm_virtual_network_address_space" { + default = ["10.0.0.0/16"] + type = list(string) +} + +variable "subnet_name" { + default = "default" + type = string +} + +variable "subnet_prefix" { + default = ["10.0.0.0/24"] + type = list(string) +} + +variable "azurerm_public_ip_name" { + default = "linuxboxpip" + type = string +} + +variable "dns_label" { + default = "matetask" + type = string +} + +variable "vm_name" { + default = "matebox" + type = string +} + +variable "vm_size" { + default = "Standard_B1s" + type = string } \ No newline at end of file