anyone have a sane way (typescript) to get an actu...
# general
p
anyone have a sane way (typescript) to get an actual
string
(or a list of strings) out of
aws.ec2.getSubnetIds
? I just want the subnet IDs. everything I've tried results in various types that are variously incompatible. can't even get something I can simply
export
without blowing up the world. a simple
export const subnetIds =  aws.ec2.getSubnetIds({ vpcId: vpcId, });
results in
error TS1184: Modifiers cannot appear here.
w
Today, you will have to "go async" to do this. That unfortunately can be complex depending on your use case. You can get a
Promise<string[]>
like this:
Copy code
async function getSubnetIds(vpcId: string): Promise<string[]> {
    const res = await aws.ec2.getSubnetIds({ vpcId: "123" });
    return res.ids;
}
But then if you want to loop over these in your code, you'll need to write that loop inside an
async
function or a
then
callback on the promise. For stack exports - the code you shared above works for me as written - and produces:
Copy code
+ subnetIds: {
      + id   : "vpc-c93b06ae"
      + ids  : [
      +     [0]: "subnet-00412149"
      +     [1]: "subnet-fd19eaa6"
      +     [2]: "subnet-ba4009dd"
      +     [3]: "subnet-8a599ba1"
        ]
      + vpcId: "vpc-c93b06ae"
    }
p
let me create a more stripped down repro -
vpcId
is from config so there's a bit of complex that it picks up along the way
the export does work if I pass
"vpc-1234"
though.
ahhhh.
Copy code
if ( config.createVpc == false ) {
export const ccsubnetIds = pulumi.concat(aws.ec2.getSubnetIds({ vpcId: vpcId, }));
}
can't do the export from inside an if...... that's where the
error TS1184: Modifiers cannot appear here
comes in.
ok - I have something that works standalone.
Copy code
async function getSubnetIds(vpcId: string): Promise<string[]> {
    const res = await aws.ec2.getSubnetIds({ vpcId: vpcId });
    return res.ids;
};
export const subnetIds = getSubnetIds(config.vpcId!);
now when I try to consume
subnetIds
(in this case, in
awsx.ec2.Vpc.fromExistingIds....publicSubnetIds: subnetIds
) I end up with:
error TS2740: Type 'Promise<string[]>' is missing the following properties from type 'Input<string>[]': length, pop, push, concat, and 26 more.
again
w
Ahh - yes. I believe you can do:
Copy code
export let ccsubnetIds;
if ( config.createVpc == false ) {
    ccsubnetIds = pulumi.concat(aws.ec2.getSubnetIds({ vpcId: vpcId, }));
}
p
Copy code
async function getSubnetIds(vpcId: string): Promise<string[]> {
    const res = await aws.ec2.getSubnetIds({ vpcId: vpcId });
    return res.ids;
};
export const subnetIds = getSubnetIds(config.vpcId!);

let vpc2_manual = awsx.ec2.Vpc.fromExistingIds("vpc2_manual", {
        vpcId: vpcId,
        // this doesnt get populated automatically, and -fargateservice- only reads this from cluster.vpc.[pubic|private]....
        publicSubnetIds: subnetIds
        // privateSubnetIds: ...
});
is the consumer attempt and
publicSubnetIds: subnetIds
returns:
Copy code
error TS2740: Type 'Promise<string[]>' is missing the following properties from type 'Input<string>[]': length, pop, push, concat, and 26 more.
w
Ahh - I see. That is subtle - in that the
publicSubnetIds
property is apparently
Input<string>[]
instead of
Input<string[]>
. I believe you will truly have to "go async" here. That is - put all of your code that creates resources dependent on these subnets inside an asynchronous callback. Possibly by putting it all inside a function which you only call from inside a
then
or
async
function. Note that we're exploring being able to expose synchronous versions of these
get*
methods to significantly simplify this in the future - see https://github.com/pulumi/pulumi-terraform/issues/387.
p
that would be nice. anything that can be done to make this easier is positive! for now, I've bailed on this and I'm just having to feed these in manually through config after human extraction.
unless I go nuts and just use the sdk directly. 🙂