Skip to content

Honor AsExisting() on AddAzureContainerAppEnvironment + add WithAcrPullIdentity - #17365

Merged
David Fowler (davidfowl) merged 5 commits into
mainfrom
eerhardt/issue-12977-azure-container-app-environment-with-ase-9cc2f3
May 22, 2026
Merged

David Fowler (davidfowl) merged 5 commits into
mainfrom
eerhardt/issue-12977-azure-container-app-environment-with-ase-9cc2f3

Conversation

@eerhardt

@eerhardt Eric Erhardt (eerhardt) commented May 21, 2026 •

Copy link
Copy Markdown
Member

Description

AddAzureContainerAppEnvironment(...).AsExisting(...) already marked the resource as existing for the model, but the publish-time configureInfrastructure callback still produced a brand-new managed environment, Log Analytics workspace, Aspire Dashboard component, storage account, and user-assigned identity. The generated Bicep tried to create the environment the user said already existed, which is the bug reported in #12977.

This PR makes the callback short-circuit when the environment is marked as existing and emit a thin module that only references the existing resources and produces the outputs the rest of the publish pipeline consumes (AZURE_CONTAINER_APPS_ENVIRONMENT_NAME / _ID / _DEFAULT_DOMAIN and AZURE_CONTAINER_REGISTRY_NAME / _ENDPOINT / _MANAGED_IDENTITY_ID). Volumes and WithDelegatedSubnet are explicitly rejected with a clear error when combined with AsExisting, since both require creating new infrastructure.

A common deployment scenario for #12977 also wants to BYO the managed identity that holds AcrPull on the container registry, so the deployment principal does not need permission to create new identities or role assignments. This PR adds a small follow-on API for that:

var mi  = builder.AddAzureUserAssignedIdentity("shared-mi").AsExisting(...);
var acr = builder.AddAzureContainerRegistry("acr").AsExisting(...);

builder.AddAzureContainerAppEnvironment("env")
    .AsExisting(envName, sharedRg)
    .WithAzureContainerRegistry(acr)
    .WithAcrPullIdentity(mi);

When WithAcrPullIdentity is set, Aspire skips creating its own env_mi identity and the AcrPull role assignment, and wires the user's identity id through a Bicep parameter into AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID. The caller is responsible for ensuring the supplied identity already has AcrPull on the registry. This works in both the greenfield and AsExisting paths.

Refactor note: the six shared registry + env outputs are now produced from a single AddSharedContainerAppEnvironmentOutputs helper so the new-vs-existing branches stay in lockstep.

Associated docs PR: microsoft/aspire.dev#1045

Fixes #12977
Fixes #9094

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No

Copilot AI review requested due to automatic review settings May 21, 2026 21:03
@github-actions

github-actions Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 17365

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 17365"

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes publish-time Azure Container Apps infrastructure generation so that AddAzureContainerAppEnvironment(...).AsExisting(...) (and PublishAsExisting) no longer emits Bicep that creates a new managed environment and related child resources. It also adds a new API to let callers supply an existing user-assigned identity for ACR image pulls (WithAcrPullIdentity), avoiding identity/role-assignment creation during deployment.

Changes:

  • Short-circuit environment infrastructure generation when the Container Apps environment is marked as existing, emitting a thin “existing references + required outputs” module and rejecting incompatible features (delegated subnet, volumes).
  • Add WithAcrPullIdentity(...) to allow BYO user-assigned identity for AcrPull, skipping creation of env_mi and its role assignment.
  • Add/refresh snapshot-based tests validating both greenfield and AsExisting publish output for these scenarios.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppEnvironmentExtensionsTests.WithAcrPullIdentity_PublishGeneratesEnvWithSuppliedIdentity.verified.json Snapshot of manifest params for greenfield env + supplied AcrPull identity.
tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppEnvironmentExtensionsTests.WithAcrPullIdentity_PublishGeneratesEnvWithSuppliedIdentity.verified.bicep Snapshot of Bicep ensuring no env-managed identity / AcrPull role assignment is created when identity is supplied.
tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppEnvironmentExtensionsTests.WithAcrPullIdentity_AsExisting_PublishGeneratesThinModuleWithSuppliedIdentity.verified.json Snapshot of manifest params for existing env + existing ACR + supplied identity.
tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppEnvironmentExtensionsTests.WithAcrPullIdentity_AsExisting_PublishGeneratesThinModuleWithSuppliedIdentity.verified.bicep Snapshot of thin Bicep module referencing existing env/ACR and outputting required values.
tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppEnvironmentExtensionsTests.AsExisting_WithExplicitContainerRegistry_PublishGeneratesThinModule.verified.json Snapshot of manifest params for existing env + explicitly existing ACR.
tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppEnvironmentExtensionsTests.AsExisting_WithExplicitContainerRegistry_PublishGeneratesThinModule.verified.bicep Snapshot validating thin module behavior while still creating identity/role assignment when no identity is supplied.
tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppEnvironmentExtensionsTests.AsExisting_PublishGeneratesThinModuleReferencingExistingEnvironment.verified.json Snapshot of manifest params for existing env with default registry wiring.
tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppEnvironmentExtensionsTests.AsExisting_PublishGeneratesThinModuleReferencingExistingEnvironment.verified.bicep Snapshot validating existing env reference (no new env/LAW/dashboard) and required outputs.
tests/Aspire.Hosting.Azure.Tests/AzureContainerAppEnvironmentExtensionsTests.cs Adds regression and scenario tests (including delegated-subnet rejection) plus snapshot verification.
src/Aspire.Hosting.Azure.AppContainers/AzureContainerAppExtensions.cs Core behavior change: honor existing env at publish-time, add AcrPull identity override path, and unify shared outputs helper.
src/Aspire.Hosting.Azure.AppContainers/AzureContainerAppEnvironmentAcrPullIdentityAnnotation.cs New internal annotation used to flow the supplied identity through infrastructure generation.

Eric Erhardt (eerhardt) and others added 2 commits May 21, 2026 17:19
When AddAzureContainerAppEnvironment(...).AsExisting(...) was used, the
publish-time infrastructure callback still produced a brand-new managed
environment, Log Analytics workspace, Aspire Dashboard component, and
managed identity. Now it short-circuits to a thin Bicep module that
references the existing managed environment via 'existing' and emits the
required outputs.

Also adds WithAcrPullIdentity to let the caller supply a pre-provisioned
user-assigned identity for the AcrPull role on the container registry,
so deployments into shared / locked-down subscriptions do not need to
create identities or role assignments.

Fixes #12977

Co-authored-by: Copilot <[email protected]>
@eerhardt
Eric Erhardt (eerhardt) force-pushed the eerhardt/issue-12977-azure-container-app-environment-with-ase-9cc2f3 branch from 0c6d923 to 1677a9a Compare May 21, 2026 22:21

@mitchdenny Mitch Denny (mitchdenny) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two findings beyond what's already on the PR. Both are doc/UX gaps, not bugs.

Eric Erhardt (eerhardt) and others added 3 commits May 21, 2026 17:52
The existing managed environment already owns its Log Analytics workspace, so a user-supplied workspace would be silently discarded. Throw a clear InvalidOperationException at publish time instead, matching the existing guardrails for WithDelegatedSubnet and volume mounts.

Co-authored-by: Copilot <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

❓ CLI E2E Tests unknown — 95 passed, 0 failed, 5 unknown (commit 1a95362)

