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
Hendro

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 = ["@${alicloud_mongodb_instance.tyk_mongodb-test.id}1.mongodb.${var.region}.rds.aliyuncs.com:${var.db_port}",
              "@${alicloud_mongodb_instance.tyk_mongodb-test.id}2.mongodb.${var.region}.rds.aliyuncs.com:${var.db_port}",
              "@${alicloud_mongodb_instance.tyk_mongodb-test.id}3.mongodb.${var.region}.rds.aliyuncs.com:${var.db_port}",
              "@${alicloud_mongodb_instance.tyk_mongodb-test.id}4.mongodb.${var.region}.rds.aliyuncs.com:${var.db_port}",
              "@${alicloud_mongodb_instance.tyk_mongodb-test.id}5.mongodb.${var.region}.rds.aliyuncs.com:${var.db_port}",
              "@${alicloud_mongodb_instance.tyk_mongodb-test.id}6.mongodb.${var.region}.rds.aliyuncs.com:${var.db_port}" 
  ]

  uri_list_out = [
      for uriname in local.uri_list:
           lower(uriname) 
           if substr(uriname,length("${alicloud_mongodb_instance.tyk_mongodb-test.id}")+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…

Hendro

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