Hi, I'm pretty new to typescript. I can make my wa...
# typescript
g
Hi, I'm pretty new to typescript. I can make my way around with help from google, but I've been solely using pulumi with yaml up till now. I recently created a pulumi program in typescript to dynamically create users and group memberships based on a list of groups and users
Copy code
groupname:
  - userA
  - userB
group2:
  - userA
  - userC
etc
This was not convenient to do in yaml as I'd have 30+ permutations in just a simple setup of 10 groups with 3 users each. So I made a dynamic loop to do it in typescript. However now I want to reference a particular user as a stack output in another program. E.g. I want to add 'userA' in an IAM policy on a particular resource. Given that I don't have static user resources, how would I reference a user as an output in another program?
l
You cannot export from inside a loop. Assuming you want to export properties from each user, and assuming you don't want to limit the number of users in your app code, then the only option left is to export an entire collection of whichever property you need.
g
Grok has suggested something like this. Does this approach make sense? Method 1:
Copy code
//inside my loop - I've redacted all the stuff above

        // Store the userId in the users object, keyed by username
        users[username] = user.apply(u => u.userId);
    });
});

// At the end of the program, outside the loop

// Export the users object
export const users = users;
In my other yaml program, I'd then reference the users like this:
Copy code
variables:
  varKeyAdmins: '[
      "${typescriptProject.users["james"]}",
      "${typescriptProject.users["bruce"]}",
    ]'
Method 2:
Copy code
//inside my loop - I've redacted all the stuff above

        // Store the userId in the users object, using a normalized key (e.g., "userJames")
        const userKey = `user${username.split("@")[0]}`; // e.g., "userJames" from "james@example.com"
        users[userKey] = user.apply(u => u.userId);
    });
});

// At the end of the program, outside the loop

// Export individual users dynamically
Object.keys(users).forEach(key => {
    (exports as any)[key] = users[key];
});
In my other program (yaml), I'd then reference the users like this:
Copy code
In my other yaml program, I'd then reference the users like this:
variables:
  varKeyAdmins: '[
      "${typescriptProject.userJames}",
      "${typescriptProject.userBruce}",
    ]'
On reflection Method 2 would be invalid given as you've said that exports can't be inside a loop
l
Yea, there's a lot of syntax errors in method 1, but that's the general idea.
Note that you almost never need code anything like this:
Copy code
users[username] = user.apply(u => u.userId);
In this case, you'd use
Copy code
userIds[index] = user.id
Most of the time,
apply()
can and should be avoided. It's needed in some tricky cases, but most of the time, you can dereference outputs just fine.
g
Thanks for the tip! It did seem needlessly complex
Actually, unfortunately that
userIds[index] = user.id
doesn't seem to work in this particular case. Possibly my AI generated code further up has done something unnecessary making this
apply
required. Anyway I got it sorted, thanks.