Getting around Terraform Indexing

Terraform has some good ways to do interpolation that can be simple.  However, I think as I’ve tried to make Terraform do more of what Ansible does, which might be outside its scope, I run into issues.

One issue I had was creating a list of all the servers in the cluster that I wanted to define static routes for.  To do this I used Terraform’s formatlist built in function.  The issue I had was that I couldn’t get a good index on it.  I searched and found this issue.  Seems I’m not the only one with an issue!  So that is comforting.  So I fought with it for a few minutes then finally went to bed exhausted.  This morning I woke up renewed and thought of a great plan!  I love how your mind works while you sleep.

I defined my compute node with a metadata to keep the count!

Notice in the metadata section above where I give each node a worker_number, which just corresponds to the count.

Later, where I’m going through and creating a template with a list of all the servers I used this bad boy variable to give me the iteration number:

This works great and gives me the count that I need!