https://pulumi.com logo
#general
Title
# general
p

proud-alarm-92546

06/05/2019, 9:16 PM
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

white-balloon-205

06/05/2019, 9:33 PM
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

proud-alarm-92546

06/05/2019, 9:40 PM
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

white-balloon-205

06/05/2019, 10:16 PM
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

proud-alarm-92546

06/05/2019, 10:58 PM
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

white-balloon-205

06/05/2019, 11:57 PM
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

proud-alarm-92546

06/06/2019, 1:15 AM
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. 🙂