https://pulumi.com logo
#general
Title
# general
b

billions-mechanic-26704

09/21/2021, 2:25 PM
Hi community, there is a prebuilt way to include prefix on resource names when creating them? Similar to terraform? Or do we need to create it programatically? In terraform I had something like:
Copy code
variable "org" {
  type = string
  validation {
    condition     = length(<http://var.org|var.org>) <= 3
    error_message = "The org variable cannot be larger than 3 characters."
  }
}

variable "tenant" {
  type = string
  validation {
    condition     = length(var.tenant) <= 4
    error_message = "The tenant variable cannot be larger than 4 characters."
  }
}

variable "environment" {
  type = string
  validation {
    condition     = length(var.environment) <= 4
    error_message = "The environment variable cannot be larger than 4 characters."
  }
}
And I use the above variables to name an azure resource group like:
Copy code
module "resource_group_name" {
  source   = "gsoft-inc/naming/azurerm//modules/general/resource_group"
  name     = "main"
  prefixes = [<http://var.org|var.org>, var.tenant, var.environment]
}
Is possible to do something similar in pulumi? I saw a similar issue reported here, but looks like this is more under programatically control?
p

prehistoric-activity-61023

09/21/2021, 4:23 PM
due to the fact pulumi uses programming language instead of some DSL (like terraform), you’re free to write your own classes/helpers etc.
you can for example write a helper function:
Copy code
def generate_resource_name(name: str, org: str, tenant: str, org: environment):
   return f"{org}-{tenant}-{environment}-{name}"
and use that while creating resources:
Copy code
SomeResource(
   generate_resource_name("main"),
   name=generate_resource_name("main"),
   ...
)
Notice that you usually have 2 separate names: • the first argument is the name within pulumi itself • the second one is the name in cloud provider resource (e.g. name of the GKE cluster)
AFAIK, it’s not possible to alter the current autonaming scheme in pulumi though
so if you want to have full control over the names (including autonaming random suffix), things might get a little bit tricky (you’ll need to override the
name
as I wrote above and it might cause some issues in case the resource must be recreated)
https://www.pulumi.com/docs/intro/concepts/resources/#autonaming
Overriding auto-naming makes your project susceptible to naming collisions. As a result, for resources that may need to be replaced, you should specify 
deleteBeforeReplace: true
 in the resource’s options. This option ensures that old resources are deleted before new ones are created, which will prevent those collisions.
b

billions-mechanic-26704

09/21/2021, 8:55 PM
@prehistoric-activity-61023 thanks for your reply and advice. According to this,
For cases that require specific names, you can override auto-naming by specifying a physical name. Most resources have a 
name
 property that you can use to name the resource yourself
If I understood well, I can override those auto-named resources which allow the
name
attribute on their API specification, but then when doing so is when naming collisions might be presented (?) In my case I am using the
StorageAccount
resource on python azure API, https://www.pulumi.com/docs/reference/pkg/azure-native/storage/storageaccount/#account_name_python it does not allow override the property name and the helper function I did was something like this:
Copy code
def generate_storage_account_name(name: str, number: int, org: str, app: str, env: str):
    return f"{name}{number}{org}{app}{env}"
And in the resource I am calling it in this way:
Copy code
# Create an Azure resource (Storage Account)
account = storage.StorageAccount(
    'main',
    resource_group_name=resource_group.name,
    account_name=generate_storage_account_name('sa', random.randint(1,100000), organization, application, environment),
    sku=storage.SkuArgs(
        name=storage.SkuName.STANDARD_LRS,
    ),
    kind=storage.Kind.STORAGE_V2)
It works and my storage account was named in that way
Copy code
> pulumi up
Previewing update (rhdhv/dev)

View Live: <https://app.pulumi.com/rhdhv/wmlab-infrastructure/dev/previews/549c2c34-853f-4fe0-b9f2-d5504525b073>

     Type                                     Name                      Plan
 +   pulumi:pulumi:Stack                      wmlab-infrastructure-dev  create
 +   ├─ azure-native:resources:ResourceGroup  resource_group            create
 +   └─ azure-native:storage:StorageAccount   main                      create

Resources:
    + 3 to create

Do you want to perform this update? details
+ pulumi:pulumi:Stack: (create)
    [urn=urn:pulumi:dev::wmlab-infrastructure::pulumi:pulumi:Stack::wmlab-infrastructure-dev]
    + azure-native:resources:ResourceGroup: (create)
        [urn=urn:pulumi:dev::wmlab-infrastructure::azure-native:resources:ResourceGroup::resource_group]
        [provider=urn:pulumi:dev::wmlab-infrastructure::pulumi:providers:azure-native::default_1_29_0::04da6b54-80e4-46f7-96ec-b56ff0331ba9]
        location         : "westeurope"
        resourceGroupName: "rhd-wmlab-dev"
    + azure-native:storage:StorageAccount: (create)
        [urn=urn:pulumi:dev::wmlab-infrastructure::azure-native:storage:StorageAccount::main]
        [provider=urn:pulumi:dev::wmlab-infrastructure::pulumi:providers:azure-native::default_1_29_0::04da6b54-80e4-46f7-96ec-b56ff0331ba9]
        accountName      : "sa99180rhdwmlabdev"
        kind             : "StorageV2"
        location         : "westeurope"
        resourceGroupName: output<string>
        sku              : {
            name: "Standard_LRS"
        }
I am right if I say that in cases when the
name
property is not available to be overrided, like this case, so collisions errors names won’t be presented?
Hi @prehistoric-activity-61023 sorry for disturb you, I just add a new resource (a github repo) and the storage account I created with the helper function will be recreated as you mentioned.
Copy code
pulumi up
Previewing update (rhdhv/dev)

View Live: <https://app.pulumi.com/myorg/wmlab-infrastructure/dev/previews/846183e7-9532-4d57-a60a-f11e34d2be07>

     Type                                    Name                      Plan        Info
     pulumi:pulumi:Stack                     wmlab-infrastructure-dev
 +   ├─ github:index:Repository              wmlab-infra               create
 +-  └─ azure-native:storage:StorageAccount  main                      replace     [diff: ~accountName]
I am afraid I will have to check this thoroughfully,