is it possible to get the result of a `getVpc()` c...
# typescript
b
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
You can
await
the promise.
const vpcResult = await ec2.getVpc({default: true});
is about the smallest workable code?
b
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
Use
.then()
.
Copy code
ec2.getVpc({ default: true}).then(result => {
  console.log(JSON.stringify(result));
}
Of course, that kind of is an anonymous async function... ¯\_(ツ)_/¯
b
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
Yes. You need to await to get it out to the top level.
b
Copy code
// 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
All of that can move into async functions and it will fit the Pulumi model better.
b
so something like this?
Copy code
(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
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
Copy code
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
hmm i didn't know
aws.ec2.Vpc.get()
existed, that might do what i need
l
I think the awsx version would be good for you since it wraps subnets as well as the VPC?
b
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
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
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