View all recordings
Status Test Recording
✅ AddPackageInteractiveWhileAppHostRunningDetached ✅ ▶️ View recording
✅ AddPackageWhileAppHostRunningDetached ✅ ▶️ View recording
✅ AgentCommands_AllHelpOutputs_AreCorrect ✅ ▶️ View recording
✅ AgentInitCommand_DefaultSelection_InstallsDefaultSkills ✅ ▶️ View recording
✅ AgentInitCommand_MigratesDeprecatedConfig ✅ ▶️ View recording
❔ AgentMcpListStructuredLogsFromStarterAppCore ❔ ▶️ View recording
✅ AllPublishMethodsBuildDockerImages ✅ ▶️ View recording
✅ AspireAddPackageVersionToDirectoryPackagesProps ✅ ▶️ View recording
✅ AspireInitSingleFileAppHostRunsViaDotnetRunAppHost ✅ ▶️ View recording
✅ AspireInitWithExistingAppHostDirRecreatesMissingNuGetConfigAndPreservesFiles ✅ ▶️ View recording
❔ AspireInitWithSolutionFileGeneratesAppHostThatBuildsAgainstChannelHive ❔ ▶️ View recording
✅ AspireUpdateRemovesAppHostPackageVersionFromDirectoryPackagesProps ✅ ▶️ View recording
✅ AspireUpdateRemovesOrphanAppHostPackageVersionWhenSdkAlreadyCurrent ✅ ▶️ View recording
✅ Banner_DisplayedOnFirstRun ✅ ▶️ View recording
✅ Banner_DisplayedWithExplicitFlag ✅ ▶️ View recording
✅ Banner_NotDisplayedWithNoLogoFlag ✅ ▶️ View recording
✅ CertificatesClean_RemovesCertificates ✅ ▶️ View recording
✅ CertificatesTrust_WithNoCert_CreatesAndTrustsCertificate ✅ ▶️ View recording
✅ CertificatesTrust_WithUntrustedCert_TrustsCertificate ✅ ▶️ View recording
✅ ConfigSetGet_CreatesNestedJsonFormat ✅ ▶️ View recording
✅ CreateAndRunAspireStarterProject ✅ ▶️ View recording
✅ CreateAndRunAspireStarterProjectWithBundle ✅ ▶️ View recording
✅ CreateAndRunEmptyAppHostProject ✅ ▶️ View recording
✅ CreateAndRunJavaEmptyAppHostProject ✅ ▶️ View recording
✅ CreateAndRunJsReactProject ✅ ▶️ View recording
✅ CreateAndRunPythonReactProject ✅ ▶️ View recording
✅ CreateAndRunTypeScriptEmptyAppHostProject ✅ ▶️ View recording
✅ CreateAndRunTypeScriptStarterProject ✅ ▶️ View recording
✅ CreateJavaAppHostWithViteApp ✅ ▶️ View recording
✅ CreateTypeScriptAppHostWithViteApp_UsesConfiguredToolchain ✅ ▶️ View recording
❔ DashboardRunWithAgentMcpCore ❔ ▶️ View recording
❔ DashboardRunWithOtelTracesReturnsNoTracesCore ❔ ▶️ View recording
✅ DeployK8sBasicApiService ✅ ▶️ View recording
✅ DeployK8sWithExternalHelmChart ✅ ▶️ View recording
✅ DeployK8sWithGarnet ✅ ▶️ View recording
✅ DeployK8sWithMongoDB ✅ ▶️ View recording
✅ DeployK8sWithMySql ✅ ▶️ View recording
✅ DeployK8sWithPostgres ✅ ▶️ View recording
✅ DeployK8sWithRabbitMQ ✅ ▶️ View recording
✅ DeployK8sWithRedis ✅ ▶️ View recording
✅ DeployK8sWithSqlServer ✅ ▶️ View recording
✅ DeployK8sWithValkey ✅ ▶️ View recording
✅ DeployTypeScriptAppToKubernetes ✅ ▶️ View recording
✅ DescribeCommandResolvesReplicaNames ✅ ▶️ View recording
✅ DescribeCommandShowsRunningResources ✅ ▶️ View recording
✅ DetachFormatJsonProducesValidJson ✅ ▶️ View recording
✅ DetachFormatJsonProducesValidJsonWhenRestartingExistingInstance ✅ ▶️ View recording
✅ DoListStepsShowsPipelineSteps ✅ ▶️ View recording
✅ DocsCommand_RendersInteractiveMarkdownFromLocalSource ✅ ▶️ View recording
✅ DoctorCommand_DetectsDeprecatedAgentConfig ✅ ▶️ View recording
✅ DoctorCommand_TypeScriptAppHostReportsMissingConfiguredToolchain ✅ ▶️ View recording
✅ DoctorCommand_WithSslCertDir_ShowsTrusted ✅ ▶️ View recording
✅ DoctorCommand_WithoutSslCertDir_ShowsPartiallyTrusted ✅ ▶️ View recording
✅ GeneratedAspireDevScript_StartsWatchMode_WithConfiguredToolchain ✅ ▶️ View recording
✅ GlobalMigration_HandlesCommentsAndTrailingCommas ✅ ▶️ View recording
✅ GlobalMigration_HandlesMalformedLegacyJson ✅ ▶️ View recording
✅ GlobalMigration_PreservesAllValueTypes ✅ ▶️ View recording
✅ GlobalMigration_SkipsWhenNewConfigExists ✅ ▶️ View recording
✅ GlobalSettings_MigratedFromLegacyFormat ✅ ▶️ View recording
✅ InitTypeScriptAppHost_AugmentsExistingViteRepoAtRoot ✅ ▶️ View recording
✅ InteractiveCSharpInitCreatesExpectedFiles ✅ ▶️ View recording
✅ InvalidAppHostPathWithComments_IsHealedOnRun ✅ ▶️ View recording
✅ JavaScriptHostingApisRunFromTypeScriptAppHost ✅ ▶️ View recording
✅ LatestCliCanStartStableChannelAppHost ✅ ▶️ View recording
✅ LatestCliCanStartStableChannelTypeScriptAppHost ✅ ▶️ View recording
✅ LegacySettingsMigration_AdjustsRelativeAppHostPath ✅ ▶️ View recording
✅ LogLevelTrace_ProducesTraceEntriesInCliLogFile ✅ ▶️ View recording
✅ LogsCommandShowsResourceLogs ✅ ▶️ View recording
✅ OtelLogsReturnsStructuredLogsFromStarterApp ✅ ▶️ View recording
✅ OtelLogsReturnsStructuredLogsFromStarterAppIsolated ✅ ▶️ View recording
✅ PsCommandListsRunningAppHost ✅ ▶️ View recording
✅ PsFormatJsonOutputsOnlyJsonToStdout ✅ ▶️ View recording
✅ PublishJavaScriptPatternsGeneratesExpectedDockerComposeArtifacts ✅ ▶️ View recording
✅ PublishWithConfigureEnvFileUpdatesEnvOutput ✅ ▶️ View recording
✅ PublishWithDockerComposeServiceCallbackSucceeds ✅ ▶️ View recording
✅ PublishWithoutOutputPathUsesAppHostDirectoryDefault ✅ ▶️ View recording
✅ ResourceCommand_FailedExecution_DisplaysAppHostLogPathAndLogContainsEntries ✅ ▶️ View recording
✅ ResourceCommand_FailsWhenInteractionServiceIsRequired ✅ ▶️ View recording
✅ ResourceCommand_SetAndDeleteParameterUpdatesDescribeOutput ✅ ▶️ View recording
✅ RestoreGeneratesSdkFiles ✅ ▶️ View recording
✅ RestoreGeneratesSdkFiles_WithConfiguredToolchain ✅ ▶️ View recording
✅ RestoreRefreshesGeneratedSdkAfterAddingIntegration ✅ ▶️ View recording
✅ RestoreSupportsConfigOnlyHelperPackageAndCrossPackageTypes ✅ ▶️ View recording
✅ RunFromParentDirectory_UsesExistingConfigNearAppHost ✅ ▶️ View recording
❔ RunPublishFailureScenarioAsync ❔ ▶️ View recording
✅ RunReportsSyntaxErrorsForDotNetAppHost ✅ ▶️ View recording
✅ RunReportsSyntaxErrorsForTypeScriptAppHost ✅ ▶️ View recording
✅ SecretCrudOnDotNetAppHost ✅ ▶️ View recording
✅ SecretCrudOnTypeScriptAppHost ✅ ▶️ View recording
✅ StagingChannel_ConfigureAndVerifySettings_ThenSwitchChannels ✅ ▶️ View recording
✅ StartAndWaitForTypeScriptSqlServerAppHostWithNativeAssets ✅ ▶️ View recording
✅ StartReportsSyntaxErrorsForDotNetAppHost ✅ ▶️ View recording
✅ StartReportsSyntaxErrorsForTypeScriptAppHost ✅ ▶️ View recording
✅ StopAllAppHostsFromAppHostDirectory ✅ ▶️ View recording
✅ StopJavaPolyglotAppHostUsingApphostDirectory ✅ ▶️ View recording
✅ StopNonInteractiveSingleAppHost ✅ ▶️ View recording
✅ StopTypeScriptPolyglotAppHostUsingApphostDirectory ✅ ▶️ View recording
✅ StopWithNoRunningAppHostExitsSuccessfully ✅ ▶️ View recording
✅ UnAwaitedChainsCompileWithAutoResolvePromises ✅ ▶️ View recording
✅ UpdateProjectChannelToStable_TypeScript_PicksUpStablePackages ✅ ▶️ View recording

