Azure / azure-dev

A developer CLI that reduces the time it takes for you to get started on Azure. The Azure Developer CLI (azd) provides a set of developer-friendly commands that map to key stages in your workflow - code, build, deploy, monitor, repeat.
https://aka.ms/azd
MIT License
393 stars 187 forks source link

Azd-Aspire-Cdk: Naming strategy #3622

Open vhvb1989 opened 5 months ago

vhvb1989 commented 5 months ago

Ideally, a decision needs to be made for Aspire GA for how resource names are generated.

From @tg-msft:

I want to have a “name strategy” that by default keeps them stable but also allows you to randomize aggressively if desired for niche scenarios. Stable requires tracking the full namespace though to use kv2 occasionally instead of kv277327222773 always. We also need to be smarter about the various lengths (i.e. using a prefix with the first 24 identical characters for two Storage accounts should throw).

azd

For naming resources, azd is following the next strategy:

Aspire + Cdk

Bicep modules created by Aspire utilize the Cdk, which writes the next naming pattern:

name: toLower(take(concat('${resourceName}', uniqueString(resourceGroup().id)), 24))

while the strategy secures the names with no dashes, it is still vulnerable to long resource names.

@davidfowl @mitchdenny @JoshLove-msft

JoshLove-msft commented 5 months ago

What happens if multiple resources of the same type are added to the same resource group - would there be a collision?

vhvb1989 commented 5 months ago

What happens if multiple resources of the same type are added to the same resource group - would there be a collision?

There are scenarios, when thinking about adding resources:

mitchdenny commented 5 months ago

I think as a general rule child resources (databases, keyvault secrets, eventhubs, queues/topics etc) just use whatever name the developer provides. We are really talking about top level DNS defining names where the potential for conflicts in Azure exists.

If the name is a hash(resourceGroup().id + aspireResourceName) then there can never be a collision. All you have to manage at that point is the naming rules for a particular resource:

  1. Typically can't have a leading number.
  2. Can't exceed some defined length.
  3. Can't use - sometimes (e.g. storage)

My strategy would be to take the most restrictive subset of names and come up with a general formula for computing the name along those lines. Historically I've just done hash(resourceGroup().id + name) and appended something short to not violate rule #1 above - then truncated the valute to meet length constraints.