Compare commits

..

2 Commits

Author SHA1 Message Date
7908433829 slight changes 2024-10-04 17:36:30 +08:00
5620b3e327 Initial put 2024-10-04 17:36:30 +08:00
17 changed files with 745 additions and 400 deletions

View File

@ -0,0 +1,21 @@
# Azure Batch
Deploy an Azure Batch account and two batch pools.
## Terraform resource types
- [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet)
- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group)
- [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string)
- [azurerm_storage_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account)
- [azurerm_batch_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/batch_account)
- [azurerm_batch_pool](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/batch_pool)
## Variables
| Name | Description | Default |
|-|-|-|
| `resource_group_name_prefix` | Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. | rg |
| `resource_group_location` | Location of the resource group. | eastus |
## Example

View File

@ -0,0 +1,114 @@
resource "random_pet" "rg_name" {
prefix = var.resource_group_name_prefix
}
resource "azurerm_resource_group" "rg" {
location = var.resource_group_location
name = random_pet.rg_name.id
}
resource "random_string" "storage_account_name" {
length = 8
lower = true
numeric = false
special = false
upper = false
}
resource "azurerm_storage_account" "example" {
name = random_string.storage_account_name.result
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "random_string" "batch_account_name" {
length = 8
lower = true
numeric = false
special = false
upper = false
}
resource "azurerm_batch_account" "example" {
name = random_string.batch_account_name.result
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
storage_account_id = azurerm_storage_account.example.id
storage_account_authentication_mode = "StorageKeys"
}
resource "random_pet" "azurerm_batch_pool_name" {
prefix = "pool"
}
resource "azurerm_batch_pool" "fixed" {
name = "${random_pet.azurerm_batch_pool_name.id}-fixed-pool"
resource_group_name = azurerm_resource_group.rg.name
account_name = azurerm_batch_account.example.name
display_name = "Fixed Scale Pool"
vm_size = "Standard_A1"
node_agent_sku_id = "batch.node.ubuntu 22.04"
fixed_scale {
target_dedicated_nodes = 2
resize_timeout = "PT15M"
}
storage_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts"
version = "latest"
}
start_task {
command_line = "echo 'Hello World from $env'"
task_retry_maximum = 1
wait_for_success = true
common_environment_properties = {
env = "TEST"
}
user_identity {
auto_user {
elevation_level = "NonAdmin"
scope = "Task"
}
}
}
metadata = {
"tagName" = "Example tag"
}
}
resource "azurerm_batch_pool" "autopool" {
name = "${random_pet.azurerm_batch_pool_name.id}-autoscale-pool"
resource_group_name = azurerm_resource_group.rg.name
account_name = azurerm_batch_account.example.name
display_name = "Auto Scale Pool"
vm_size = "Standard_A1"
node_agent_sku_id = "batch.node.ubuntu 22.04"
auto_scale {
evaluation_interval = "PT15M"
formula = <<EOF
startingNumberOfVMs = 1;
maxNumberofVMs = 25;
pendingTaskSamplePercent = $PendingTasks.GetSamplePercent(180 * TimeInterval_Second);
pendingTaskSamples = pendingTaskSamplePercent < 70 ? startingNumberOfVMs : avg($PendingTasks.GetSample(180 * TimeInterval_Second));
$TargetDedicatedNodes=min(maxNumberofVMs, pendingTaskSamples);
EOF
}
storage_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts"
version = "latest"
}
}

View File

@ -0,0 +1,19 @@
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}
output "storage_account_name" {
value = azurerm_storage_account.example.name
}
output "batch_account_name" {
value = azurerm_batch_account.example.name
}
output "batch_pool_fixed_name" {
value = azurerm_batch_pool.fixed.name
}
output "batch_pool_autopool_name" {
value = azurerm_batch_pool.autopool.name
}

View File

@ -0,0 +1,18 @@
terraform {
required_version = ">=1.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
random = {
source = "hashicorp/random"
version = "~>3.0"
}
}
}
provider "azurerm" {
features {}
}

View File

@ -0,0 +1,11 @@
variable "resource_group_location" {
type = string
default = "eastus"
description = "Location of the resource group."
}
variable "resource_group_name_prefix" {
type = string
default = "rg"
description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
}

