Skip to content

Instantly share code, notes, and snippets.

@rswrz
Last active July 12, 2024 11:32
Show Gist options
  • Select an option

  • Save rswrz/2cdca1a738c247144844c0e4bac34d1f to your computer and use it in GitHub Desktop.

Select an option

Save rswrz/2cdca1a738c247144844c0e4bac34d1f to your computer and use it in GitHub Desktop.

Complex Terraform Input Variable

This is an example of a complex Terraform input variable with a description written in Markdown. terraform-docs can then generate a lovely README.

For complex descriptions, it is recommended to use the terraform-docs formatter markdown-document.

Required Inputs

The following input variables are required:

Type: string

Type: string

Optional Inputs

The following input variables are optional (have default values):

Description: A list of network interface objects to be created and attached to the virtual machine.

Required object parameters:

  • name - The name of the Network Interface. Changing this forces a new resource to be created.

Optional object parameters:

  • accelerated_networking_enabled - Should Accelerated Networking be enabled? Defaults to false.
  • auxiliary_mode - Specifies the auxiliary mode used to enable network high-performance feature on Network Virtual Appliances (NVAs). This feature offers competitive performance in Connections Per Second (CPS) optimization, along with improvements to handling large amounts of simultaneous connections. Possible values are AcceleratedConnections, Floating, MaxConnections and None.
  • auxiliary_sku - Specifies the SKU used for the network high-performance feature on Network Virtual Appliances (NVAs). Possible values are A8, A4, A1, A2 and None.
  • dns_servers - A list of IP Addresses defining the DNS Servers which should be used for this Network Interface.
  • edge_zone - Specifies the Edge Zone within the Azure Region where this Network Interface should exist. Changing this forces a new Network Interface to be created.
  • internal_dns_name_label - The (relative) DNS Name used for internal communications between Virtual Machines in the same Virtual Network.
  • ip_forwarding_enabled - Should IP Forwarding be enabled? Defaults to false.
  • ip_configurations – A list ip configuration opbjects.

A ip_configurations object supports the following optional parameters:

  • name - A name used for this IP Configuration. Default name is ipconfigN, where N is a consecutive number.
  • gateway_load_balancer_frontend_ip_configuration_id - The Frontend IP Configuration ID of a Gateway SKU Load Balancer.
  • subnet_id - The ID of the Subnet where this Network Interface should be located in.
  • private_ip_address_version - The IP Version to use. Possible values are IPv4 or IPv6. Defaults to IPv4.
  • private_ip_address_allocation - The allocation method used for the Private IP Address. Possible values are Dynamic and Static.
  • public_ip_address_id - Reference to a Public IP Address to associate with this NIC
  • primary - Is this the Primary IP Configuration? Must be true for the first ip_configuration when multiple are specified. Defaults to false.
  • private_ip_address - The Static IP Address which should be used.

Example

module "example" {
  source              = "cloudeteer/vm/azurerm"
  name                = "vm-example-dev-we-01"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  image     = "Win2022Datacenter"
  network_interfaces = [
    {
      name        = "nic-example-dev-we-01

      ip_configurations = [
        {
          subnet_id = azurerm_subnet.example.id
        }
      ]
    }
  ]
}

Type:

list(
    object({
      name                           = string
      accelerated_networking_enabled = optional(bool)
      auxiliary_mode                 = optional(string)
      auxiliary_sku                  = optional(string)
      dns_servers                    = optional(list(string))
      edge_zone                      = optional(string)
      internal_dns_name_label        = optional(string)
      ip_forwarding_enabled          = optional(bool)

      ip_configurations = optional(list(object({
        gateway_load_balancer_frontend_ip_configuration_id = optional(string)
        name                                               = optional(string)
        primary                                            = optional(bool)
        private_ip_address                                 = optional(string)
        private_ip_address_allocation                      = optional(string)
        private_ip_address_version                         = optional(string)
        public_ip_address_id                               = optional(string)
        subnet_id                                          = optional(string)
      })))
    })
  )

Default: []

