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

important-magician-41327

11/22/2021, 11:22 AM
I really have a basic question: In the documentation for a resource, I can see this
Copy code
import pulumi
import pulumi_aws as aws

main = aws.ec2.Subnet("main",
    vpc_id=aws_vpc["main"]["id"],
    cidr_block="10.0.1.0/24",
    tags={
        "Name": "Main",
    })
Unfortunately specify the name of the resource does not work in my case, what Do I miss?
Copy code
#Create VPC
vpc = aws.ec2.Vpc("vpc_1", 
                    cidr_block="10.0.1.0/24", 
                    )       

vpc = aws.ec2.Vpc("vpc_2", 
                    cidr_block="10.0.2.0/24", 
                    )       

    #create WANem subnet
subnet_vpc1 = aws.ec2.Subnet("vpc_1",
    vpc_id=vpc["vpc_1"].id,
    cidr_block="10.0.1.0/24",
    availability_zone=available.names[0],
    )

    #create WANem subnet
subnet_vpc2 = aws.ec2.Subnet("vpc_2",
    vpc_id=vpc["vpc_2"].id,
    cidr_block="10.0.2.0/24",
    availability_zone=available.names[0],
    )
My goal is to reference the VPC by the name, not by object. How can I do this?
p

prehistoric-activity-61023

11/22/2021, 12:10 PM
let me try to help
btw, you’re redeclaring
vpc
variable (and probably that’s not indented by you):
Copy code
vpc = aws.ec2.Vpc("vpc_1", 
                    cidr_block="10.0.1.0/24", 
                    )       

vpc = aws.ec2.Vpc("vpc_2", 
                    cidr_block="10.0.2.0/24", 
                    )
second part, I cannot understand the part
vpc["vpc_1"]
,
vpc
is not a
dict
Copy code
#Create VPC
vpc1 = aws.ec2.Vpc("vpc_1", 
                    cidr_block="10.0.1.0/24", 
                    )       

vpc2 = aws.ec2.Vpc("vpc_2", 
                    cidr_block="10.0.2.0/24", 
                    )       

    #create WANem subnet
subnet_vpc1 = aws.ec2.Subnet("vpc_1",
    vpc_id=vpc1.id,
    cidr_block="10.0.1.0/24",
    availability_zone=available.names[0],
    )

    #create WANem subnet
subnet_vpc2 = aws.ec2.Subnet("vpc_2",
    vpc_id=vpc2.id,
    cidr_block="10.0.2.0/24",
    availability_zone=available.names[0],
    )
the easiest fix to your code should look like this
if you really want to access VPCs by name for some reason, create a
dict
for them:
Copy code
vpc = {
  "vpc_1": aws.ec2.Vpc("vpc_1", cidr_block="10.0.1.0/24"),
  "vpc_1": aws.ec2.Vpc("vpc_1", cidr_block="10.0.2.0/24"),       
}
then you’ll be able to access them with
vpc["vpc_1"]
I have a feeling you have strong terraform background and little experience with python (or programming languages). If that’s true, you have to keep in mind that pulumi uses full-fledged programming language rather than some custom DSL. Passing arguments can be done via declaring and injecting variables. If you need to iterate over something or more dynamically access some previously resources, you have to explicitly create a data structure for them (either list or dict). BTW, can you share where you got this first snippet from? Nvm, I’ve found it. It’s actually from official resource docs as you said. Basically, that example is incomplete and I got it how it could confuse you.
i

important-magician-41327

11/22/2021, 12:47 PM
Thanks Jakub for helping me again. Yes the documentation is confusing. Let me give you the example what I want to do: I want to create VPC’s in every AWS Region. Later in the code, I have to access the the specific VPC from the different regions in the code: This would be my solution, but it does not work. Please ignor that I used 2x loops, it is only to show you want I want to do:
Copy code
for region in aws_regions:
    vpc = aws.ec2.Vpc(f"vpc_{region}", 
                        cidr_block="10.0.1.0/24", 
            )        


for region in aws_regions:
    subnet_vpc = aws.ec2.Subnet(f"vpc_{region}",
        vpc_id=f"vpc_{region}".id,
        cidr_block="10.0.1.0/24",
        availability_zone=available.names[0],
        )
p

prehistoric-activity-61023

11/22/2021, 12:59 PM
I’d naturally do that like this:
Copy code
for region in aws_regions:
    vpc = aws.ec2.Vpc(f"vpc_{region}", 
                        cidr_block="10.0.1.0/24", 
            )
    subnet_vpc = aws.ec2.Subnet(f"vpc_{region}",
        vpc_id=vpc.id,
        cidr_block="10.0.1.0/24",
        availability_zone=available.names[0],
        )
so basically, join these 2 operations together in one loop iteration so you can reference the local variable
vpc
created just before created
subnet_vpc
i

important-magician-41327

11/22/2021, 1:01 PM
yes this is what I already do, but now I have a situation, that I need to access outside the loop, therefore I have provided the example, which does not make sense, just to illustrate the problem.
More specific if you want to create a TGW peering, you need to access the peering TGW in an other region. Therefore you need to loop thru every region and declare the TGW. In a different Loop you need to access the TGW in the peering_region, and than you need to specify the correct variable. But I think I need to create a Dictionary of TGW’s in every region, to access ist, like you have mentioned above?
p

prehistoric-activity-61023

11/22/2021, 1:10 PM
(sorry, I got a call)
try to do sth like this:
Copy code
vpcs = {}

for region in aws_regions:
    vpc = aws.ec2.Vpc(f"vpc_{region}", 
                        cidr_block="10.0.1.0/24", 
            )        
    vpc[region] = vpc
Copy code
from ... import vpcs

for region in aws_regions:
   vpc = vpcs[region] 
   subnet_vpc = aws.ec2.Subnet(f"vpc_{region}",
        vpc_id=vpc.id,
        cidr_block="10.0.1.0/24",
        availability_zone=available.names[0],
        )
👀 1
1
or if you just need to iterate over all vpc networks, you can declare them as a list/set
i

important-magician-41327

11/22/2021, 1:13 PM
Dictionary is the right approach! Thanks for the tipp, I still think to much in Terraform 🙂