How do you loop a list using index in Terraform?

A newbie question. How do you do the following python code in Terraform?

count = 0
mylist = []
while count < 5:
    mylist.append("anystring"+ str(count))
    count += 1 

Thanks for any hint

Well, the real case that I need to do is to build the connection string to mongoDB replicaset service. The replication_factor is 3, 5 or 7.

The terraform code that sofar I built:

 locals {
  uri_list = ["@${}1.mongodb.${var.region}${var.db_port}",

  uri_list_out = [
      for uriname in local.uri_list:
           if substr(uriname,length("${}")+1,1) < (var.mongos_config["${var.environment}"]["replication_factor"])

What I expect from

output "uri_list_out" {
  value = local.uri_list_out

is the firts two elements of uri_list but instead I got only [1,2] for replication_factor = 3. Seems if instruction in for also modify the output ???

Appreciate for any hints…


I just realize that for terraform instead of using loop, my problem can be solved using simple lookup element() :smiley: