index 1 out of range for list var.subnet_ids (max 1)

1,711 views
Skip to first unread message

stravze

unread,
Dec 13, 2016, 4:54:33 AM12/13/16
to Terraform
Hello Terraformer's

I was hoping you could help, I've started using modules and I can build out 1 virtual machine using modules etc, as soon as I want increment the number of hosts to 2 or 3 i'm getting this error, I was hoping you can help.


Error creating plan: 1 error(s) occurred:

* index 1 out of range for list var.subnet_ids (max 1) in:
${var.subnet_ids[count.index]}

I have a module structure like


root /
/
/
/- windows_hosts
/
/- subnet
/
/- storage
/
/- resource_group
/

where I have defined var.subnet_ids in my windows_host.tf file

variable "subnet_ids" {
  default     = []
  description = "Subnets for the network interface"
}



# All VMs require a network interface
resource "azurerm_network_interface" "net" {
  count               = "${var.count}"
  name                = "${var.vm_name}${format(var.count_format, var.count_offset + count.index + 1)}"
  location            = "${var.location}"
  resource_group_name = "${var.resource_group_name}"
  dns_servers         = ["${var.dns_servers}"]

  network_security_group_id = "${var.network_security_group_id}"

  ip_configuration {
    name                          = "${var.vm_name}-${format(var.count_format, var.count_offset + count.index + 1) }"
    subnet_id                     = "${var.subnet_ids[count.index]}"
    private_ip_address_allocation = "${var.private_ip_address_allocation}"

  }
}

if anyone can advise or point in the right direction, I would be most grateful

thanks again

regards

James

Lowe Schmidt

unread,
Dec 13, 2016, 6:20:13 AM12/13/16
to terrafo...@googlegroups.com
whats the value of `subnet_ids` ? 

--
Lowe Schmidt | +46 723 867 157

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/db4a9cc6-c6ff-4308-95f9-327579fac086%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

stravze

unread,
Dec 13, 2016, 6:30:51 AM12/13/16
to Terraform
Hi Lowe

Here is my module

module "windows_machines" {
  source              = "./windows_machines"
  name                = "${var.name}"
  vm_name             = "${var.name}-w"
  location            = "${var.location}"
  resource_group_name = "${module.resource_group.name}"
  count               = "${var.win_count}"
  subnet_ids          = "${module.subnet.ips}"

  storage_account_name = "${module.storage_account.name}"
  admin_username       = "${var.admin_username}"
  admin_password       = "${var.admin_password}"

  #public_ip_addresses = "${module.public_ip.ip_addresses}"
  #public_ip_address_ids = "${module.public_ip.ids}"
  storage_primary_blob_endpoint = "${module.storage_account.primary_blob_endpoint}"
}

here is the info in the subnet.tf

resource "azurerm_subnet" "pfe_sn" {
  count                = "${length(var.address_prefixes)}"
  name                 = "${var.name}-ips"
  resource_group_name  = "${var.core_resource_group}"
  virtual_network_name = "${var.core_virtual_network}"
  address_prefix       = "${element(var.address_prefixes,count.index)}"
}

output "ips" {
  value = ["${azurerm_subnet.pfe_sn.*.id}"]
}

output "name" {
}


Regards

James

Lowe Schmidt

unread,
Dec 13, 2016, 8:05:28 AM12/13/16
to terrafo...@googlegroups.com
I still don't see where you get the value for 'variable "subnet_ids"' from, is there perhaps a terraform.tfvars file you forgot to include ? 

--
Lowe Schmidt | +46 723 867 157

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.

James Matthews

unread,
Dec 13, 2016, 9:00:51 AM12/13/16
to terrafo...@googlegroups.com
here is my main.tf file
provider "azurerm" {
  subscription_id = "${var.azure_sub_id}"
  client_id       = "${var.azure_client_id}"
  client_secret   = "${var.azure_client_secret}"
  tenant_id       = "${var.azure_tenant_id}"
}

