Hi! I am writing a ComponentResource in Typescrip...
# general
e
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
Do you mean a name? Why does a component resource need an id?
e
argh yes
l
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
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
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
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
No problems. That's what Pulumi puts suffixes there for.
e
So the suffix will be added even on my compoent resource?
l
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
Copy code
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
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
Because the docs say
name – The unique name of the resource.
So unique within a stack?
ah ok
l
Unique within a given project/stack/type triple.
e
easy 🙂
l
You can have loads of resources with the same name, so long as they're all different types.
e
I thought it had to be unique within the state file
l
The URN needs to be unique within the state file. The name only needs to be unique within the (type + stack + project).
e
Ah makes sense. That simplifies my life! Thank you!