Hi! I am writing a ComponentResource in Typescrip...
# general
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" }
return parameter;
const name: string = convert prefix() to string
super("ruwen/RedisCluster", prefix() + id, {}, opts);
Do you mean a name? Why does a component resource need an id?
argh yes
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.
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
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.
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?
No problems. That's what Pulumi puts suffixes there for.
So the suffix will be added even on my compoent resource?
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.
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.
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?
The URNs have the stack name in them. Uniqueness is guaranteed.
No, it won't clash.
The URN will be
Since the stack name is in there, there is no clash.
Because the docs say
name – The unique name of the resource.
So unique within a stack?
ah ok
Unique within a given project/stack/type triple.
easy 🙂
You can have loads of resources with the same name, so long as they're all different types.
I thought it had to be unique within the state file
The URN needs to be unique within the state file. The name only needs to be unique within the (type + stack + project).
Ah makes sense. That simplifies my life! Thank you!