module "resource_group" {
  source   = "./resource_group"
  name     = "${var.name}"
  location = "${var.location}"
}

module "subnet" {
  source               = "./subnet"
  name                 = "${var.name}"
  resource_group_name  = "${var.core_resource_group}"
  virtual_network_name = "${var.core_virtual_network}"
  address_prefixes     = "${var.subnet_address_prefixes}"
}

module "storage_account" {
  source              = "./storage_account"
  account_name        = "${var.name}"
  resource_group_name = "${module.resource_group.name}"
}

module "public_ip" {
  name                = "${var.name}"
  source              = "./public_ip"
  location            = "${var.location}"
  resource_group_name = "${module.resource_group.name}"
  count               = "${var.count}"
}

module "linux_machines" {
  source              = "./linux_machines"
  name                = "${var.name}"
  vm_name             = "${var.name}-lnx"
  location            = "${var.location}"
  resource_group_name = "${module.resource_group.name}"
  count               = "${var.count}"
  subnet_ids          = "${module.subnet.ips}"

  storage_account_name = "${module.storage_account.name}"
  admin_username       = "${var.admin_username}"
  admin_password       = "${var.admin_password}"

  #public_ip_addresses = "${module.public_ip.ip_addresses}"
  #public_ip_address_ids = "${module.public_ip.ids}"
  storage_primary_blob_endpoint = "${module.storage_account.primary_blob_endpoint}"
}

module "windows_machines" {
  source              = "./windows_machines"
  name                = "${var.name}"
  vm_name             = "${var.name}-w"
  location            = "${var.location}"
  resource_group_name = "${module.resource_group.name}"
  count               = "${var.win_count}"
  subnet_ids          = "${module.subnet.ips}"

  storage_account_name = "${module.storage_account.name}"
  admin_username       = "${var.admin_username}"
  admin_password       = "${var.admin_password}"

  #public_ip_addresses = "${module.public_ip.ip_addresses}"
  #public_ip_address_ids = "${module.public_ip.ids}"
  storage_primary_blob_endpoint = "${module.storage_account.primary_blob_endpoint}"
}

###############################################################################

# Outputs

output "resource_group_name" {
}

output "resource_group_location" {
  value = "${module.resource_group.location}"
}

output "subnet_address_prefixes" {
  value = "${module.subnet.name}"
}

output "azurerm_public_ip" {
  value = "${module.public_ip.ip_address}"
}


output "Linux_private_ip_address" {
  value = "${module.linux_machines.private_ip_address}"
}

output "Windows_private_ip_address" {
  value = "${module.windows_machines.private_ip_address}"
}

output "subnet_ids" {
  value = ["${module.windows_machines.subnet_ids}"]
}

output "avail_set" {
  value = "${module.public_ip.avail_set}"
}


here is my var.tf file from the root of the module (excluding azure subscription informatino)

# Perfect Exchange Subscriptions Variable defaults

variable "location" {
  default = "west europe"
} #{ default = "#{AzureLocation}" }

variable "name" {
  default = "weupexiat02"
} #{ default = "#{AzureResourceGroup}" }

variable "core_resource_group" {
  default = "we-eu-pex-core-rg"
}

variable "core_virtual_network" {
  default = "we-eu-pex-core-vn"
}

variable "subnet_address_prefixes" {
  default = ["10.20.1.0/28"]
}

variable "count" {
  default = 1
}

variable "win_count" {
  default = 2
}

variable "admin_username" {
  default = "insecure-deployer"
}

variable "admin_password" {
  default = "2y2adm;n2016"
}

variable "dns_servers" { 
  default = "10.1.0.1" 
}

here is my subnet.tf file

variable "name" {
  default     = "terraform"
  description = "Name of the virtual network"
}

variable "address_prefixes" {
  default     = ["10.20.255.0/28"]
  description = "List of CIDRs for the subnets."
}

variable "resource_group_name" {
  default     = ""
  description = "Resource group name"
}

