From fcab4a6b95dae2bf0d36def02924764e7d5d4564 Mon Sep 17 00:00:00 2001 From: Gal Szkolnik Date: Tue, 1 Aug 2023 06:47:20 +0000 Subject: [PATCH] Added hosts as modules --- .../_tf/modules/deployed_host/host.input.tf | 41 +++++++++++++++++++ .../_tf/modules/deployed_host/host.output.tf | 10 +++++ .../_tf/modules/deployed_host/host.plan.tf | 26 ++++++++++++ .../_tf/modules/deployed_host/network.plan.tf | 34 +++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 src/ASSIGNMENT-03/_tf/modules/deployed_host/host.input.tf create mode 100644 src/ASSIGNMENT-03/_tf/modules/deployed_host/host.output.tf create mode 100644 src/ASSIGNMENT-03/_tf/modules/deployed_host/host.plan.tf create mode 100644 src/ASSIGNMENT-03/_tf/modules/deployed_host/network.plan.tf diff --git a/src/ASSIGNMENT-03/_tf/modules/deployed_host/host.input.tf b/src/ASSIGNMENT-03/_tf/modules/deployed_host/host.input.tf new file mode 100644 index 0000000..39e81f2 --- /dev/null +++ b/src/ASSIGNMENT-03/_tf/modules/deployed_host/host.input.tf @@ -0,0 +1,41 @@ +variable "username" { + type = string + description = "VMs admin username" +} + +variable "ssh_public_key" { + type = string + description = "SSH public signature's of the admin" +} + +variable "machine_type" { + type = string + description = "Machine type/size" + default = "Standard_B1s" # Defaulting to the 750 free hours tier machine. +} + +variable "host_name" { + type = string + description = "Name of VM instance" +} + +variable "resource_group_name" { + type = string + description = "Azure resource group name" +} + +variable "location" { + type = string + description = "location of resource" + default = "eastus" +} + +variable "local_network_cidr" { + type = string + description = "CIDR Range of the local virtual network" +} + +variable "local_subnet_cidr" { + type = string + description = "CIDR Range of the local subnet, usually contained within the local_network_cidr" +} \ No newline at end of file diff --git a/src/ASSIGNMENT-03/_tf/modules/deployed_host/host.output.tf b/src/ASSIGNMENT-03/_tf/modules/deployed_host/host.output.tf new file mode 100644 index 0000000..2ccfd87 --- /dev/null +++ b/src/ASSIGNMENT-03/_tf/modules/deployed_host/host.output.tf @@ -0,0 +1,10 @@ +output "resources" { + value = { + host = azurerm_linux_virtual_machine.deployed_host + nic = azurerm_network_interface.host_network_interface + net = azurerm_virtual_network.local_network + subnet = azurerm_subnet.local_subnet + pub_ip = azurerm_public_ip.host_public_ip + } + sensitive = false +} \ No newline at end of file diff --git a/src/ASSIGNMENT-03/_tf/modules/deployed_host/host.plan.tf b/src/ASSIGNMENT-03/_tf/modules/deployed_host/host.plan.tf new file mode 100644 index 0000000..0923f7e --- /dev/null +++ b/src/ASSIGNMENT-03/_tf/modules/deployed_host/host.plan.tf @@ -0,0 +1,26 @@ +resource "azurerm_linux_virtual_machine" "deployed_host" { + admin_username = var.username + location = var.location + name = var.host_name + resource_group_name = var.resource_group_name + secure_boot_enabled = true + size = var.machine_type + vtpm_enabled = true + admin_ssh_key { + public_key = var.ssh_public_key + username = var.username + } + boot_diagnostics { + } + os_disk { + caching = "ReadWrite" + storage_account_type = "Premium_LRS" + } + source_image_reference { + offer = "0001-com-ubuntu-server-jammy" + publisher = "canonical" + sku = "22_04-lts-gen2" + version = "latest" + } + network_interface_ids = [ azurerm_network_interface.host_network_interface.id ] +} diff --git a/src/ASSIGNMENT-03/_tf/modules/deployed_host/network.plan.tf b/src/ASSIGNMENT-03/_tf/modules/deployed_host/network.plan.tf new file mode 100644 index 0000000..00a8675 --- /dev/null +++ b/src/ASSIGNMENT-03/_tf/modules/deployed_host/network.plan.tf @@ -0,0 +1,34 @@ +resource "azurerm_virtual_network" "local_network" { + address_space = [ var.local_network_cidr ] + location = var.location + name = "${var.host_name}-vnet" + resource_group_name = var.resource_group_name +} + +resource "azurerm_subnet" "local_subnet" { + address_prefixes = [ var.local_subnet_cidr ] + name = "default" + resource_group_name = var.resource_group_name + virtual_network_name = azurerm_virtual_network.local_network.name +} + +resource "azurerm_public_ip" "host_public_ip" { + name = "${var.host_name}-ip" + allocation_method = "Static" + domain_name_label = var.host_name + location = var.location + resource_group_name = var.resource_group_name + sku = "Standard" +} + +resource "azurerm_network_interface" "host_network_interface" { + name = "${var.host_name}-nic" + location = var.location + resource_group_name = var.resource_group_name + ip_configuration { + name = "ipconfig1" + private_ip_address_allocation = "Dynamic" + public_ip_address_id = azurerm_public_ip.host_public_ip.id + subnet_id = azurerm_subnet.local_subnet.id + } +} \ No newline at end of file