0
votes

I am facing a problem with Terraform (v0.12) to create multiple instances using count variable and subnet id's list, where the count is greater than the length of the subnet id's list.

For example;

resource "aws_instance" "main" {
  count                     = 20
  ami                       = var.ami_id
  instance_type             = var.instance_type
  # ...
  subnet_id                 = var.subnet_ids_list[count.index]
}

Where my count is '20' and length(var.subnet_ids_list) is 2. It throws the following error:

count.index is 2
    var.instance_subnet_id is tuple with 2 elements

The given key does not identify an element in this collection value.

I tried to make the "subnet_ids_list" as string with comma-separated and used "split", but it too give the same error.

Later thought to append subnet elements to "subnet_ids_list" in order to make it to "20". something like;

Python 2.7
>>> subnet_ids_list = subnet_ids_list * 10

Can someone help me with how to achieve similar with Terraform or any other approaches to solve this problem.

Original like;

subnet_ids_list = ["sub-1", "sub-2"]

Converted to - satisfy the value provided to count;

subnet_ids_list = ["sub-1", "sub-2", "sub-1", "sub-2",....., "sub-1", "sub-2",] (length=20).

I don't want to use AWS autoscaling groups for this purpose.

2

2 Answers

0
votes

It doesn't make sense to create a new subnet whenever you need to spin up a new EC2. I'd recommend you to take a look at the official documentation about the basics of VPC and subnets: https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Subnets.html#vpc-subnet-basics

For example, if you create a VPC with CIDR block 10.0.0.0/24, it supports 256 IP addresses. You can break this CIDR block into two subnets, each supporting 128 IP addresses. One subnet uses CIDR block 10.0.0.0/25 (for addresses 10.0.0.0 - 10.0.0.127) and the other uses CIDR block 10.0.0.128/25 (for addresses 10.0.0.128 - 10.0.0.255).

In your Terraform example, it looks like you have 2 subnets (private and public?), so your counter must be rather 0 or 1 when accessing subnet_ids_list. Even a better solution would be to tag your subnets: https://www.terraform.io/docs/providers/aws/r/subnet.html#inner

You might have another counter though to control number of instances. Hope it helps!

EDIT: Based on your comments, a Map would be a better data structure to control instance/subnet. Key could be the instance or the subnet itself, e.g. { "aws_instance" = "sub-1" }

Reference: https://www.terraform.io/docs/configuration-0-11/variables.html#maps

0
votes

You can use the element function if you need to loop back through a list of things as mentioned in the linked documentation:

The index is zero-based. This function produces an error if used with an empty list.

Use the built-in index syntax list[index] in most cases. Use this function only for the special additional "wrap-around" behavior described below.

> element(["a", "b", "c"], 3)
a