Use nested loops with count

1,149 views
Skip to first unread message

Vikas Kumar

unread,
Feb 25, 2017, 4:08:37 AM2/25/17
to Terraform

Hey All,


I am trying to use a nested loop in terraform. I have two list variables list_of_allowed_accountsand list_of_images, and looking to iterate over list list_of_images and then iterate over list list_of_allowed_accounts.

Here is my terraform code.


variable "list_of_allowed_accounts" {
  type    
= "list"
 
default = ["111111111", "2222222"]
}

variable
"list_of_images" {
  type    
= "list"
 
default = ["alpine", "java", "jenkins"]
}

data
"template_file" "ecr_policy_allowed_accounts" {
 
template = "${file("${path.module}/ecr_policy.tpl")}"
  vars
{
    count
= "${length(var.list_of_allowed_accounts)}"
    account_id
= "${element(var.list_of_allowed_accounts, count.index)}"
 
}
}

resource
"aws_ecr_repository_policy" "repo_policy_allowed_accounts" {
  count
= "${length(var.list_of_images)}"
  repository
= "${element(aws_ecr_repository.images.*.id, count.index)}"
  count
= "${length(var.list_of_allowed_accounts)}"
  policy
= "${data.template_file.ecr_policy_allowed_accounts.rendered}"
}



This is a bash equivalent of what I am trying to do.


for image in alpine java jenkins
do
 
for account_id in 111111111 2222222
 
do
   
// call template here using variable 'account_id' and 'image'
 
done
done


Thanks for your time here.


Cheers,

Vikas

Brian Lalor

unread,
Feb 25, 2017, 6:21:25 AM2/25/17
to terrafo...@googlegroups.com
I don't believe this is possible directly. I think you can accomplish that with an external data source that combines the variables, however. https://www.terraform.io/docs/providers/external/data_source.html

--
Brian Lalor

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/0cfb4902-8a98-495f-b0b9-fa5b69d95b76%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vikas Kumar

unread,
Feb 25, 2017, 6:37:34 AM2/25/17
to Terraform
Thanks Brian, trying that out. 

- Vikas

Vikas Kumar

unread,
Feb 25, 2017, 8:44:42 AM2/25/17
to Terraform
Hey Brian,

I did tried this, but list variable is not accepted in external data source. I have raised a feature request for the same.

- Vikas

Brian Lalor

unread,
Feb 25, 2017, 9:01:48 AM2/25/17
to terrafo...@googlegroups.com
You can use join and split. And you also cannot use a computed value for count. :-(

-- 
Brian Lalor
bla...@bravo5.org

Vikas Kumar

unread,
Feb 27, 2017, 4:54:22 PM2/27/17
to Terraform
Thanks Brian, join worked.
Reply all
Reply to author
Forward
0 new messages