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

important-magician-41327

11/18/2021, 9:50 AM
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

prehistoric-activity-61023

11/18/2021, 10:16 AM
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

important-magician-41327

11/18/2021, 10:20 AM
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

prehistoric-activity-61023

11/18/2021, 10:21 AM
I see,
Vpc
doesn’t have any
region
input
in that case, it most probably get the region values from provider
i

important-magician-41327

11/18/2021, 10:22 AM
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

prehistoric-activity-61023

11/18/2021, 10:22 AM
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

important-magician-41327

11/18/2021, 10:24 AM
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

prehistoric-activity-61023

11/18/2021, 10:24 AM
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

important-magician-41327

11/18/2021, 10:26 AM
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

prehistoric-activity-61023

11/18/2021, 10:27 AM
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

important-magician-41327

11/18/2021, 10:28 AM
GREAT! This was my missing point! Let me try this!
p

prehistoric-activity-61023

11/18/2021, 10:32 AM
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

important-magician-41327

11/18/2021, 10:38 AM
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

prehistoric-activity-61023

11/18/2021, 10:45 AM
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

important-magician-41327

11/18/2021, 10:47 AM
so I need to import pulumi as well in parallel with pulumi_aws, let me try that
p

prehistoric-activity-61023

11/18/2021, 10:47 AM
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

important-magician-41327

11/18/2021, 10:53 AM
Great! Now it works!
import pulumi was missing 😄
p

prehistoric-activity-61023

11/18/2021, 10:55 AM
where are you developing your pulumi code?
i

important-magician-41327

11/18/2021, 10:55 AM
visual code
p

prehistoric-activity-61023

11/18/2021, 10:55 AM
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

important-magician-41327

11/18/2021, 10:57 AM
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

prehistoric-activity-61023

11/18/2021, 10:58 AM
I’m glad I could help 😉
i

important-magician-41327

11/18/2021, 10:58 AM
I have to thank!!!
g

great-sunset-355

12/14/2021, 9:58 AM
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

prehistoric-activity-61023

12/14/2021, 10:03 AM
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