https://pulumi.com logo
Title
b

breezy-butcher-78604

09/15/2020, 3:45 AM
is it possible to get the result of a
getVpc()
call immediately?
i can see passing
{ async: false }
into the function might be what im looking for but the comments say this has been deprecated. is there new functionality that achieves this?
l

little-cartoon-10569

09/15/2020, 3:46 AM
You can
await
the promise.
const vpcResult = await ec2.getVpc({default: true});
is about the smallest workable code?
b

breezy-butcher-78604

09/15/2020, 3:49 AM
i can't do that as this is code running in
index.ts
and not inside an
async
function so it complains about top-level
await
. is there a way to do this without wrapping the whole template in an anonymous async function?
l

little-cartoon-10569

09/15/2020, 3:49 AM
Use
.then()
.
ec2.getVpc({ default: true}).then(result => {
  console.log(JSON.stringify(result));
}
Of course, that kind of is an anonymous async function... ¯\_(ツ)_/¯
b

breezy-butcher-78604

09/15/2020, 3:51 AM
will that limit me to using the result within that callback though?
let me paste some code of what i'm trying to do, one sec
l

little-cartoon-10569

09/15/2020, 3:51 AM
Yes. You need to await to get it out to the top level.
b

breezy-butcher-78604

09/15/2020, 3:58 AM
// Grab VPC ID
const vpcId = (await aws.ec2.getVpc({ filters: [ /* filters here */ ] })).id;

// Grab a security group within the VPC
const sgId = (await aws.ec2.getSecurityGroup({ vpcId, filters: [ /* filters here */ ] })).id;

// Grab an array of subnets
const subnetIds = (await aws.ec2.getSubnetIds({ vpcId, filters: [ /* filters here */ ] })).ids;

subnetIds.forEach(subnetId => {
    const instance = new aws.ec2.Instance('instance', { /* instance in subnet with above sg */ });
});
l

little-cartoon-10569

09/15/2020, 3:59 AM
All of that can move into async functions and it will fit the Pulumi model better.
b

breezy-butcher-78604

09/15/2020, 4:00 AM
so something like this?
(async () => {
    // Grab VPC ID
    const vpcId = (await aws.ec2.getVpc({ filters: [ /* filters here */ ] })).id;

    // Grab a security group within the VPC
    const sgId = (await aws.ec2.getSecurityGroup({ vpcId, filters: [ /* filters here */ ] })).id;

    // Grab an array of subnets
    const subnetIds = (await aws.ec2.getSubnetIds({ vpcId, filters: [ /* filters here */ ] })).ids;

    subnetIds.forEach(subnetId => {
        const instance = new aws.ec2.Instance('instance', { /* instance in subnet with above sg */ });
    });
})();
l

little-cartoon-10569

09/15/2020, 4:01 AM
Maybe you can load a VPC using
ec2.Vpc.get()
or
awsx.ec2.Vpc.initializeExistingVpcIdArgs()
? They provide some great Pulumi functions...
For your existing code, I would use something like
initiInstancesInSubnets(vpcFilters, sgFilters,  subnetFilters);

async function initiInstancesInSubnets(filters) {
const vpcId = await aws.ec2.getVpc({ filters: [ vpcFilters ] }).id;
const sgId = await aws.ec2.getSecurityGroup({ vpcId, filters: sgFilters }).id;
const subnetIds = (wait aws.ec2.getSubnetIds({ vpcId, filters: subnetFilters ] }).ids;
subnetIds.forEach(subnetId => {
    const instance = new aws.ec2.Instance('instance', { /* instance in subnet with above sg */ });
});
But even that is pretty "imperative" and not very (declarative + functional) ...
b

breezy-butcher-78604

09/15/2020, 4:06 AM
hmm i didn't know
aws.ec2.Vpc.get()
existed, that might do what i need
l

little-cartoon-10569

09/15/2020, 4:06 AM
I think the awsx version would be good for you since it wraps subnets as well as the VPC?
b

breezy-butcher-78604

09/15/2020, 4:07 AM
yeah i'll have a look at that too
hmm neither of these approaches will work for what i'm trying to do as they will require providing at least the IDs up front. this is possible, but I was hoping to be able to avoid it and have the template grab them based on some tag filters
so i think my options are to just supply the various IDs upfront or to wrap the relevant parts in an anonymous async function as above
i'll probably opt for the former
l

little-cartoon-10569

09/15/2020, 4:13 AM
When I do this sort of thing (essentially wrapping the AWS SDK in Pulumi), I've been fortunate enough to be able to put the needed IDs in the Pulumi config file, and remove the need for filters. I don't use Pulumi for "utility" code. Once your VPCs are managed by Pulumi, then filters and the like go out the window 🙂
b

breezy-butcher-78604

09/15/2020, 4:14 AM
yeah the only reason im doing this is to help with slowly migrating some legacy stuff over to pulumi, its a halfway step. eventually everything will be managed by pulumi and this problem will go away
providing the IDs in config isn't the end of the world and makes the template code a bit easier to follow anyway so i'll go with that
thanks for your help man, appreciate it 👍
👍 1