I am pretty new to Terraform and I am using this function to re-use same list of subnets to launch more instances than subnets I have. (It'll just loop over) This works great if I provide my own map of subnets but the data in the remote state is a tuple and I get this error:
Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument. data.terraform_remote_state.vpc.outputs.private_subnets is tuple with 4 elements
I have also tried the tomap( function but this fails with:
Invalid value for "v" parameter: cannot convert tuple to map of any single
type.
Here is my code:
count = var.instance_count
ami = var.ami
instance_type = "t2.medium"
subnet_id = lookup(data.terraform_remote_state.vpc.outputs.private_subnets, count.index%length(data.terraform_remote_state.vpc.outputs.private_subnets))
vpc_security_group_ids = ["${data.terraform_remote_state.foo_sg.outputs.foo_sg_id}"]
key_name = var.key_name
iam_instance_profile = var.iam_instance_profile
user_data = <<-EOF
#!/bin/bash
hostnamectl set-hostname --static "${var.app_name}-${count.index + 1}.${data.terraform_remote_state.vpc.outputs.private_zone_domain_name}"
echo "127.0.0.1 localhost.localdomain localhost4 localhost4.localdomain4 ${var.app_name}-${count.index + 1}.${data.terraform_remote_state.vpc.outputs.private_zone_domain_name} localhost" > hosts
echo "::1 localhost localhost.localdomain localhost6 localhost6.localdomain6" >> hosts
EOF
tags = {
Name = "${var.app_name}-${count.index +1}.${data.terraform_remote_state.vpc.outputs.private_zone_domain_name}"
}
Like I said my goal is to re-use the 4 subnets in that remote state I have, so if I want 6 instances it would loop through the 4 I have and the 5th and 6th instance would be on subnet 1 and 2 in the Tuple. Any suggestions would be appreciated!