I read https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9 and others, but I still couldn’t understand how I can iterate over two lists. For example, I have a list of 10 names. I need to create 10 S3 buckets, 10 AWS Transfer servers, and 20 users, 2 per server (readonly and readwrite).
After I created S3 buckets and Transfer servers, I have two lists…
My pseudo code is like this (in bash now), but I want to implement this end-to-end flow in Terraform :
for(i=0; i<10; i++)
do something for bucket[i]
create some IAM policies
do something for server[i]
create readonly user for bucket[i]
create readwrite user for bucket[i]
end
Hi! It looks like you need a list of maps to iterate over. If the buckets, iam transfer servers and users have strickly-defined sizes with respect to each other, you could do something like:
items = [
{
name = "name"
bucket = "bucket"
iam_policy = ...
read_user = "reader"
write_user = "writer"
},
{ ... }
]
You can then do something like :
for_each = var.items
...
Have you considered something like this?
1 Like
I would suggest you need 4 resources, each using a for_each
to loop over your list.
You’d want a s3 bucket resource you create those, a transfer server resource to create those and then two resources to create the IAM users (one for read-only, the other for read/write).
1 Like
Thank you, it’s an interesting idea, but I’m not sure how I can use a loop for populate this structure. I’d like to start from something like
bucket_names = ["bucket1","bucket2","bucket3"]
as input.
Yep. Your first resource would have a for_each
over that list to create the bucket. Then the second resource would loop of that to create the servers, then one to create the read-only user and one to create the read/write user.
1 Like