https://pulumi.com logo
Title
e

echoing-address-44214

03/21/2023, 1:22 AM
Hi! I am writing a ComponentResource in Typescript. I need to set a name. For my name I have to do some lookups. So I got a Promise<string> instead of a string. Any recommendation to convert it to a string?
async prefix(): Promise<string> {
const parameter = await aws.ssm.getParameter(
{ name: "/prefix" }
).then(x=>x.value);
return parameter;
}
const name: string = convert prefix() to string
super("ruwen/RedisCluster", prefix() + id, {}, opts);
l

little-cartoon-10569

03/21/2023, 1:55 AM
Do you mean a name? Why does a component resource need an id?
e

echoing-address-44214

03/21/2023, 1:55 AM
argh yes
l

little-cartoon-10569

03/21/2023, 1:57 AM
Names shouldn't be figured out by a component. They must be available at execution time, rather than deploy time (no Outputs allowed). And they must be guaranteed to not change. Looking up a value like this breaks both of these rules.
The name of a resource is the first parameter passed into its constructor.
e

echoing-address-44214

03/21/2023, 1:58 AM
All of our resource names follow a pattern: <dev|qa|stg|prd>-<region>-<the actual name>
I want the user of my component to pass in the actual name
I add the prefix
This way I can ensure centrally that the naming schema is enforced
l

little-cartoon-10569

03/21/2023, 1:59 AM
Don't do that. Those other values are obvious from the project and stack names. No need to duplicate that data.
And if you have to, you can put those values in tags or similar.
Not in the resource names.
Having long resource names makes it hard to read, and can in a few cases break your cloud provider. Most resources use the names in the provider-specific id or name, and some of those names or ids are length-limited.
e

echoing-address-44214

03/21/2023, 2:01 AM
I am aware of those limits. We are doing it for years and makes it really easy if you have to do reservations etc
What happens if two stacks use the same name?
l

little-cartoon-10569

03/21/2023, 2:02 AM
No problems. That's what Pulumi puts suffixes there for.
e

echoing-address-44214

03/21/2023, 2:02 AM
So the suffix will be added even on my compoent resource?
l

little-cartoon-10569

03/21/2023, 2:02 AM
Anyway, to answer your question: convert that async lookup to a simple typescript function. Move the logic into reusable code, instead of a remove provider.
Yes
Wait, what is it that you are asking there?
Are you asking if the component name will have a random suffix?
Component names don't have a random suffix. Only cloud names.
Components are internal to a stack, therefore there can never be inter-stack clashes.
e

echoing-address-44214

03/21/2023, 2:04 AM
super("ruwen/RedisCluster", name, {}, opts);
What happens if I have in my state two stacks who use for the name the same value?
Let's say someone deploys their code in two regions in the same dev environment. For tname they use my-redis. Will it clash?
l

little-cartoon-10569

03/21/2023, 2:05 AM
The URNs have the stack name in them. Uniqueness is guaranteed.
No, it won't clash.
The URN will be
urn:pulumi:stackName::projectName::typeName::name
Since the stack name is in there, there is no clash.
e

echoing-address-44214

03/21/2023, 2:06 AM
Because the docs say
name – The unique name of the resource.
So unique within a stack?
ah ok
l

little-cartoon-10569

03/21/2023, 2:06 AM
Unique within a given project/stack/type triple.
e

echoing-address-44214

03/21/2023, 2:06 AM
easy 🙂
l

little-cartoon-10569

03/21/2023, 2:07 AM
You can have loads of resources with the same name, so long as they're all different types.
e

echoing-address-44214

03/21/2023, 2:07 AM
I thought it had to be unique within the state file
l

little-cartoon-10569

03/21/2023, 2:07 AM
The URN needs to be unique within the state file. The name only needs to be unique within the (type + stack + project).
e

echoing-address-44214

03/21/2023, 2:08 AM
Ah makes sense. That simplifies my life! Thank you!