I'm new to Pulumi and the documentation makes me w...
# general
b
I'm new to Pulumi and the documentation makes me want to rip my hair out, what is the proper way to either generate multiple subnets using aws-python or to use the stack to automatically generate a certain number of subnets
m
Hi there. Is this the kind of thing you might do with a
for
loop? Like given some value like
3
obtained from somewhere, like stack configuration, generate 3 subnets?
A simple example (and apologies if this is too simple, hopefully I’m not misunderstanding) would be something like this, which attempts to read a number from the stack configuration and loops to declare some bucket resources:
Copy code
import pulumi
from pulumi_aws import s3

config = pulumi.Config()
buckets = config.get_int("buckets", 3)

for i in range(0, buckets):
    bucket = s3.Bucket(f"bucket-{i}")
(.. falling back to 3 if there isn’t a config setting, in this case.)
b
subnets aren't an s3 thing though this would be for building your vpc there should be some way within aws.ec2.Vpc to do this
m
subnets aren’t an s3 thing though
Yes, was just for illustration. Subnets would be defined similarly — lemme see.
Something like this? Again, hopefully answering your question.
Copy code
import pulumi
from pulumi_aws import ec2

config = pulumi.Config()
subnets = config.get_int("subnets", 3)

vpc = ec2.Vpc("vpc", cidr_block="10.0.0.0/16")

for i in range(0, subnets):
    subnet = ec2.Subnet(f"subnet-{i}",
        vpc_id=vpc.id,
        cidr_block=f"10.0.{i + 1}.0/24")
Should yield something like
Copy code
Updating (dev)

     Type                 Name                        Status              
 +   pulumi:pulumi:Stack  2021-01-25-python-loop-dev  created (0.25s)     
 +   ├─ aws:ec2:Vpc       vpc                         created (2s)        
 +   ├─ aws:ec2:Subnet    subnet-0                    created (1s)        
 +   ├─ aws:ec2:Subnet    subnet-1                    created (1s)        
 +   └─ aws:ec2:Subnet    subnet-2                    created (1s)        

Resources:
    + 5 created
b
interesting, I really didn't know you could do something like this, I'll need to get better at coding straight up python to really make use of the power of this engine apparently
m
Yeah, if you’re coming from something like TF, you might be used to a
count
property or something defined on the resource itself. Pulumi is different in that you use the language to define those kinds of constructs —
if
conditionals, loops, things like that.
b
I'm actually still pretty fresh to devops i'm a sysadmin right now and I'm doing this as a project with some people in the devops engineers slack but a lot of it is left on us to read the documentation and figure out with minimal guidance so we actually learn. Problem is the documentation i've found is all for crosswalk or out of date
m
This is good feedback, thanks. Documentation improvement is something we’re actively working on, but that isn’t super helpful when you need something now, I know. 🙂 Definitely feel free to ask here to compensate for that in the meantime, though. Lots of folks willing to help. And the #python channel would be good too if that’s the language you’re mainly working in.
b
yea I joined over there too I'm learning a lot of things all at once, maybe you know why it's telling me failed to load language plugin python all of a sudden when it's been working all night until I added that loop?
m
What’s your OS?
b
macOS ventura
m
Interesting. In
Pulumi.yaml
, do you have
Copy code
runtime:
  name: python
  options:
    virtualenv: venv
?
I started with
pulumi new aws-python
, if that helps.
b
yes but I added a line to it maybe I screwed it up let me check
ok I think i got passed that
s
I find the documentation quite good…! Comprehensive and clear… If you’re coming to Pulumi without a proficiency in another language… the YAML interface might be a great place to start for simple resources
b
might be I definitely have more experience in that
a lot of these sites just spoon feed you code and I didn't realize that I was just manipulating an api until @miniature-musician-31262 gave me that loop and it started throwing up this error that public_subnet.id didn't exist and i went oh crap i'm just creating variables I can name that anything i want so it is clicking slowly but surely but i'm coming to this with absolutely no experience coding. Until a year ago I was the average sysadmin and I had no reason to code now I want to start making real money and not see my job die out
m
Until a year ago I was the average sysadmin and I had no reason to code now I want to start making real money and not see my job die out
👆 This! Great statement and I try to encourage and help people to move learning development and development principals to solve the new Ops-challanges! If I can help you, let me know @broad-holiday-50009!
b
Hey @many-telephone-49025 thank you, I am certain you will be of much help in learning this if not other things as well. I'm doing a project with a couple people in the devops engineers slack right now that's using pulumi to deploy all our resources and i've never touched it before two days ago.
s
Awesome to see! @broad-holiday-50009 Pulumi is a powerful framework which builds on generations of infra-as-code technologies like Terraform and Ansible - Pulumi has finally nailed down many of the inherent problems and limitations in past technologies and i believe we are now in a golden age of infra development where the tools are no longer limiting factors in what we want to build or express. But of course this means it’s more to learn and might take longer to get proficient because we are using “fully featured” languages - the cool thing is that you can use pretty much whatever language you want!
g
@miniature-musician-31262 I do not think you can just use
if
statement for conditional resources in the same was as terraform
count
(hack). With terraform
count
you can make 1 resource dependent on the existence of another resource. This is not possible with pulumi and simple
if
because the resources are executed asynchronously.