What would be the best approach to store outputs f...
# golang
l
What would be the best approach to store outputs from resources created in a loop which will the be used later on? Ex: Loop over a # of S3 buckets and store each bucket
Arn
in an Array. That array will then later be used in an IAM policy (all the bucket arns will be used in the Policy
source
) The issue I now have is that I initialize an empty Array that I can fill up, but there doesn't seem to be a way to make the Policy depending until populating the Array is finished. This results in an empty Array as the Policy creation will not wait...
p
I'm kinda new to pulumi as well however I think you can use this https://www.pulumi.com/docs/concepts/inputs-outputs/apply/
Apply
function will wait for the resource to be created and for its properties to be resolved before getting the desired value of the property
I've used it to create kubeconfig for eks cluster(it was failing since there were no cluster) however logic is the same, I think it should work for iam policy part as well
l
yes, I know. The thing is dat I need to populate an Array which sits outside the
Apply
function. This Array is used as an input in another resource but since there is no dependency, that Array (which is empty by default) get read right away and doesn't wait until the loop (which populates the Array) is finished
I think I'll have to make some changes on where I create the resources. The loop is over a Component resource (where the IAM policy is created)
p
I see,
So why don't you run Apply function on this Array, and provide a callback function that creates iamrole for you?
something like this
Copy code
pulumi.Run (...
{
var bucketArns pulumi.StringArrayOutput

for loop{
  bucket, err := new bucket

  bucketArns = pulumi.AppendToStringArrayOutput(bucketArns, bucket.Arn)

} // For loop ends

bucketArns.ApplyT(func(arns []string) error {
iam.NewPolicy(ctx, "testpolicy", args{
Policy: pulumi.Sprintf(`"Resource":%v`, arns)
  }
})
})


}
It's been kinda messy but hope it makes the point
If I get your point properly, I think this should work though 🤔
l
Yeah, maybe something like that. Thanks. I'll have a look at it
🙏 1
pulumi.AppendToStringArrayOutput
doesn't exist though. Regular
append
only works on
StringArray
type.
that's what I had
p
yea, I just wrote it as pseudo sorry for the confusion though 😅
hope it worked
l
well, passing an Array of outputs to the
.All
function does work
🙌 1
p
great news!
glad you made it,
but I'm not still sure that's the correct way, because that callback function can get easily bloated if you just put more over on those resources, like using that iam policy at other resources etc.
you will need to put those into that callback func since all of them requires iam policy to be created,
have you tried putting dependsOn to iam policy to make a explicit dependency for s3 buckets