https://pulumi.com logo
Title
s

steep-lamp-20408

09/27/2022, 6:17 AM
Hey all, I’m using AWSSx package to create a AWS VPC as @little-cartoon-10569 kindly suggested in this conversation. I don’t understand the documentation though (https://www.pulumi.com/registry/packages/awsx/api-docs/ec2/vpc/): how I can get the ID of the VPC I create? For example:
from pulumi_awsx import ec2

vpc = ec2.Vpc(resource_name="my-vpc")
print(vpc.id)
... outputs
None
. Any idea?
b

boundless-tomato-68419

09/27/2022, 6:31 AM
Hi @steep-lamp-20408, at the bottom of the page you can find the "output" graph where the available outputs are listed for each of the resources. In your case, if you want to use them as a reference for another resource you have to use the following syntax:
name_of_your_resource.id
Also note that if you want to make a reference to another resource it have to be created already!
s

steep-lamp-20408

09/27/2022, 6:32 AM
Hi @boundless-tomato-68419. Yes, that’s what I did :
vpc.id
However it outputs
None
. Hence my question
vpc = ec2.Vpc(resource_name="my-vpc")
print(vpc.id)
b

boundless-tomato-68419

09/27/2022, 6:36 AM
Its known only after initialization of the resource. Did you create it already?
s

steep-lamp-20408

09/27/2022, 6:36 AM
Yes the creation is this line :
vpc = ec2.Vpc(resource_name="my-vpc")
b

boundless-tomato-68419

09/27/2022, 6:37 AM
But i mean you did pulumi Up and you created the resources right?
s

steep-lamp-20408

09/27/2022, 6:37 AM
well... yeah 🙂
b

boundless-tomato-68419

09/27/2022, 6:44 AM
I think that you have to actually export the specific outputs in order to be able to print them on the console. JS:
exports.bucketName = siteBucket.id;
Did you tried it?
s

steep-lamp-20408

09/27/2022, 6:46 AM
I’m just using the print to debug, but I need to use the id of the VPC in another resources. I don’t need it in the console, this is just an example for the debug.
vpc.id
seems to be null whether it’s printed or not.
For example, this
vpc = ec2.Vpc(resource_name="my-vpc")

public_subnet = aws.ec2.Subnet("my-subnet",
    vpc_id=vpc.id,
    cidr_block="10.0.1.0/24",
    tags={
        "Name": "Main",
    },
)
....would throw an error when creating the
public_subnet
resource, saying the
vpc_id
cannot be
null
.
b

boundless-tomato-68419

09/27/2022, 6:51 AM
vpc = ec2.Vpc(resource_name="my-vpc")
But why is it ec2.vpc?
s

steep-lamp-20408

09/27/2022, 6:53 AM
Because it’s the
ec2
from the
pulumi_awsx
package (
from pulumi_awsx import ec2
). Anyway the creation of the VPC is totally working, my point is about getting its
id
afterwards
l

little-cartoon-10569

09/27/2022, 7:20 AM
It should work. You can try using
vpc.vpc.id
if you like: the awsx VPC wraps the aws VPC. They should have the same ID (they do in TS/JS.. maybe not in Python?).
👍 1
s

steep-lamp-20408

09/27/2022, 7:29 AM
It seems
vpc_id
outputs a promise/output. Maybe I should use that.
God I love Pulumi but the doc is the worst 😅
@little-cartoon-10569 actually I don’t see how
pulumi_awsx.ec2.vpc
is better/simpler than AWS classic’s
pulumi_aws.ec2.Vpc
, so I’m switching to
pulumi_aws
. I get also a promise with
.id
from this. It should work.
l

little-cartoon-10569

09/27/2022, 7:47 AM
The power of awsx is in the extra resources it creates for free: some subnets, an IGW, one NAT per private subnet, and all the route tables and routes you need. I prefer aws myself, but if you don't know all the extra bits you need for general purpose networking, then awsx is great.
👍 1
It also works out CIDRs for you.
Re: the output: you must use the output. You cannot get at the string id during deployment. It's not available until deployment completes.
s

steep-lamp-20408

09/27/2022, 7:48 AM
I see! Thanks. Not very familiar with AWS devops so didn’t realize all this was necessary 😮
l

little-cartoon-10569

09/27/2022, 7:49 AM
Build the awsx VPC in a test project with the correct public, private and isolated subnets, then look at the graph of resources it produces. Then convert that to your own code, using aws. This is the best option, at least until aws-native is fully featured.
💯 1