Is there a way to programmatically get the account...
# typescript
g
Is there a way to programmatically get the account/organization name for a pulumi project? I want to construct a stack reference under the same account/org without hard-coding (or configuring) the org.
v
You can use the getCallerIdentity method and obtain the account ID from the outputs
g
As far as i understand this returns the AWS account. The account/organization name required to construct a pulumi StackReference is the pulumi account or org name, no?
v
Ah sorry you said in your message about getting account information. Do you mean the organization ID?
g
Whatever is necessary to construct a fully qualified stack reference. I believe the documentation states it's the organization id if the project is in an organization, or the account is otherwise
v
You just pass the stack name in to the stack reference as long as it's in the same account. I'm not aware of a way to do cross account stack references
l
AWS accounts aren't relevant here, Pulumi has
getProject()
and
getStack()
functions, but they return the current project and stack names. StackReference has a
name
property. There is no way to get the org, project or stack name from another stack. @gentle-zoo-32137 Why do you need to get the organization name? Since you know what project you're in, you can safely hard code the organization and project values. I think you shouldn't extract this logic into re-usable functions that might be called from different organizations or projects. There is too great a risk of getting the wrong stack reference when moving code around. A value like organization name can be made a const string in your index.ts and passed around: it doesn't make coupling worse, and there is no loss of re-usability.
g
Hard-coding the organization and project name in a collection of micro-projects that depend on each other makes it difficult to later clone or move this entire set of projects into a different organization. For example, right now I'm working in a personal non-org account, but at some point I believe I'll move these projects into an organization. Going over all the code and modifying the hard-coded org name seems brittle.
Note that I don't want to get the org name of a different project in a different org. All I am looking for is a stable way to refer to another project within the same organization. For example, if I could pass a relative stack name (instead of a fully qualified stack name) into StackReference that would solve my problem.
l
You can import it from a single file. It's just an import statement..
Copy code
import * as pulumiConstants from "@mycompany/PulumiConstants";
// Get a stack reference to the same stack in the _Base_ project.
const baseStackRef = new pulumi.StackReference(`${pulumiConstants.ORG_NAME}/Base/${pulumi.getStack()}`);
g
That works if I want to change the account to an organization or move organizations, but not if I want to clone projects to a new organization (while keeping the old one). Anyway it seems like this indeed needs to be resolved outside of pulumi. I'll make do with this, and submit a feature request to support relative stack references or something
l
Since v3.39.0, the NodeJS & Python SDKs have a
getOrganization()
method available: https://github.com/pulumi/pulumi/releases/tag/v3.39.0
g
Found that in the source code just now! That's awesome, seems like it isn't updated in the documentation yet