Is it possible to break up a large `__main__.py` d...
# general
b
Is it possible to break up a large
__main__.py
definition into smaller, more manageable py files. For example, I'm specifying multiple AWS SES templates, where I'd like that IAC code to live in its own file. I looked through the docs, but it's not clear on how to achieve in a way similar to how terraform allows modules. A little bit like the example image...
Also, is it possible to rename
__main__.py
and then specify pulumi cli to look for a custom name?
b
Terrific thanks @billowy-army-68599
@billowy-army-68599 looking through the example the separate pulumi files like network.py are defining classes for the resources defined in main.py which is cool, but I should have been more specific in my question. Is it possible to create and deploy resources in code located outside of main.py? For example, if I wanted to create my s3 buckets in s3.py, and my lambda functions in lambda.py?
b
you can define the files, but you’ll always need a
main.py
entrypoint
Pulumi won’t just interpolate all files in a directory
b
Perfect thanks @billowy-army-68599 . Whats the best way to ensure pulumi can see those additional files with their own resournces - some kind of import? Sorry for the questions, I'm coming from a
.tf
world where terraform will just automatically see anything with that extension. It's a mental model shift w pulumi, IAC py files and actual application code py files - where they can't be auto-detected.
b
yes it’s definitely a shit, but Pulumi doesn’t violate the principal of least surprise where terraform just randomly throws what’s in a file at you 😉 You simply define the Component like so: https://github.com/pulumi/examples/blob/master/gcp-py-network-component/network.py#L13-L51 Import it: https://github.com/pulumi/examples/blob/master/gcp-py-network-component/__main__.py#L5 Then declare the resource: https://github.com/pulumi/examples/blob/master/gcp-py-network-component/__main__.py#L12-L15
b
Ha - valid point... and with some tinkering I managed to figure it out at the same time you were replying. Makes perfect sense now. Defined the resource (an AWS SES template) as a function in a separate
ses.py
file, exported the
template()
function, imported into
__main__.py
and works like a charm. Thanks for your help 👍
b
I find ComponentResources are better than exported functions, but both work great!
b
I'll def spend some more time digging into componentresources. One downside of using python for iac is that people come with their own best practices for applications, not IAC which may actually be a case of unlearning, then learning vs coming fresh to a new IAC language and learning from scratch.
I like what I see so far tho