Skip to content

Commit

Permalink
fix: default variables (#51)
Browse files Browse the repository at this point in the history
* fix: default variables

* fmt run

* fix: naming correction for resources
  • Loading branch information
d4kverma committed Feb 21, 2024
1 parent 264b684 commit 12b83e5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 99 deletions.
67 changes: 23 additions & 44 deletions _example/linux-vm/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module "security_group" {
module "key_vault" {
source = "clouddrove/key-vault/azure"
version = "1.1.0"
name = "app3433"
name = "app399433"
environment = "test"
label_order = ["name", "environment", ]
resource_group_name = module.resource_group.resource_group_name
Expand Down Expand Up @@ -128,61 +128,40 @@ module "log-analytics" {
## linux virtual-machine module call.
##-----------------------------------------------------------------------------
module "virtual-machine" {
source = "../../"
depends_on = [module.key_vault]
name = "app"
environment = "test"
label_order = ["environment", "name"]
is_vm_linux = true
enabled = true
machine_count = 1
resource_group_name = module.resource_group.resource_group_name
location = module.resource_group.resource_group_location
disable_password_authentication = true
identity_enabled = true
vm_identity_type = "SystemAssigned"
source = "../../"
depends_on = [module.key_vault]
name = "app"
environment = "test"
resource_group_name = module.resource_group.resource_group_name
location = module.resource_group.resource_group_location
is_vm_linux = true
user_object_id = {
"user1" = {
role_definition_name = "Virtual Machine Administrator Login"
principal_id = data.azurerm_client_config.current_client_config.object_id
},
}
## Network Interface
subnet_id = module.subnet.default_subnet_id
private_ip_address_version = "IPv4"
private_ip_address_allocation = "Static"
primary = true
private_ip_addresses = ["10.0.1.4"]
subnet_id = module.subnet.default_subnet_id
private_ip_addresses = ["10.0.1.4"]
#nsg
network_interface_sg_enabled = true
network_security_group_id = module.security_group.id
## Availability Set
availability_set_enabled = true
platform_update_domain_count = 1
platform_fault_domain_count = 2
## Public IP
public_ip_enabled = true
sku = "Basic"
allocation_method = "Static"
ip_version = "IPv4"
public_ip_enabled = false
## Virtual Machine
vm_size = "Standard_B1s"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADDF4vBXMDnVjYLTLVnOa4lDrrC0CKxRcg8=" # Enter valid p key here
admin_username = "ubuntu"
# admin_password = "P@ssw0rd!123!" # It is compulsory when disable_password_authentication = false
caching = "ReadWrite"
disk_size_gb = 30
storage_image_reference_enabled = true
image_publisher = "Canonical"
image_offer = "0001-com-ubuntu-server-jammy"
image_sku = "22_04-lts-gen2"
image_version = "latest"
enable_disk_encryption_set = true
key_vault_id = module.key_vault.id
addtional_capabilities_enabled = true
ultra_ssd_enabled = false
enable_encryption_at_host = true
key_vault_rbac_auth_enabled = false
vm_size = "Standard_B1s"
public_key = "ssh-rsa AAAA"
admin_username = "ubuntu"
caching = "ReadWrite"
disk_size_gb = 30
image_publisher = "Canonical"
image_offer = "0001-com-ubuntu-server-jammy"
image_sku = "22_04-lts-gen2"
image_version = "latest"

enable_disk_encryption_set = true
key_vault_id = module.key_vault.id
data_disks = [
{
name = "disk1"
Expand Down
39 changes: 14 additions & 25 deletions _example/windows-vm/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ provider "azurerm" {
features {}
}

data "azurerm_client_config" "current_client_config" {}

##-----------------------------------------------------------------------------
## Resource Group module call
## Resource group in which all resources will be deployed.
Expand Down Expand Up @@ -109,43 +111,30 @@ module "virtual-machine" {
machine_count = 1
resource_group_name = module.resource_group.resource_group_name
location = module.resource_group.resource_group_location
create_option = "Empty"
disk_size_gb = 128
provision_vm_agent = true
identity_enabled = true
sa_type = "SystemAssigned"
user_object_id = {
"user1" = {
role_definition_name = "Virtual Machine Administrator Login"
principal_id = "3xxxxxxxxxxxxxxxxxxxe"
principal_id = data.azurerm_client_config.current_client_config.object_id
},
}
## Network Interface
subnet_id = module.subnet.default_subnet_id
private_ip_address_version = "IPv4"
private_ip_address_allocation = "Dynamic"
primary = true
subnet_id = module.subnet.default_subnet_id
private_ip_addresses = ["10.0.1.4"]
#nsg
network_interface_sg_enabled = true
network_security_group_id = module.security_group.id
## Availability Set
availability_set_enabled = true
platform_update_domain_count = 7
platform_fault_domain_count = 3
## Public IP
public_ip_enabled = true
sku = "Basic"
allocation_method = "Static"
ip_version = "IPv4"
computer_name = "app-win-comp"
vm_size = "Standard_B1s"
admin_username = "azureadmin"
admin_password = "Password@123"
image_publisher = "MicrosoftWindowsServer"
image_offer = "WindowsServer"
image_sku = "2019-datacenter"
image_version = "latest"
caching = "ReadWrite"
## Virtual Machine
computer_name = "app-win-comp"
vm_size = "Standard_B1s"
admin_username = "azureadmin"
admin_password = "Password@123"
image_publisher = "MicrosoftWindowsServer"
image_offer = "WindowsServer"
image_sku = "2019-datacenter"
image_version = "latest"
data_disks = [
{
name = "disk1"
Expand Down
24 changes: 12 additions & 12 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module "labels" {
##-----------------------------------------------------------------------------
resource "azurerm_network_interface" "default" {
count = var.enabled ? var.machine_count : 0
name = var.vm_addon_name == null ? format("%s-network-interface-%s", module.labels.id, count.index + 1) : format("%s-network-interface-%s", module.labels.id, var.vm_addon_name)
name = var.vm_addon_name == null ? format("%s-nic-%s", module.labels.id, count.index + 1) : format("%s-nic-%s", module.labels.id, var.vm_addon_name)
resource_group_name = var.resource_group_name
location = var.location
dns_servers = var.dns_servers
Expand All @@ -26,7 +26,7 @@ resource "azurerm_network_interface" "default" {
tags = module.labels.tags

ip_configuration {
name = var.vm_addon_name == null ? format("%s-ip-configuration-%s", module.labels.id, count.index + 1) : format("%s-ip-configuration-%s", module.labels.id, var.vm_addon_name)
name = var.vm_addon_name == null ? format("%s-ip-config-%s", module.labels.id, count.index + 1) : format("%s-ip-config-%s", module.labels.id, var.vm_addon_name)
subnet_id = var.private_ip_address_version == "IPv4" ? element(var.subnet_id, count.index) : ""
private_ip_address_version = var.private_ip_address_version
private_ip_address_allocation = var.private_ip_address_allocation
Expand All @@ -48,7 +48,7 @@ resource "azurerm_network_interface" "default" {
##-----------------------------------------------------------------------------
resource "azurerm_availability_set" "default" {
count = var.enabled && var.availability_set_enabled ? 1 : 0
name = var.vm_addon_name == null ? format("%s-availability-set", module.labels.id) : format("%s-availability-set-%s", module.labels.id, var.vm_addon_name)
name = var.vm_addon_name == null ? format("%s-vm-availability-set", module.labels.id) : format("%s-vm-availability-set-%s", module.labels.id, var.vm_addon_name)
resource_group_name = var.resource_group_name
location = var.location
platform_update_domain_count = var.platform_update_domain_count
Expand All @@ -70,7 +70,7 @@ resource "azurerm_availability_set" "default" {
##-----------------------------------------------------------------------------
resource "azurerm_public_ip" "default" {
count = var.enabled && var.public_ip_enabled ? var.machine_count : 0
name = var.vm_addon_name == null ? format("%s-public-ip-%s", module.labels.id, count.index + 1) : format("%s-public-ip-%s", module.labels.id, var.vm_addon_name)
name = var.vm_addon_name == null ? format("%s-pip-%s", module.labels.id, count.index + 1) : format("%s-pip-%s", module.labels.id, var.vm_addon_name)
resource_group_name = var.resource_group_name
location = var.location
sku = var.sku
Expand Down Expand Up @@ -105,7 +105,7 @@ resource "azurerm_linux_virtual_machine" "default" {
disable_password_authentication = var.disable_password_authentication
network_interface_ids = [element(azurerm_network_interface.default[*].id, count.index)]
source_image_id = var.source_image_id != null ? var.source_image_id : null
availability_set_id = azurerm_availability_set.default[0].id
availability_set_id = var.availability_set_enabled ? azurerm_availability_set.default[0].id : null
proximity_placement_group_id = var.proximity_placement_group_id
encryption_at_host_enabled = var.enable_encryption_at_host
patch_assessment_mode = var.patch_assessment_mode
Expand Down Expand Up @@ -159,7 +159,7 @@ resource "azurerm_linux_virtual_machine" "default" {
}

os_disk {
name = var.vm_addon_name == null ? format("%s-storage-os-disk", module.labels.id) : format("%s-storage-os-disk-%s", module.labels.id, var.vm_addon_name)
name = var.vm_addon_name == null ? format("%s-vm-os-disk", module.labels.id) : format("%s-storage-os-disk-%s", module.labels.id, var.vm_addon_name)
storage_account_type = var.os_disk_storage_account_type
caching = var.caching
disk_encryption_set_id = var.enable_disk_encryption_set ? azurerm_disk_encryption_set.example[0].id : null
Expand Down Expand Up @@ -236,7 +236,7 @@ resource "azurerm_windows_virtual_machine" "win_vm" {
for_each = var.identity_enabled ? [1] : []

content {
type = var.sa_type
type = var.vm_identity_type
identity_ids = var.identity_ids
}
}
Expand All @@ -246,7 +246,7 @@ resource "azurerm_windows_virtual_machine" "win_vm" {
disk_encryption_set_id = var.enable_disk_encryption_set ? azurerm_disk_encryption_set.example[0].id : null
disk_size_gb = var.disk_size_gb
write_accelerator_enabled = var.enable_os_disk_write_accelerator
name = var.vm_addon_name == null ? format("%s-win-storage-data-disk-%s", module.labels.id, count.index + 1) : format("%s-win-storage-data-disk-%s", module.labels.id, var.vm_addon_name)
name = var.vm_addon_name == null ? format("%s-win-vm-storage-data-disk-%s", module.labels.id, count.index + 1) : format("%s-win-storage-data-disk-%s", module.labels.id, var.vm_addon_name)
}
dynamic "source_image_reference" {
for_each = var.source_image_id != null ? [] : [1]
Expand Down Expand Up @@ -315,7 +315,7 @@ resource "azurerm_role_assignment" "ad_role_assignment" {
##-----------------------------------------------------------------------------
resource "azurerm_key_vault_key" "example" {
count = var.enabled && var.enable_disk_encryption_set ? var.machine_count : 0
name = var.vm_addon_name == null ? format("vm-%s-vault-key-%s", module.labels.id, count.index + 1) : format("vm-%s-vault-key-%s", module.labels.id, var.vm_addon_name)
name = var.vm_addon_name == null ? format("%s-vm-vault-key-%s", module.labels.id, count.index + 1) : format("vm-%s-vault-key-%s", module.labels.id, var.vm_addon_name)
key_vault_id = var.key_vault_id
key_type = var.key_type
key_size = var.key_size
Expand Down Expand Up @@ -362,7 +362,7 @@ resource "azurerm_managed_disk" "data_disk" {
data_disk : data_disk,
}
} : {}
name = format("%s-%s-managed-disk", module.labels.id, each.value.data_disk.name)
name = format("%s-%s-vm-managed-disk", module.labels.id, each.value.data_disk.name)
resource_group_name = var.resource_group_name
location = var.location
storage_account_type = lookup(each.value.data_disk, "storage_account_type", "StandardSSD_LRS")
Expand Down Expand Up @@ -411,7 +411,7 @@ resource "azurerm_virtual_machine_extension" "vm_insight_monitor_agent" {
##-----------------------------------------------------------------------------
resource "azurerm_monitor_diagnostic_setting" "pip_gw" {
count = var.enabled && var.diagnostic_setting_enable && var.public_ip_enabled ? var.machine_count : 0
name = var.vm_addon_name == null ? format("%s-vm-pip-diagnostic-log-%s", module.labels.id, count.index + 1) : format("%s-vm-pip-%s-diagnostic-log", module.labels.id, var.vm_addon_name)
name = var.vm_addon_name == null ? format("%s-vm-pip-diag-log-%s", module.labels.id, count.index + 1) : format("%s-vm-pip-%s-diagnostic-log", module.labels.id, var.vm_addon_name)
target_resource_id = azurerm_public_ip.default[0].id
storage_account_id = var.storage_account_id
eventhub_name = var.eventhub_name
Expand Down Expand Up @@ -442,7 +442,7 @@ resource "azurerm_monitor_diagnostic_setting" "pip_gw" {
##-----------------------------------------------------------------------------
resource "azurerm_monitor_diagnostic_setting" "nic_diagnostic" {
count = var.enabled && var.diagnostic_setting_enable ? var.machine_count : 0
name = var.vm_addon_name == null ? format("%s-pe-vm-nic-diagnostic-log-%s", module.labels.id, count.index + 1) : format("%s-pe-vm-nic-%s-diagnostic-log-%", module.labels.id, var.vm_addon_name)
name = var.vm_addon_name == null ? format("%s-vm-pe-nic-diag-log-%s", module.labels.id, count.index + 1) : format("%s-vm-pe-nic-%s-diagnostic-log-%", module.labels.id, var.vm_addon_name)
target_resource_id = azurerm_network_interface.default[0].id
storage_account_id = var.storage_account_id
eventhub_name = var.eventhub_name
Expand Down
30 changes: 12 additions & 18 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ variable "repository" {

variable "enabled" {
type = bool
default = false
default = true
description = "Flag to control the module creation."
}

variable "machine_count" {
type = number
default = 0
default = 1
description = "Number of Virtual Machines to create."
}

Expand Down Expand Up @@ -182,13 +182,13 @@ variable "sku" {

variable "allocation_method" {
type = string
default = ""
default = "Static"
description = "Defines the allocation method for this IP address. Possible values are Static or Dynamic."
}

variable "ip_version" {
type = string
default = ""
default = "IPv4"
description = "The IP Version to use, IPv6 or IPv4."
}

Expand Down Expand Up @@ -225,16 +225,10 @@ variable "boot_diagnostics_enabled" {

variable "identity_enabled" {
type = bool
default = false
default = true
description = "Whether identity block is enabled."
}

variable "sa_type" {
type = string
default = ""
description = "Specifies the identity type of the Storage Account. At this time the only allowed value is SystemAssigned."
}

variable "vm_size" {
type = string
default = ""
Expand All @@ -255,7 +249,7 @@ variable "license_type" {

variable "disable_password_authentication" {
type = bool
default = false
default = true
description = "Specifies whether password authentication should be disabled."
}

Expand Down Expand Up @@ -291,7 +285,7 @@ variable "ultra_ssd_enabled" {

variable "vm_identity_type" {
type = string
default = ""
default = "SystemAssigned"
description = "The Managed Service Identity Type of this Virtual Machine. Possible values are SystemAssigned and UserAssigned."
}

Expand Down Expand Up @@ -365,7 +359,7 @@ variable "create_option" {

variable "caching" {
type = string
default = ""
default = "ReadWrite"
description = "Specifies the caching requirements for the OS Disk. Possible values include None, ReadOnly and ReadWrite."
}

Expand All @@ -377,7 +371,7 @@ variable "computer_name" {

variable "disk_size_gb" {
type = number
default = 8
default = 30
description = "Specifies the size of the OS Disk in gigabytes."
}

Expand All @@ -389,7 +383,7 @@ variable "write_accelerator_enabled" {

variable "storage_image_reference_enabled" {
type = bool
default = false
default = true
description = "Whether storage image reference is enabled."
}

Expand Down Expand Up @@ -419,7 +413,7 @@ variable "image_sku" {

variable "image_version" {
type = string
default = ""
default = "latest"
description = "Specifies the version of the image used to create the virtual machine."
}

Expand Down Expand Up @@ -472,7 +466,7 @@ variable "key_vault_id" {

variable "enable_encryption_at_host" {
type = bool
default = false
default = true
description = "Flag to control Disk Encryption at host level"
}

Expand Down

0 comments on commit 12b83e5

Please sign in to comment.