https://pulumi.com logo
#python
Title
# python
b

broad-morning-80838

12/24/2022, 10:57 PM
For context, I'm generating a yaml file with a list of existing AWS resources, then I want to import the resources defined in the yaml file if they don't already exist in the pulumi state. If they do exist, I just want to manage them as an existing pulumi resource. I feel like I'm possibly missing something obvious but I can't seem to figure out how to inspect the stack resources in a python script.
c

clever-painter-96148

12/25/2022, 5:28 PM
Hi @broad-morning-80838
Is there a way to get a list of resources in the stack with python? Something equivalent to
pulumi stack --show-urns
I'd go with
pulumi stack export
.
the yaml config will be used for creating new resource
Did you know that Pulumi has a yaml backend? https://www.pulumi.com/docs/intro/languages/yaml/
b

broad-morning-80838

12/25/2022, 5:37 PM
Hi @clever-painter-96148 thanks for the response! I was hoping to do it directly inside of a python script without shelling out and parsing cli output. I'd assumed something like
pulumi.stack.list_resources()
existed, but so far haven't found anything.
I have looked at the yaml backend! In my case, I'm using a single yaml file for a few different things, so I didn't want to necessarily adhere to the yaml format required for pulumi YAML
Looks like I can just subprocess
pulumi stack export
and make things work that way for now
Thanks @clever-painter-96148!
c

clever-painter-96148

12/25/2022, 8:05 PM
You can do this using the automation api : pulumi.automation.select_stack(‘foo’).export_stack()
b

broad-morning-80838

12/26/2022, 8:23 PM
Ohh thank you! I haven't looked at the automation API. Thanks so much @clever-painter-96148
c

clever-painter-96148

12/26/2022, 8:30 PM
Just starting with it and I already love it 😁
b

broad-morning-80838

12/26/2022, 8:47 PM
@clever-painter-96148 sorry, hopefully last question! Have you used
export_stack
to get a list of resources in the stack? I'm not sure how to use the type
pulumi.automation._workspace.Deployment
I see that you can use it to import state back into the stack, but in my case I'm hoping to use it to conditionally either import resources that don't exist in the stack but do exist in AWS, or create/update resources that are net new in my config file. Ultimately I'm trying to use this yaml config to drive the management of these resources, but since many of them exist in AWS already and I'm trying to bring them under management, I need to handle those resources differently than resources created in the future with this program.
c

clever-painter-96148

12/26/2022, 9:06 PM
For example, here is how to get URNs:
Copy code
>>> from pulumi.automation import select_stack
>>> deployment=select_stack('dev',work_dir='.').export_stack().deployment
>>> urns=[r['urn'] for r in deployment['resources']]
b

broad-morning-80838

12/26/2022, 10:02 PM
Ah!
.deployment
is what I was missing. Thanks a million @clever-painter-96148
c

clever-painter-96148

12/26/2022, 10:28 PM
You're welcome. Have fun! 🙂