Compare commits

..

2 Commits

Author SHA1 Message Date
caff317c3f fix example 2024-10-05 09:41:28 +08:00
af3343e56f bump azurerm to v3 2024-10-05 09:33:39 +08:00
10 changed files with 396 additions and 410 deletions

View File

@ -4,7 +4,7 @@ terraform {
required_providers { required_providers {
azurerm = { azurerm = {
source = "hashicorp/azurerm" source = "hashicorp/azurerm"
version = "~>2.0" version = "~>3.0"
} }
} }
} }
@ -17,6 +17,15 @@ provider "azurerm" {
} }
} }
resource "random_password" "password" {
count = var.admin_password == null ? 1 : 0
length = 20
}
locals {
admin_password = try(random_password.password[0].result, var.admin_password)
}
resource "azurerm_resource_group" "vmss" { resource "azurerm_resource_group" "vmss" {
name = var.resource_group_name name = var.resource_group_name
location = var.location location = var.location
@ -27,7 +36,7 @@ resource "random_string" "fqdn" {
length = 6 length = 6
special = false special = false
upper = false upper = false
number = false numeric = false
} }
resource "azurerm_virtual_network" "vmss" { resource "azurerm_virtual_network" "vmss" {
@ -73,14 +82,12 @@ resource "azurerm_lb_backend_address_pool" "bpepool" {
} }
resource "azurerm_lb_probe" "vmss" { resource "azurerm_lb_probe" "vmss" {
resource_group_name = azurerm_resource_group.vmss.name
loadbalancer_id = azurerm_lb.vmss.id loadbalancer_id = azurerm_lb.vmss.id
name = "ssh-running-probe" name = "ssh-running-probe"
port = var.application_port port = var.application_port
} }
resource "azurerm_lb_rule" "lbnatrule" { resource "azurerm_lb_rule" "lbnatrule" {
resource_group_name = azurerm_resource_group.vmss.name
loadbalancer_id = azurerm_lb.vmss.id loadbalancer_id = azurerm_lb.vmss.id
name = "http" name = "http"
protocol = "Tcp" protocol = "Tcp"
@ -127,7 +134,7 @@ resource "azurerm_virtual_machine_scale_set" "vmss" {
os_profile { os_profile {
computer_name_prefix = "vmlab" computer_name_prefix = "vmlab"
admin_username = var.admin_user admin_username = var.admin_user
admin_password = var.admin_password admin_password = local.admin_password
custom_data = file("web.conf") custom_data = file("web.conf")
} }
@ -198,7 +205,7 @@ resource "azurerm_virtual_machine" "jumpbox" {
os_profile { os_profile {
computer_name = "jumpbox" computer_name = "jumpbox"
admin_username = var.admin_user admin_username = var.admin_user
admin_password = var.admin_password admin_password = local.admin_password
} }
os_profile_linux_config { os_profile_linux_config {

View File

@ -28,6 +28,6 @@ variable "admin_user" {
variable "admin_password" { variable "admin_password" {
description = "Default password for admin account" description = "Default password for admin account"
default = "ChangeMe123!" default = null
sensitive = true sensitive = true
} }

View File

@ -4,14 +4,8 @@ locals {
hub-nva-resource-group = "hub-nva-rg" hub-nva-resource-group = "hub-nva-rg"
} }
resource "random_string" "suffix" {
length = 5
special = false
upper = false
}
resource "azurerm_resource_group" "hub-nva-rg" { resource "azurerm_resource_group" "hub-nva-rg" {
name = "${local.prefix-hub-nva}-rg-${random_string.suffix.result}" name = "${local.prefix-hub-nva}-rg"
location = local.hub-nva-location location = local.hub-nva-location
tags = { tags = {
@ -61,7 +55,7 @@ resource "azurerm_virtual_machine" "hub-nva-vm" {
os_profile { os_profile {
computer_name = "${local.prefix-hub-nva}-vm" computer_name = "${local.prefix-hub-nva}-vm"
admin_username = var.username admin_username = var.username
admin_password = local.password admin_password = var.password
} }
os_profile_linux_config { os_profile_linux_config {
@ -84,7 +78,7 @@ resource "azurerm_virtual_machine_extension" "enable-routes" {
settings = <<SETTINGS settings = <<SETTINGS
{ {
"fileUris": [ "fileUris": [
"https://raw.githubusercontent.com/lonegunmanb/reference-architectures/refs/heads/master/scripts/linux/enable-ip-forwarding.sh" "https://raw.githubusercontent.com/mspnp/reference-architectures/master/scripts/linux/enable-ip-forwarding.sh"
], ],
"commandToExecute": "bash enable-ip-forwarding.sh" "commandToExecute": "bash enable-ip-forwarding.sh"
} }
@ -148,7 +142,7 @@ resource "azurerm_route_table" "spoke1-rt" {
route { route {
name = "default" name = "default"
address_prefix = "0.0.0.0/0" address_prefix = "0.0.0.0/0"
next_hop_type = "VnetLocal" next_hop_type = "vnetlocal"
} }
tags = { tags = {
@ -184,7 +178,7 @@ resource "azurerm_route_table" "spoke2-rt" {
route { route {
name = "default" name = "default"
address_prefix = "0.0.0.0/0" address_prefix = "0.0.0.0/0"
next_hop_type = "VnetLocal" next_hop_type = "vnetlocal"
} }
tags = { tags = {

View File

@ -1,7 +1,7 @@
locals { locals {
prefix-hub = "hub" prefix-hub = "hub"
hub-location = "eastus" hub-location = "eastus"
hub-resource-group = "hub-vnet-rg-${random_string.suffix.result}" hub-resource-group = "hub-vnet-rg"
shared-key = "4-v3ry-53cr37-1p53c-5h4r3d-k3y" shared-key = "4-v3ry-53cr37-1p53c-5h4r3d-k3y"
} }
@ -84,7 +84,7 @@ resource "azurerm_virtual_machine" "hub-vm" {
os_profile { os_profile {
computer_name = "${local.prefix-hub}-vm" computer_name = "${local.prefix-hub}-vm"
admin_username = var.username admin_username = var.username
admin_password = local.password admin_password = var.password
} }
os_profile_linux_config { os_profile_linux_config {

View File

@ -5,24 +5,11 @@ terraform {
required_providers { required_providers {
azurerm = { azurerm = {
source = "hashicorp/azurerm" source = "hashicorp/azurerm"
version = "~> 3.0" version = "~>2.0"
} }
} }
} }
provider "azurerm" { provider "azurerm" {
features { features {}
resource_group {
prevent_deletion_if_contains_resources = false
}
}
}
resource "random_password" "password" {
count = var.password == null ? 1 : 0
length = 20
}
locals {
password = try(random_password.password[0].result, var.password)
} }

View File

@ -1,6 +1,6 @@
locals { locals {
onprem-location = "eastus" onprem-location = "eastus"
onprem-resource-group = "onprem-vnet-rg-${random_string.suffix.result}" onprem-resource-group = "onprem-vnet-rg"
prefix-onprem = "onprem" prefix-onprem = "onprem"
} }
@ -111,7 +111,7 @@ resource "azurerm_virtual_machine" "onprem-vm" {
os_profile { os_profile {
computer_name = "${local.prefix-onprem}-vm" computer_name = "${local.prefix-onprem}-vm"
admin_username = var.username admin_username = var.username
admin_password = local.password admin_password = var.password
} }
os_profile_linux_config { os_profile_linux_config {

View File

@ -1,6 +1,6 @@
locals { locals {
spoke1-location = "eastus" spoke1-location = "eastus"
spoke1-resource-group = "spoke1-vnet-rg-${random_string.suffix.result}" spoke1-resource-group = "spoke1-vnet-rg"
prefix-spoke1 = "spoke1" prefix-spoke1 = "spoke1"
} }
@ -44,7 +44,7 @@ resource "azurerm_virtual_network_peering" "spoke1-hub-peer" {
allow_forwarded_traffic = true allow_forwarded_traffic = true
allow_gateway_transit = false allow_gateway_transit = false
use_remote_gateways = true use_remote_gateways = true
depends_on = [azurerm_virtual_network.spoke1-vnet, azurerm_virtual_network.hub-vnet, azurerm_virtual_network_gateway.hub-vnet-gateway] depends_on = [azurerm_virtual_network.spoke1-vnet, azurerm_virtual_network.hub-vnet , azurerm_virtual_network_gateway.hub-vnet-gateway]
} }
resource "azurerm_network_interface" "spoke1-nic" { resource "azurerm_network_interface" "spoke1-nic" {
@ -84,7 +84,7 @@ resource "azurerm_virtual_machine" "spoke1-vm" {
os_profile { os_profile {
computer_name = "${local.prefix-spoke1}-vm" computer_name = "${local.prefix-spoke1}-vm"
admin_username = var.username admin_username = var.username
admin_password = local.password admin_password = var.password
} }
os_profile_linux_config { os_profile_linux_config {

View File

@ -1,6 +1,6 @@
locals { locals {
spoke2-location = "eastus" spoke2-location = "eastus"
spoke2-resource-group = "spoke2-vnet-rg-${random_string.suffix.result}" spoke2-resource-group = "spoke2-vnet-rg"
prefix-spoke2 = "spoke2" prefix-spoke2 = "spoke2"
} }
@ -88,7 +88,7 @@ resource "azurerm_virtual_machine" "spoke2-vm" {
os_profile { os_profile {
computer_name = "${local.prefix-spoke2}-vm" computer_name = "${local.prefix-spoke2}-vm"
admin_username = var.username admin_username = var.username
admin_password = local.password admin_password = var.password
} }
os_profile_linux_config { os_profile_linux_config {

View File

@ -10,8 +10,6 @@ variable "username" {
variable "password" { variable "password" {
description = "Password for Virtual Machines" description = "Password for Virtual Machines"
sensitive = true
default = null
} }
variable "vmsize" { variable "vmsize" {