📹 Recordings uploaded automatically from CI run #26258589439

@davidfowl
David Fowler (davidfowl) merged commit 82f8334 into main May 22, 2026
306 checks passed
@microsoft-github-policy-service microsoft-github-policy-service Bot added this to the 13.4 milestone May 22, 2026
@aspire-repo-bot

Copy link
Copy Markdown
Contributor

✅ No documentation update needed.

docs_optional → bug_fix_restores_documented_behavior (+ existing docs PR already covers the new API surface)

No triggered signals (signal_count = 0; recommendation = docs_optional).

Changed files (12 total): all under src/Aspire.Hosting.Azure.AppContainers/ and tests/Aspire.Hosting.Azure.Tests/ — no path-pattern signals fired.

Why no new docs PR is needed:

  1. Bug fix portion (AsExisting() honoured at publish time) — falls under bug_fix_restores_documented_behavior: the AsExisting API and its intended semantics are already documented; the PR restores the implementation to match that documented contract (fixes Azure Container App Environment with AsExisting() creates new infrastructure instead of using existing resources #12977 and Support existing ACA environment resource #9094).

  2. New API surface (WithAcrPullIdentity) — the PR author (Eric Erhardt (@eerhardt)) has already filed a comprehensive docs PR at Document AsExisting on AzureContainerAppEnvironment and WithAcrPullIdentity aspire.dev#1045 (open, targeting release/13.4) that covers both the AsExisting fix and the new WithAcrPullIdentity API, including a combination matrix, code samples, and rejection cases. Creating a second docs PR would duplicate that work.

Please review microsoft/aspire.dev#1045 (already requested: @IEvangelist) to ensure all user-facing changes are documented before merging.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

4 participants