variable "virtual_network_name" {
  default     = ""
  description = "Virtual network name"
}

variable "core_resource_group" {
  default = "we-eu-pex-core-rg"
}

variable "core_virtual_network" {
  default = "we-eu-pex-core-vn"
}

resource "azurerm_subnet" "pfe_sn" {
  count                = "${length(var.address_prefixes)}"
  name                 = "${var.name}-ips"
  resource_group_name  = "${var.core_resource_group}"
  virtual_network_name = "${var.core_virtual_network}"
  address_prefix       = "${element(var.address_prefixes,count.index)}"
}

output "ips" {
  value = ["${azurerm_subnet.pfe_sn.*.id}"]
}

output "name" {
}

here is my windows_machine file


variable "name" {
  default     = "terraform"
  description = "Name of the virtual network"
}

variable "location" {
  default     = "west europe"
  description = "Geographic location of the virtual network"
}

variable "role" {
  default     = ""
  description = "Set role tag "
}

variable "datacenter" {
  default     = ""
  description = "Set datacenter tag"
}

variable "count" {
  default     = 1
  description = "Number of VMs to provision."
}

variable "count_offset" {
  default     = 0
  description = "Start server numbering from this value. If you set it to 100, servers will be numbered -101, 102,..."
}

variable "count_format" {
  default     = "%02d"
  description = "Server numbering format (-01, -02, etc.) in printf format"
}

variable "subnet_ids" {
  default     = [""]
  description = "Subnets for the network interface"
}

variable "private_ip_address_allocation" {
  default     = "dynamic"
  description = "IP assignment for the network interface. Can be static or dynamic: if using static please set private_ip_address"
}

variable "private_ip_addresses" {
  default     = [""]
  description = "Rrivate IP address for the network interface. Required if private_ip_address_allocation is static"
}

variable "public_ip_addresses" {
  default     = [""]
  description = "Optional Public IP addresses assigned to the network interface"
}

variable "public_ip_address_ids" {
  default     = [""]
  description = "Optional Public IP address id to assign to the network interface"
}

variable "resource_group_name" {
  default     = ""
  description = "Resource group name"
}

variable "network_security_group_id" {
  default     = ""
  description = "Optional network security group id to attach to instance"
}

variable "vm_name" {
  default     = "vm"
  description = "Name of the Virtual Machine"
}

variable "storage_primary_blob_endpoint" {
  default     = ""
  description = "Azure storage blob endpoint to use to store OS disk images"
}

# Images to use
variable "image_publisher" {
  default     = "MicrosoftWindowsServer"
  description = "OS Image publisher"
}

variable "image_offer" {
  default     = "WindowsServer"
  description = "OS Image offer"
}

variable "image_sku" {
  default     = "2012-R2-Datacenter"
  description = "OS Image sku"
}

variable "image_version" {
  default     = "latest"
  description = "OS Image version"
}

variable "admin_username" {
  default     = "root"
  description = "Admin account"
}

variable "admin_password" {
  default     = ""
  description = "Admin account password (required)"
}

variable "os_disk_name" {
  default     = "osdisk"
  description = "Base name of the OS disk"
}

variable "vm_size" {
  default     = "Standard_A3"
  description = "Size of the VM."
}

# Storage account to use
variable "storage_account_name" {
  default     = ""
  description = "Azure storage account to use to store OS disk images"
}

variable "chef_run_list" {
  default = ["pc_os_base_windows"]
}

variable "chef_env" {
  default = "weupexiat03"
}

variable "chef_version" {
  default = "12.13.37"
}

variable "dns_servers"  { 
  default = "10.1.0.1" }

# All VMs require a network interface
resource "azurerm_network_interface" "net" {
  count               = "${var.count}"
  name                = "${var.vm_name}${format(var.count_format, var.count_offset + count.index + 1)}"
  location            = "${var.location}"
  resource_group_name = "${var.resource_group_name}"
  dns_servers         = ["${var.dns_servers}"]

  network_security_group_id = "${var.network_security_group_id}"

  ip_configuration {
    name                          = "${var.vm_name}-${format(var.count_format, var.count_offset + count.index + 1) }"
    subnet_id                     = ["${var.subnet_ids}"]
    private_ip_address_allocation = "${var.private_ip_address_allocation}"

    #private_ip_address = "${var.private_ip_addresses[count.index]}" #can't be zero yet

    #public_ip_address_id = "${var.public_ip_address_ids[count]}"
  }
}

resource "azurerm_virtual_machine" "vm" {
  count                 = "${var.count}"
  name                  = "${var.vm_name}${format(var.count_format, var.count_offset + count.index + 1)}"
  location              = "${var.location}"
  resource_group_name   = "${var.resource_group_name}"
  network_interface_ids = ["${element(azurerm_network_interface.net.*.id, count.index)}"]
  vm_size               = "${var.vm_size}"

  storage_image_reference {
    publisher = "${var.image_publisher}"
    offer     = "${var.image_offer}"
    sku       = "${var.image_sku}"
    version   = "${var.image_version}"
  }

  storage_os_disk {
    name          = "${var.vm_name}-${var.os_disk_name}${format(var.count_format, var.count_offset + count.index + 1)}"
    vhd_uri       = "https://${var.storage_account_name}.blob.core.windows.net/vhds/${var.vm_name}-os-0${count.index + 1}.vhd"
    caching       = "ReadWrite"
    create_option = "FromImage"
  }

  os_profile {
    computer_name  = "${var.vm_name}${format(var.count_format, var.count_offset+count.index+1)}"
    admin_username = "${var.admin_username}"
    admin_password = "${var.admin_password}"
  }

}

output "vm_ids" {
  value = "${list(azurerm_virtual_machine.vm.*.id)}"
}

output "private_ip_address" {
  value = ["${azurerm_network_interface.net.*.private_ip_address}"]
}

output "subnet_ids" {
  value = ["${azurerm_network_interface.net.*.subnet_id}"]
}


You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/ZoXB8mM3LGI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/CAC-wWcR2H3WJiv2xpbXoPo-myh83A6mhdY_2hq060zcEjAsdwQ%40mail.gmail.com.

James Matthews

unread,
Dec 14, 2016, 4:23:00 AM12/14/16
to terrafo...@googlegroups.com
Hi Lowe,

How does it pick it up, when I only have a 1 as the count, as soon as I choose a higher number I start getting my error.

Regards

James

Teemu Matilainen

unread,
Dec 14, 2016, 7:46:51 AM12/14/16
to terrafo...@googlegroups.com
Hi,

You specify the default value for `subnet_ids` to be `[""]`, i.e. a list with one empty string. So with count index 0 you get an empty string, with 1 you get the index out of range error.

Cheers,
  - Teemu


James Matthews

unread,
Dec 14, 2016, 8:42:01 AM12/14/16
to terrafo...@googlegroups.com
Thanks Teemu,

so do you define subnet_ids` to be `["2"] or have I misunderstood this ?

regards

James

stravze

unread,
Dec 14, 2016, 10:26:54 AM12/14/16
to Terraform
Hi Teemu

how do I get this to reference the subnet that was built in another folder within this module

when I just had flat terraform files in folders (not using modules), I would just define something like

    subnet_id                       = "${azurerm_subnet.pex_network.id}"  

but now using modules, I don't know how to reference / call refer to the folder within the module that defines the subnet 

if that makes sense at all ??

Regards

James
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/ZoXB8mM3LGI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-too...@googlegroups.com.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

Lowe Schmidt

unread,
Dec 14, 2016, 10:58:45 AM12/14/16
to terrafo...@googlegroups.com
You need to have the module output the values you want to use.

So in the network module you need to do: 

output "subnet_ids" {

to be able to use ${module.network.subnet_ids} somewhere else.

--
Lowe Schmidt | +46 723 867 157

To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/46b78172-218f-4d55-b0ba-52b6a7b5fe8e%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages