Terraform loops with templates
One use case for looping in terraform is if you need to provide a list of values. The example below shows the installtion of the nginx-ingress Helm chart to a Kubernetes cluster. In this particular case we’re setting up TCP Services which requires a list of one of more arguments. We could just as easily provide a values file however the technique show below deomonstrates the use of a templating and looping nicely
When running:
terraform apply \
-var="tcp_services_string=4092: workflow/kcp-cp-kafka-0:9092,4093: workflow/kcp-cp-kafka-1:9092,4094: workflow/kcp-cp-kafka-2:9092" -auto-approve
The folling values are produced:
tcp:
4092: "workflow/kcp-cp-kafka-0:9092"
4093: "workflow/kcp-cp-kafka-1:9092"
4094: "workflow/kcp-cp-kafka-2:9092"