View File

@ -0,0 +1,21 @@
# Azure Batch with start task
Deploy an Azure Batch account and two batch pools, one of which has a "start task".
## Terraform resource types
- [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet)
- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group)
- [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string)
- [azurerm_storage_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account)
- [azurerm_batch_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/batch_account)
- [azurerm_batch_pool](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/batch_pool)
## Variables
| Name | Description | Default |
|-|-|-|
| `resource_group_name_prefix` | Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. | rg |
| `resource_group_location` | Location of the resource group. | eastus |
## Example

View File

@ -0,0 +1,114 @@
resource "random_pet" "rg_name" {
prefix = var.resource_group_name_prefix
}
resource "azurerm_resource_group" "rg" {
location = var.resource_group_location
name = random_pet.rg_name.id
}
resource "random_string" "storage_account_name" {
length = 8
lower = true
numeric = false
special = false
upper = false
}
resource "azurerm_storage_account" "example" {
name = random_string.storage_account_name.result
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "random_string" "batch_account_name" {
length = 8
lower = true
numeric = false
special = false
upper = false
}
resource "azurerm_batch_account" "example" {
name = random_string.batch_account_name.result
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
storage_account_id = azurerm_storage_account.example.id
storage_account_authentication_mode = "StorageKeys"
}
resource "random_pet" "azurerm_batch_pool_name" {
prefix = "pool"
}
resource "azurerm_batch_pool" "fixed" {
name = "${random_pet.azurerm_batch_pool_name.id}-fixed-pool"
resource_group_name = azurerm_resource_group.rg.name
account_name = azurerm_batch_account.example.name
display_name = "Fixed Scale Pool"
vm_size = "Standard_D4_v3"
node_agent_sku_id = "batch.node.ubuntu 22.04"
fixed_scale {
target_dedicated_nodes = 2
resize_timeout = "PT15M"
}
storage_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts"
version = "latest"
}
start_task {
command_line = "echo 'Hello World from $env'"
task_retry_maximum = 1
wait_for_success = true
common_environment_properties = {
env = "TEST"
}
user_identity {
auto_user {
elevation_level = "NonAdmin"
scope = "Task"
}
}
}
metadata = {
"tagName" = "Example tag"
}
}
resource "azurerm_batch_pool" "autopool" {
name = "${random_pet.azurerm_batch_pool_name.id}-autoscale-pool"
resource_group_name = azurerm_resource_group.rg.name
account_name = azurerm_batch_account.example.name
display_name = "Auto Scale Pool"
vm_size = "Standard_D4_v3"
node_agent_sku_id = "batch.node.ubuntu 22.04"
auto_scale {
evaluation_interval = "PT15M"
formula = <<EOF
startingNumberOfVMs = 1;
maxNumberofVMs = 25;
pendingTaskSamplePercent = $PendingTasks.GetSamplePercent(180 * TimeInterval_Second);
pendingTaskSamples = pendingTaskSamplePercent < 70 ? startingNumberOfVMs : avg($PendingTasks.GetSample(180 * TimeInterval_Second));
$TargetDedicatedNodes=min(maxNumberofVMs, pendingTaskSamples);
EOF
}
storage_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts"
version = "latest"
}
}

View File

@ -0,0 +1,19 @@
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}
output "storage_account_name" {
value = azurerm_storage_account.example.name
}
output "batch_account_name" {
value = azurerm_batch_account.example.name
}
output "batch_pool_fixed_name" {
value = azurerm_batch_pool.fixed.name
}
output "batch_pool_autopool_name" {
value = azurerm_batch_pool.autopool.name
}

View File

@ -0,0 +1,18 @@
terraform {
required_version = ">=1.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
random = {
source = "hashicorp/random"
version = "~>3.0"
}
}
}
provider "azurerm" {
features {}
}

View File

@ -0,0 +1,11 @@
variable "resource_group_location" {
type = string
default = "eastus"
description = "Location of the resource group."
}
variable "resource_group_name_prefix" {
type = string
default = "rg"
description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
}

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" {