Hi there. I'm new to Pulumi. My team is using Terr...
# general
s
Hi there. I'm new to Pulumi. My team is using Terraform, and we're trying to use Pulumi as a POC. I'm wondering what the best practice is for getting a list from the Terraform state stored in an S3 backend and iterating it over the Pulumi stack. the error i getting is:
Copy code
for _ in infra_state.outputs["some_output_key"]:
    print(_)
#     TypeError: 'Output' object is not iterable, consider iterating the underlying value inside an 'apply'
t
Welcome to using Pulumi! So basically when you're running your program before anything is complete it's stored as what's called an output as you see but until it's actually provisioned and you execute the polynomial up it's not available so what you have to do is run it inside an apply method or in a interpolated string depending on the language that you're using and then that value will be available as an output because you have actually provisioned the resource and therefore it is available
If what you're trying to read already exists in an S3 bucket then you can just read it using the regular S3 API since pulumi is just a regular program
Does that make it clearer?
s
yes, I understand the logic, but I still can't get the results even with the apply method. can you please give an example, i using python? @thankful-flower-8175
t
I'm out and about right now and haven't used python so off the tip of my head I'm not sure but id seriously (no joke) recommend putting your code and the error into Pulumi ai and asking it. It has worked a lot for me in the past. Lmk if that doesn't work and I'll take a beat to see if I can figure it out
d
It's worth familiarising yourself with Inputs and Outputs: https://www.pulumi.com/docs/concepts/inputs-outputs/#outputs As well as how Apply works: https://www.pulumi.com/docs/concepts/inputs-outputs/apply/
You can't use values from Outputs in your top level script, the iteration has to happen within the Apply
s
Thanks but that will allow me iterate with the for loop to build resources?
d
As a general rule, you shouldn't make resources inside an apply as changes don't always get picked up. I'm not sure if the tf state provider can be safely used that way
l
In theory, this might be a time when it's ok to break the rule, since presumably the intention is to create the resources this way only once. However, it is likely that this is not the best approach. What is the goal? Are you trying to have Pulumi manage all your existing resources? Creating the resources from the existing state will (presumably) result in two of every resource: the Terraform one and the Pulumi one. It would be better to either translate the Terraform code to Pulumi (and ignore the existing state), or import the resources to Pulumi (to take over managing the existing resources). Pulumi has a good array of ways to migrate from Terraform. Importing is probably the most popular and suits most cases (https://www.pulumi.com/docs/concepts/vs/terraform/#adopting) but translating code is useful for many use cases too (https://www.pulumi.com/docs/concepts/vs/terraform/#providers-converting).