Dear Experts, is there a Python example, how to us...
# python
i
Dear Experts, is there a Python example, how to use *multi-region deploymen*t to deploy VPC, EC2 or something else? My goal would be to use a for loop to provision resources in different regions. I found the “*AWS Classic / Provider*”, but there is now example how to use in in Python.
p
what kind of problems do you have?
I haven’t used pulumi for AWS but I guess it should be similar to other providers. Basically, you do not use global aws config values in stack and explicitly pass region/zone arguments to the resources.
if you want, you may also create multiple providers manually (instead of using the default one) and point them to appropriate regions
i
Here is an example: #Create VPC vpc = aws.ec2.Vpc(f”vpc_sdwan_{region}“, cidr_block=“10.0.0.0/16”, tags={ “Name”: f”vpc_sdwan_{region}” ) How to specify here the region, in which needs to be deployed
p
I see,
Vpc
doesn’t have any
region
input
in that case, it most probably get the region values from provider
i
yes but how to do this? My goal is a Loop and to specify in the Loop all the resources like VPC, EC2 which should be done for the region
p
I’d go with the second option I suggested above (custom provider for each region). I don’t have access to any AWS subscription right now to test it, so you’ll have to test it yourself.
i
My problem is that I don’t know how I can specify this provider within a loop, which iterates thru multiple regions. Do you have an example?
p
sure, gimme me a second 😉
try to create dedicated providers (one for each region):
Copy code
aws_providers = {
  region: aws.Provider(f"aws-provider-{region}", region=region)
  for region in ("region-a", "region-b")
}
just fill the correct AWS region names instad of
region-a
and
region-b
when you create any pulumi resource, you can override the provider that’s gonna be used
i
ok great and how to use this provider for the specific VPC. Can you extend your example to one resource?
#Create VPC resource “aws_vpc” “vpc_sdwan_eu-central-1" { provider = aws.eu-central-1 cidr_block = “10.0.0.0/16” tags = { Name = “vpc_sdwan_eu-central-1", Owner = “cloudPOC” } }
this is from Terraform
p
sth like:
Copy code
for region in (...):
    provider = aws_providers.get(region)
    vpc = aws.ec2.Vpc(
        f"vpc_sdwan_{region}",
        cidr_block="10.0.0.0/16",
        tags={
            "Name": f"vpc_sdwan_{region}"
        },
=>      opts=pulumi.ResourceOptions(
=>          provider=provider,
        )
    )
it’s up to you how you’re gonna get these providers in code
i
GREAT! This was my missing point! Let me try this!
p
let me know if it works as expected 🙂
btw, you might wanna get rid of
aws:region
in your pulumi config (yaml file) so you can enforce using manually created providers (default one should complain about missing
region
argument)
i
opts=pulumi.ResourceOpts( provider=provider,
This part does not work, I think I miss here something. I work with pulumi_aws
I tried it to replace “pulumi” with “pulumi_aws” but it does not work.
This is an example with TypeScript but I can’t really translate it in Python. It is really something basic which I miss
p
well, the TS example does example what I suggested (that’s a good thing actually 😄)
anyway, let me see why it doesn’t work
I was just typing from my memory
hmm, how about
pulumi.ResourceOptions
? 😉
and this class is defined within
pulumi
(not
pulumi_aws
)
i
so I need to import pulumi as well in parallel with pulumi_aws, let me try that
p
yep
just take a look what type is expected in
opts
pulumi code has a lot of type hints so you should be able to easily deduce what’s expected in the given argument
Copy code
class Instance(pulumi.CustomResource):
    @overload
    def __init__(__self__,
                 resource_name: str,
                 opts: Optional[pulumi.ResourceOptions] = None,
                 ...
that’s from GCP module but every pulumi resources should support: • resource_name (required) • opts (here you can override things like provider, parent etc.)
i
Great! Now it works!
import pulumi was missing 😄
p
where are you developing your pulumi code?
i
visual code
p
personally, I’d recommend PyCharm (Community Edition is available for free, even for commercial use)
it should remind you about such trivial things as missing import automatically
I’m pretty sure that VSCode with some plugins can do a lot as well but I think python support is still better in PyCharm (not a surprise, as this is a dedicated IDE for python and it’s much heavier than VSCode unfortunately)
i
it was more that I miss the concept with multi region. Your example was perfect, I would nee more examples for Python. Terraform is better in that sense, because you have plenty of examples
aws_providers = { region: aws.Provider(f”aws-provider-{region}“, region=region) for region in regions }
This for example was important!
p
I’m glad I could help 😉
i
I have to thank!!!
g
Just a note, unless you are doing something really really big that you need dynamic regions, I'd advise you to not loop over region resources because 1 typo can result in a complete disaster. Imagine you destroy everything in all regions.
p
I’d say that you can achieve the same level of disaster with everything if you’re relying on any IaC solution 🙂, regardless of what you do (ofc, the more complex the code, the easier is to introduce some bugs). If you want to prevent deletion of resources, you should think about adding
protect
flag to critical resources: https://www.pulumi.com/docs/intro/concepts/resources/#protect