Query Fly for all running instances by app name from within the app

I have a page that displays all cache keys for a running instance (both in-memory cache as well as SQLite cache) and I’d like to be able to specify which instance’s caches I want to look at. To do this I need the instance hostname. Is it possible for my app to query fly for all running instance hostnames of itself?

  1. Retrieve alloc-ids of an app: Feature request: list of running ALLOC_IDs via DNS - #2 by charsleysa
  2. Address them: Fly Apps on machines: prerelease of fly deploy - #8 by kurt

Docs: Private Networking · Fly Docs

1 Like

Thanks, but I have no idea what to do with this information :sweat_smile:

Sorry, I should have linked to sample code. Here’s one in JavaScript: https://github.com/fly-apps/privatenet/blob/cfe81768d3/lookup.js#L61-L73

In your case, you’d want to:

  1. resolveTxt on vms.<app-name>.internal
  2. Split on , and retrieve alloc-ids
  3. resolve6 those alloc-ids: <alloc-id>.vm.<app-name>.internal

I’m typing the below on mobile, so it won’t be accurate:

// step 1
async function getAllocIds(appname) {
    let recs = null;
    try {
        recs = await dns.promises.resolveTxt(`vms.${appname}.internal`)
    } catch (err) {
        console.error(err);
    }

    if (rec == null) return [];

    // step 2; may be incorrect...
    const ids = recs.map(r => r.split(",")).map(vm => vm.split(" ")[0]);

    return ids;
};

// step 3
async function resolve(appname, allocids) {
    const ips = new Array(allocids.length);
    Arrays.fill(ips, "");
    for (const i in allocids)  {
        const id = allocids[i];
        try {
            const ans = await dns.promises.resolve6(`${id}.vm.${appname}.internal`));
            if (ans == null || ans.length <=0) continue;
            else ips[i] = ans[0];
        } catch (err) {
            console.log(err);
            continue;
        }
    }
    return ips;
};

1 Like

That helped a lot! Thank you.

I don’t need to resolve to IP addresses myself, so here’s my solution: kentcdodds.com/fly.server.ts at 96db2a79158809f09dda50aa15d0b34982958047 · kentcdodds/kentcdodds.com · GitHub

And here’s the first place I’m using this: kentcdodds.com/cache.admin.tsx at 96db2a79158809f09dda50aa15d0b34982958047 · kentcdodds/kentcdodds.com · GitHub

You can watch me build this here: Coding with Kent: Multi-region instance management - YouTube (see me use the feature I’m building this for here: Coding with Kent: Multi-region instance management - YouTube, basically, it allows me to inspect the cache for a specific instance).

Thanks again.

1 Like