Skip to content
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
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ module "linux_virtual_machine" {
| <a name="input_suffix"></a> [suffix](#input\_suffix)| Optional suffix that would be added to the end of resources names. It is recommended to use dash at the beginning of variable (e.x., '-example') | `string` | "" | no |
| <a name="input_location"></a> [location](#input\_location)| Azure location | `string` | n/a | yes |
| <a name="input_custom_virtual_machine_name"></a> [custom\_virtual\_machine\_name](#input\_custom\_virtual\_machine\_name)| Specifies the name of the virtual machine name resource | `string` | null | no |
| <a name="input_custom_network_interface_name"></a> [custom\_network\_interface\_name](#input\_custom\_network\_interface\_name)| Specifies the name of the virtual machine interface name resource | `string` | null | no |
| <a name="input_custom_network_interface_first_name"></a> [custom\_network\_interface\_first\_name](#input\_custom\_network\_interface\_first\_name)| Specifies the name of the virtual machine first interface name resource | `string` | null | no |
| <a name="input_custom_network_interface_second_name"></a> [custom\_network\_interface\_second\_name](#input\_custom\_network\_interface\_second\_name)| Specifies the name of the virtual machine second interface name resource | `string` | null | no |
| <a name="input_custom_public_ip_name"></a> [custom\_public\_ip\_name](#input\_custom\_public\_ip\_name)| Specifies the name of the public ip name name resource | `string` | null | no |
| <a name="input_tags"></a> [tags](#input\_tags)| Resource tags | map(any) | {} | no |
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id)| The ID of the Subnet where this Network Interface should be located in. | `string` | n/a | yes |
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id)| The ID of the Subnet where this Network first Interface should be located in. | `string` | n/a | yes |
| <a name="input_subnet_id_second_nic"></a> [subnet_id\_second\_nic](#input\_subnet_id\_second\_nic)| The ID of the Subnet where this Network second Interface should be located in. | `string` | "" | no |
| <a name="input_public_ip_enabled"></a> [public\_ip\_enabled](#input\_public\_ip\_enabled)| Boolean flag to enable Public Ip address creation and assignment to Virtual Machine | `bool` | true | no |
| <a name="input_public_ip_allocation_method"></a> [public\_ip\_allocation\_method](#input\_public\_ip\_allocation_method)| Defines the allocation method for this IP address. Possible values are Static or Dynamic | `string` | Static | no |
| <a name="input_network_interface_private_ip_address_allocation"></a> [network\_interface\_private\_ip_address\_allocation](#input\_network\_interface\_private\_ip_address\_allocation)| The allocation method used for the Private IP Address. | `string` | Dynamic | no |
| <a name="input_network_interface_private_ip_address_allocation"></a> [network\_interface\_private\_ip_address\_allocation](#input\_network\_interface\_private\_ip_address\_allocation)| The allocation method used for the Private IP Address. | `string` | Dynamic | no |
| <a name="input_network_interface_private_ip_address_allocation_nic_second"></a> [network\_interface\_private\_ip\_address\_allocation\_nic\_second](#input\_network\_interface\_private\_ip\_address\_allocation\_nic\_second)| The allocation method used for second nic the Private IP Address. | `string` | Static | no |
| <a name="input_vm_size"></a> [vm\_size](#input\_vm\_size)| The SKU which should be used for this Virtual Machine. | `string` | Standard_F2 | no |
| <a name="input_vm_admin_username"></a> [vm\_admin\_username](#input\_vm\_admin\_username)| The username of the local administrator used for the Virtual Machine. | `string` | adminuser | no |
| <a name="input_vm_admin_password"></a> [vm\_admin\_password](#input\_vm\_admin\_password)| The password of the local administrator used for the Virtual Machine. | `string` | null | no |
Expand All @@ -87,7 +90,8 @@ No modules.
| Name | Type |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| [azurerm_public_ip.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip) | resource |
| [azurerm_network_interface.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) | resource |
| [azurerm_network_interface.first](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) | resource |
| [azurerm_network_interface.second](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) | resource |
| [azurerm_linux_virtual_machine.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine) | resource |


Expand All @@ -98,6 +102,7 @@ No modules.
| <a name="output_id"></a> [id](#output\_id) | The ID of the Linux Virtual Machine |
| <a name="output_identity"></a> [identity](#output\_identity) | linux virtual machine identity |
| <a name="output_public_ip"></a> [public\_ip](#output\_public\_ip) | Linux Virtual Machine public IP address |
| <a name="output_private_ip_second_nic"></a> [private\_ip\_second\_nic](#output\_private\_ip\_second\_nic) | Linux Virtual Machine second nic private IP address |
<!-- END_TF_DOCS -->

## License
Expand Down
40 changes: 29 additions & 11 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
locals {
suffix = length(var.suffix) == 0 ? "" : "-${var.suffix}"
virtual_machine_name = var.custom_virtual_machine_name == null ? "vm-${var.project}-${var.env}-${var.location}${local.suffix}" : "${var.custom_virtual_machine_name}${local.suffix}"
network_interface_name = var.custom_network_interface_name == null ? "nic-${var.project}-${var.env}-${var.location}${local.suffix}" : "${var.custom_network_interface_name}${local.suffix}"
public_ip = var.custom_public_ip_name == null ? "ip-${var.project}-${var.env}-${var.location}${local.suffix}" : "${var.custom_public_ip_name}${local.suffix}"
suffix = length(var.suffix) == 0 ? "" : "-${var.suffix}"
virtual_machine_name = var.custom_virtual_machine_name == null ? "vm-${var.project}-${var.env}-${var.location}${local.suffix}" : "${var.custom_virtual_machine_name}${local.suffix}"
network_interface_first_name = var.custom_network_interface_first_name == null ? "nic-${var.project}-${var.env}-${var.location}${local.suffix}" : "${var.custom_network_interface_first_name}${local.suffix}"
network_interface_name_second = var.custom_network_interface_second_name == null ? "nic-${var.project}-${var.env}-${var.location}${local.suffix}" : "${var.custom_network_interface_second_name}${local.suffix}"
public_ip = var.custom_public_ip_name == null ? "ip-${var.project}-${var.env}-${var.location}${local.suffix}" : "${var.custom_public_ip_name}${local.suffix}"
}

resource "azurerm_public_ip" "this" {
Expand All @@ -14,28 +15,45 @@ resource "azurerm_public_ip" "this" {
allocation_method = var.public_ip_allocation_method
}

resource "azurerm_network_interface" "this" {
name = local.network_interface_name
location = var.location
resource_group_name = var.resource_group
tags = var.tags
resource "azurerm_network_interface" "first" {
name = local.network_interface_first_name
location = var.location
resource_group_name = var.resource_group
tags = var.tags
enable_ip_forwarding = true

ip_configuration {
name = "ip-config-${var.project}-${var.env}-${var.location}"
name = "ip-config-first-${var.project}-${var.env}-${var.location}"
subnet_id = var.subnet_id
private_ip_address_allocation = var.network_interface_private_ip_address_allocation
public_ip_address_id = try(azurerm_public_ip.this[0].id, null)
}
}

resource "azurerm_network_interface" "second" {
count = length(var.subnet_id_second_nic) != null ? 1 : 0

name = local.network_interface_name_second
location = var.location
resource_group_name = var.resource_group
tags = var.tags
enable_ip_forwarding = true

ip_configuration {
name = "ip-config-second-${var.project}-${var.env}-${var.location}"
subnet_id = var.subnet_id_second_nic
private_ip_address_allocation = var.network_interface_private_ip_address_allocation_nic_second
}
}

resource "azurerm_linux_virtual_machine" "this" {
name = local.virtual_machine_name
resource_group_name = var.resource_group
location = var.location
size = var.vm_size
admin_username = var.vm_admin_username
tags = var.tags
network_interface_ids = [azurerm_network_interface.this.id]
network_interface_ids = [azurerm_network_interface.first.id, try(azurerm_network_interface.second[0].id, "")]
admin_password = var.vm_admin_password
disable_password_authentication = var.password_access_enabled ? false : true

Expand Down
5 changes: 5 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ output "private_ip" {
value = try(azurerm_linux_virtual_machine.this.private_ip_address, null)
description = "Linux Virtual Machine private IP address"
}

output "private_ip_second_nic" {
value = try(azurerm_network_interface.second[0].private_ip_address, null)
description = "Linux Virtual Machine second nic private IP address"
}
24 changes: 21 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ variable "custom_virtual_machine_name" {
default = null
}

variable "custom_network_interface_name" {
variable "custom_network_interface_first_name" {
type = string
description = "Specifies the name of the virtual machine interface name resource"
description = "Specifies the name of the virtual machine first interface name resource"
default = null
}

variable "custom_network_interface_second_name" {
type = string
description = "Specifies the name of the virtual machine second interface name resource"
default = null
}

Expand All @@ -50,7 +56,13 @@ variable "tags" {

variable "subnet_id" {
type = string
description = "The ID of the Subnet where this Network Interface should be located in."
description = "The ID of the Subnet where this Network first Interface should be located in."
}

variable "subnet_id_second_nic" {
type = string
description = "The ID of the Subnet where this Network second Interface should be located in."
default = ""
}

variable "public_ip_enabled" {
Expand All @@ -71,6 +83,12 @@ variable "network_interface_private_ip_address_allocation" {
default = "Dynamic"
}

variable "network_interface_private_ip_address_allocation_nic_second" {
type = string
description = "The allocation method used for second nic the Private IP Address."
default = "Static"
}

variable "vm_size" {
type = string
description = "The SKU which should be used for this Virtual Machine."
Expand Down