From 781c7dc62165f1169d0a11c2c806c483d611d01f Mon Sep 17 00:00:00 2001 From: Yakov K Date: Sat, 2 Nov 2024 11:34:56 +0200 Subject: [PATCH 1/2] Solution --- backend.tf | 8 ++++ main.tf | 41 ++++++++++++++++++ modules/compute/main.tf | 83 ++++++++++++++++++++++++++++++++++++ modules/compute/outputs.tf | 14 ++++++ modules/compute/variables.tf | 29 +++++++++++++ modules/network/main.tf | 43 +++++++++++++++++++ modules/network/outputs.tf | 29 +++++++++++++ modules/network/variables.tf | 15 +++++++ modules/storage/main.tf | 13 ++++++ modules/storage/outputs.tf | 9 ++++ modules/storage/variables.tf | 14 ++++++ outputs.tf | 34 +++++++++++++++ terraform.tfvars | 3 ++ variables.tf | 51 ++++++++++++++++++++++ 14 files changed, 386 insertions(+) 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 terraform.tfvars create mode 100644 variables.tf diff --git a/backend.tf b/backend.tf new file mode 100644 index 0000000..2e952a8 --- /dev/null +++ b/backend.tf @@ -0,0 +1,8 @@ +terraform { + backend "azurerm" { + resource_group_name = "mate-azure-task-12" + storage_account_name = "tfstate144" + container_name = "tfstate" + key = "terraform.tfstate" + } +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..6a2a487 --- /dev/null +++ b/main.tf @@ -0,0 +1,41 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "3.105.0" + } + } +} + +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "main" { + name = var.resource_group_name + location = var.location +} + +module "compute" { + source = "./modules/compute" + resource_group_name = azurerm_resource_group.main.name + vm_name = var.vm_name + location = azurerm_resource_group.main.location + subnet_id = module.network.subnet_id + public_ip_id = module.network.public_ip_address + ssh_key = var.ssh_key +} + +module "network" { + source = "./modules/network" + resource_group_name = azurerm_resource_group.main.name + location = azurerm_resource_group.main.location + dns_label_prefix = var.dns_label +} + +module "storage" { + source = "./modules/storage" + resource_group_name = azurerm_resource_group.main.name + location = azurerm_resource_group.main.location + storage_account_name = var.storage_account_name +} diff --git a/modules/compute/main.tf b/modules/compute/main.tf new file mode 100644 index 0000000..d68e281 --- /dev/null +++ b/modules/compute/main.tf @@ -0,0 +1,83 @@ +resource "azurerm_network_interface" "main" { + name = "${var.vm_name}-nic" + location = var.location + resource_group_name = var.resource_group_name + + ip_configuration { + name = "internal" + subnet_id = var.subnet_id + private_ip_address_allocation = "Dynamic" + public_ip_address_id = azurerm_public_ip.linuxboxpip.id + } +} + +resource "azurerm_public_ip" "linuxboxpip" { + name = "linuxboxpip" + location = var.location + resource_group_name = var.resource_group_name + allocation_method = "Static" + sku = "Standard" +} + +resource "azurerm_virtual_machine" "main" { + name = "matebox" + location = var.location + resource_group_name = var.resource_group_name + network_interface_ids = [azurerm_network_interface.main.id] + vm_size = "Standard_B1s" + delete_os_disk_on_termination = true + delete_data_disks_on_termination = true + + os_profile { + computer_name = "todoappserver" + admin_username = "adminuser" + } + + os_profile_linux_config { + disable_password_authentication = true + + ssh_keys { + path = "/home/adminuser/.ssh/authorized_keys" + key_data = var.ssh_key + } + } + + storage_image_reference { + publisher = "Canonical" + offer = "0001-com-ubuntu-server-jammy" + sku = "22_04-lts" + version = "latest" + } + + storage_os_disk { + name = "myosdisk1" + caching = "ReadWrite" + create_option = "FromImage" + managed_disk_type = "Standard_LRS" + } + + tags = { + environment = "staging" + } + + lifecycle { + prevent_destroy = true + } +} + +resource "azurerm_virtual_machine_extension" "CustomScript" { + name = "CustomScript" + virtual_machine_id = azurerm_virtual_machine.main.id + publisher = "Microsoft.Azure.Extensions" + type = "CustomScript" + type_handler_version = "2.0" + auto_upgrade_minor_version = true + + settings = < Date: Sat, 2 Nov 2024 13:24:39 +0200 Subject: [PATCH 2/2] Solution2MajorChanges --- main.tf | 25 +++++----- modules/compute/main.tf | 89 +++++++++++++++--------------------- modules/compute/outputs.tf | 4 +- modules/compute/variables.tf | 20 ++++---- modules/network/main.tf | 35 +++++++------- modules/network/outputs.tf | 6 +-- modules/network/variables.tf | 8 ++-- modules/storage/variables.tf | 10 ++-- variables.tf | 55 ++++++---------------- 9 files changed, 104 insertions(+), 148 deletions(-) diff --git a/main.tf b/main.tf index 6a2a487..a84561b 100644 --- a/main.tf +++ b/main.tf @@ -12,18 +12,8 @@ provider "azurerm" { } resource "azurerm_resource_group" "main" { - name = var.resource_group_name - location = var.location -} - -module "compute" { - source = "./modules/compute" - resource_group_name = azurerm_resource_group.main.name - vm_name = var.vm_name - location = azurerm_resource_group.main.location - subnet_id = module.network.subnet_id - public_ip_id = module.network.public_ip_address - ssh_key = var.ssh_key + name = "todolist-terraform-task" + location = "West Europe" } module "network" { @@ -33,6 +23,17 @@ module "network" { dns_label_prefix = var.dns_label } +module "compute" { + source = "./modules/compute" + location = azurerm_resource_group.main.location + resource_group_name = azurerm_resource_group.main.name + vm_name = var.vm_name + + subnet_id = module.network.subnet_id + public_ip_id = module.network.public_ip_address + ssh_key = var.ssh_key +} + module "storage" { source = "./modules/storage" resource_group_name = azurerm_resource_group.main.name diff --git a/modules/compute/main.tf b/modules/compute/main.tf index d68e281..1cec084 100644 --- a/modules/compute/main.tf +++ b/modules/compute/main.tf @@ -1,4 +1,11 @@ -resource "azurerm_network_interface" "main" { +resource "azurerm_public_ip" "linuxboxpip" { + name = "linuxboxpip" + location = var.location + resource_group_name = var.resource_group_name + allocation_method = "Static" +} + +resource "azurerm_network_interface" "vm-nic" { name = "${var.vm_name}-nic" location = var.location resource_group_name = var.resource_group_name @@ -11,73 +18,49 @@ resource "azurerm_network_interface" "main" { } } -resource "azurerm_public_ip" "linuxboxpip" { - name = "linuxboxpip" - location = var.location +resource "azurerm_linux_virtual_machine" "matebox" { + name = "matebox" resource_group_name = var.resource_group_name - allocation_method = "Static" - sku = "Standard" -} - -resource "azurerm_virtual_machine" "main" { - name = "matebox" - location = var.location - resource_group_name = var.resource_group_name - network_interface_ids = [azurerm_network_interface.main.id] - vm_size = "Standard_B1s" - delete_os_disk_on_termination = true - delete_data_disks_on_termination = true + location = var.location + size = "Standard_B1s" + admin_username = "adminuser" + network_interface_ids = [ + azurerm_network_interface.vm-nic.id, + ] - os_profile { - computer_name = "todoappserver" - admin_username = "adminuser" + admin_ssh_key { + username = "adminuser" + public_key = var.ssh_key } - os_profile_linux_config { - disable_password_authentication = true - - ssh_keys { - path = "/home/adminuser/.ssh/authorized_keys" - key_data = var.ssh_key - } + os_disk { + caching = "ReadWrite" + storage_account_type = "Standard_LRS" } - storage_image_reference { + source_image_reference { publisher = "Canonical" offer = "0001-com-ubuntu-server-jammy" sku = "22_04-lts" version = "latest" } - - storage_os_disk { - name = "myosdisk1" - caching = "ReadWrite" - create_option = "FromImage" - managed_disk_type = "Standard_LRS" - } - - tags = { - environment = "staging" - } - - lifecycle { - prevent_destroy = true - } } resource "azurerm_virtual_machine_extension" "CustomScript" { - name = "CustomScript" - virtual_machine_id = azurerm_virtual_machine.main.id - publisher = "Microsoft.Azure.Extensions" - type = "CustomScript" - type_handler_version = "2.0" - auto_upgrade_minor_version = true + name = "CustomScript" + virtual_machine_id = azurerm_linux_virtual_machine.matebox.id + publisher = "Microsoft.Azure.Extensions" + type = "CustomScript" + type_handler_version = "2.0" settings = <