Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution? #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 20 additions & 40 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,56 +1,27 @@
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.105.0"
}
}
}

provider "azurerm" {
features {}
}

variable "prefix" {
default = "tfvmex"
}

resource "azurerm_resource_group" "example" {
name = "${var.prefix}-resources"
location = "West Europe"
}

resource "azurerm_virtual_network" "main" {
name = "${var.prefix}-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.main.name
address_prefixes = ["10.0.2.0/24"]
locals {
nic_names = ["nic1", "nic2", "nic3"]
}

resource "azurerm_network_interface" "main" {
name = "${var.prefix}-nic"
for_each = toset(local.nic_names)

name = "${var.prefix}-${each.key}"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name

ip_configuration {
name = "testconfiguration1"
name = "testconfiguration-${each.key}"
subnet_id = azurerm_subnet.internal.id
private_ip_address_allocation = "Dynamic"
}
}

resource "azurerm_virtual_machine" "main" {
name = "${var.prefix}-vm"
count = var.vm_count
name = "${var.prefix}-vm-${count.index}"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
network_interface_ids = [azurerm_network_interface.main.id]
network_interface_ids = [azurerm_network_interface.main["nic${count.index + 1}"].id]
vm_size = "Standard_DS1_v2"

storage_image_reference {
Expand All @@ -59,21 +30,30 @@ resource "azurerm_virtual_machine" "main" {
sku = "22_04-lts"
version = "latest"
}

storage_os_disk {
name = "myosdisk1"
name = "myosdisk-${count.index}"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}

os_profile {
computer_name = "hostname"
computer_name = "hostname-${count.index}"
admin_username = "testadmin"
admin_password = "Password1234!"
}

os_profile_linux_config {
disable_password_authentication = false
}

lifecycle {
prevent_destroy = true
}

tags = {
environment = "staging"
instance = count.index
}
}
15 changes: 15 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
output "vm_names_uppercase" {
value = [for vm in azurerm_virtual_machine.main : upper(vm.name)]
}

output "joined_tags" {
value = join(", ", ["environment", "instance"])
}

output "vm_ids" {
value = [for vm in azurerm_virtual_machine.main : vm.id]
}

output "nic_ids" {
value = values(azurerm_network_interface.main)[*].id
}
12 changes: 12 additions & 0 deletions provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.105.0"
}
}
}

provider "azurerm" {
features {}
}
47 changes: 47 additions & 0 deletions resourse.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
resource "azurerm_resource_group" "example" {
name = "${var.prefix}-resources"
location = "West Europe"
}

resource "azurerm_virtual_network" "main" {
name = "${var.prefix}-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.main.name
address_prefixes = ["10.0.2.0/24"]
}

resource "azurerm_network_security_group" "main" {
name = "${var.prefix}-nsg"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name

dynamic "security_rule" {
for_each = local.nsg_rules

content {
name = security_rule.value["name"]
priority = security_rule.value["priority"]
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = security_rule.value["destination_port_range"]
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
}

locals {
nsg_rules = [
{ name = "SSH", priority = 100, destination_port_range = "22" },
{ name = "HTTP", priority = 110, destination_port_range = "80" },
]
}
Binary file added tfplan
Binary file not shown.
7 changes: 7 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "prefix" {
default = "tfvmex"
}

variable "vm_count" {
default = 2
}