locals {
network_interfaces = coalescelist(var.network_interfaces, tolist([
{
name = "nic-default"
}
]))
}
resource "azurerm_network_interface" "this" {
for_each = { for item in local.network_interfaces : item.name => item }
name = each.value.name
location = var.location
resource_group_name = var.resource_group_name
dynamic "ip_configuration" {
for_each = coalescelist(try(each.value.ip_configurations, []), [{ _ : null }])
content {
name = try(ip_configuration.value.name, "ipconfig${try((index(ip_configuration.value.*.name, ip_configuration.value.name) + 1), 1)}")
private_ip_address_allocation = try(ip_configuration.private_ip_address_allocation, "Dynamic")
subnet_id = try(ip_configuration.subnet_id, null)
}
}
}
variable "location" {
type = string
}
variable "resource_group_name" {
type = string
}
variable "network_interfaces" {
description = <<-EOT
A list of network interface objects to be created and attached to the virtual machine.
Required parameters:
- `name` - The name of the Network Interface. Changing this forces a new resource to be created.
Optional parameters:
- `accelerated_networking_enabled` - Should Accelerated Networking be enabled? Defaults to `false`.
- `auxiliary_mode` - Specifies the auxiliary mode used to enable network high-performance feature on Network Virtual Appliances (NVAs). This feature offers competitive performance in Connections Per Second (CPS) optimization, along with improvements to handling large amounts of simultaneous connections. Possible values are `AcceleratedConnections`, `Floating`, `MaxConnections` and `None`.
- `auxiliary_sku` - Specifies the SKU used for the network high-performance feature on Network Virtual Appliances (NVAs). Possible values are `A8`, `A4`, `A1`, `A2` and `None`.
- `dns_servers` - A list of IP Addresses defining the DNS Servers which should be used for this Network Interface.
- `edge_zone` - Specifies the Edge Zone within the Azure Region where this Network Interface should exist. Changing this forces a new Network Interface to be created.
- `internal_dns_name_label` - The (relative) DNS Name used for internal communications between Virtual Machines in the same Virtual Network.
- `ip_forwarding_enabled` - Should IP Forwarding be enabled? Defaults to `false`.
- `ip_configurations` – A list ip configuration opbjects.
A `ip_configurations` object supports the following optional parameters:
- `name` - A name used for this IP Configuration. Default name is `ipconfigN`, where `N` is a consecutive number.
- `gateway_load_balancer_frontend_ip_configuration_id` - The Frontend IP Configuration ID of a Gateway SKU Load Balancer.
- `subnet_id` - The ID of the Subnet where this Network Interface should be located in.
- `private_ip_address_version` - The IP Version to use. Possible values are IPv4 or IPv6. Defaults to IPv4.
- `private_ip_address_allocation` - The allocation method used for the Private IP Address. Possible values are Dynamic and Static.
- `public_ip_address_id` - Reference to a Public IP Address to associate with this NIC
- `primary` - Is this the Primary IP Configuration? Must be true for the first ip_configuration when multiple are specified. Defaults to false.
- `private_ip_address` - The Static IP Address which should be used.
Example:
```hcl
module "example" {
source = "cloudeteer/vm/azurerm"
name = "vm-example-dev-we-01"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
image = "Win2022Datacenter"
network_interfaces = [
{
name = "nic-example-dev-we-01
ip_configurations = [
{
subnet_id = azurerm_subnet.example.id
}
]
}
]
}
```
EOT
type = list(
object({
name = string
accelerated_networking_enabled = optional(bool)
auxiliary_mode = optional(string)
auxiliary_sku = optional(string)
dns_servers = optional(list(string))
edge_zone = optional(string)
internal_dns_name_label = optional(string)
ip_forwarding_enabled = optional(bool)
ip_configurations = optional(list(object({
gateway_load_balancer_frontend_ip_configuration_id = optional(string)
name = optional(string)
primary = optional(bool)
private_ip_address = optional(string)
private_ip_address_allocation = optional(string)
private_ip_address_version = optional(string)
public_ip_address_id = optional(string)
subnet_id = optional(string)
})))
})
)
default = []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment