Skip to content

AddAsExistingResource does not take ExistingAzureResourceAnnotation into account #10992

@davidfowl

Description

@davidfowl

Referencing #10973 and #10908: When using AddAsExistingResource, the method does not take the ExistingAzureResourceAnnotation into account. This causes issues when referencing existing azure resources, such as Log Analytics Workspaces or Key Vaults, that are located in different resource groups than the deployment target. The provisioning logic ends up using the wrong resource group or scope, leading to failed deployments or runtime errors.

AddAsExistingResource should respect the ExistingAzureResourceAnnotation and ensure that any referenced resources use the correct resource group and scope. The new helper and pattern introduced in #10991 should be applied to all AddAsExistingResource implementations.

See issues #10973 and #10908 for repro steps.


Before (broken):

public override ProvisionableResource AddAsExistingResource(AzureResourceInfrastructure infra)
{
    var bicepIdentifier = this.GetBicepIdentifier();
    var resources = infra.GetProvisionableResources();

    // Check if a KeyVaultService with the same identifier already exists
    var existingStore = resources.OfType<KeyVaultService>().SingleOrDefault(store => store.BicepIdentifier == bicepIdentifier);

    if (existingStore is not null)
    {
        return existingStore;
    }

    // Create and add new resource if it doesn't exist
    var store = KeyVaultService.FromExisting(bicepIdentifier);
    store.Name = NameOutputReference.AsProvisioningParameter(infra);
    infra.Add(store);
    return store;
}

After (fixed):

public override ProvisionableResource AddAsExistingResource(AzureResourceInfrastructure infra)
{
    var bicepIdentifier = this.GetBicepIdentifier();
    var resources = infra.GetProvisionableResources();

    // Check if a KeyVaultService with the same identifier already exists
    var existingStore = resources.OfType<KeyVaultService>().SingleOrDefault(store => store.BicepIdentifier == bicepIdentifier);

    if (existingStore is not null)
    {
        return existingStore;
    }

    // Create and add new resource if it doesn't exist
    var store = KeyVaultService.FromExisting(bicepIdentifier);

    if (!TryApplyExistingResourceNameAndScope(
        this,
        infra,
        store))
    {
        store.Name = NameOutputReference.AsProvisioningParameter(infra);
    }

    infra.Add(store);
    return store;
}

This new pattern needs to be applied to all AddAsExistingResource implementations.

Metadata

Metadata

Assignees

Labels

area-integrationsIssues pertaining to Aspire Integrations packagesazureIssues associated specifically with scenarios tied to using Azure

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions