You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds Terraform samples for AI Gateway, the Terraform counterparts of the Bicep AI Gateway tier sample
(infrastructure-setup-bicep/01-connections/ai-gateway-tier). New files only (36 files in three new folders);
no existing files change.
infrastructure/infrastructure-setup-terraform/
├── golden-path/README.md Terraform edition of the Foundry golden path
├── 01-connections/ai-gateway-tier/ Public path (hub + spoke)
│ ├── modules/ai-gateway/ gateway + Foundry User grant + MI provider + model (token limit) + alias + runtime key
│ ├── modules/ai-gateway-connection/ ModelGateway "Admin-connected models" connection
│ ├── hub/ run once: Foundry account + model + hub project + gateway
│ └── connection/ run per project: reads gateway host + key, creates the connection
└── 16-private-network-standard-agent-apim-setup-preview/extensions/ai-gateway-tier-private/
└── code/ one apply: private standard-agent foundation + private model account
+ VNet-integrated gateway + connection (reuses the modules)
The folders mirror the Bicep AI Gateway samples, and the golden-path README maps the same six steps to
Terraform: 41-standard-agent-setup for the account and project, these samples for the gateway and model
connection, and the existing public-byom-apim/samples/create-agent.py for the agent.
Why
There were no Terraform samples for AI Gateway. These use the Microsoft.ApiManagement/aigateways resource
type, because new Microsoft.ApiManagement/service + AIGateway SKU resources can no longer be created
(migration guide). Resource shapes and the 2025-09-01-preview API version follow the official AI Gateway labs.
Compared with the earlier service + AIGateway SKU form:
The runtime host comes from properties.frontend.defaultHostname (*.ai.gateway.azure.com).
The callable model name is an explicit alias.
The gateway's outbound (private routing) subnet is delegated to Microsoft.App/environments.
Keys are sent as Authorization: Bearer <key>; a request with both api-key and Authorization gets HTTP 400.
Design points:
The gateway key is read with an ephemerallistSecrets and sent as a write-only connection
credential, so it is never stored in Terraform state (Terraform ≥ 1.11).
The connection/ root reads the gateway's runtime host and key itself, so there's no key copy-paste, and it
works for a project in another subscription.
terraform destroy purges the soft-deleted Foundry accounts, so names and model quota are released immediately.
assign_foundry_user_role = false supports organizations where an administrator grants the gateway
identity's Foundry User role separately (steps in the README).
The connection's auth header is configurable (auth_header_name / auth_header_format); the default is Authorization: Bearer {api_key}.
Security
No credentials in the repo; the gateway key never enters Terraform state. The gateway runtime key is read
with an ephemeral listSecrets and sent to the Foundry connection as a write-only value; example .tfvars
files contain only placeholders. All 36 files were scanned for keys, tokens, connection strings, private keys,
emails, public IPs, and environment-specific identifiers: none found.
Terraform state still holds Storage and Cosmos DB keys. The azurerm provider always records these account
keys in state (as in the other Terraform samples), but both services have key auth disabled here, so the keys
can't access data. Treat state as sensitive anyway: use a secured remote backend (the backend "azurerm" {}
block in versions.tf).
Keyless. Every account and data store has local / API-key auth disabled: both Foundry accounts, AI Search,
Cosmos DB, and Storage (shared keys). Access is Entra ID + RBAC only; verified live, the agent account refuses
to list keys.
Private by default (VNet sample). The model account and all data stores have public network access disabled
and are reachable only through private endpoints; the gateway reaches the model over its VNet integration.
AI Gateway's inbound endpoint is public (it doesn't support inbound private endpoints yet) and requires the
runtime key.
Testing switches (foundry_allowed_ip_ranges, foundry_public_network_access_enabled) are off by default,
mutually exclusive, and still require Entra ID.
Supply chain. No workflows or package manifests are added. The READMEs pin the agent-step Python packages
to the tested versions (azure-ai-projects==2.11.0, azure-identity==1.27.0).
Notes for reviewers
Review feedback: the three Copilot review comments (AI Search local auth, Storage data-plane reads, and
the testing-switch precedence) are addressed in 2f4554f; see each thread. The same commit also disables API
keys on the agent's Foundry account and the Storage account's public endpoint.
The two later comments (project endpoint subdomain, role grant on gateway recreate) are addressed in 3bc7d95
and verified live on the public path, including terraform apply -replace of the gateway.
API version: the gateway and its child resources use 2025-09-01-preview, as in the official AI Gateway
labs. The Bicep sample in this repo uses 2026-11-01 for the gateway and 2026-09-01-preview for its
children. Azure Resource Manager still accepts all of them (re-checked 2026-11-27).
Region: the Terraform samples create their resource group, so location is an explicit variable, as in
the other Terraform samples (default eastus2 for the hub, swedencentral for the private sample). They
don't inherit the resource group's region the way the Bicep samples do since Samples inherit resource group instead of defaulting to eastus2 #1183.
Static checks on the latest main (2026-11-27) for all three roots (hub/, connection/, private code/),
with Terraform 1.17.2, azapi 2.14.0, and azurerm 4.81.0:
terraform fmt -recursive -check — passed
terraform init -backend=false and terraform validate — passed
plan with both testing switches set — fails with the intended validation error; with either one alone — passes
Relative links and script paths in the three READMEs — all 39 resolve
Azure Resource Manager still offers every API version the samples use (including Microsoft.ApiManagement/aigateways@2025-09-01-preview)
Live, private VNet sample after the security hardening (2026-11-27, Sweden Central):
terraform apply — passed: 58 resources in one apply, capability host Succeeded.
Verified in Azure: AI Search disableLocalAuth: true (no authOptions) and public access disabled; Storage
public access disabled and shared keys off; Cosmos DB local auth off and public access disabled; model (hub)
account keys off and public access disabled; agent account keys off (listKeys is refused).
A second terraform plan — No changes.
Direct POST <gateway>/default/models/openai/v1/chat/completions with Authorization: Bearer — returned a gpt-5.4 completion from the private model.
infrastructure-setup-bicep/01-connections/public-byom-apim/samples/create-agent.py --model ai-gateway/gpt-5.4
(prompt agent, Responses API, Entra ID only) — answered on the first attempt through the agent data proxy →
gateway → private model.
terraform destroy — removed every resource the sample created and purged both soft-deleted Foundry accounts. Only the resource group delete stopped, on NSGs that a tenant Azure Policy had added (not created by Terraform); after deleting the group, nothing from the test remains.
Live, public hub-and-spoke path (2026-11-18, East US 2; unchanged since apart from the README install line):
terraform apply in hub/ — passed (13 resources, about 3.5 minutes); a second terraform plan reports No changes.
terraform apply in connection/ — passed; a second plan reports No changes; the gateway key is not
present in Terraform state.
Direct gateway call with Authorization: Bearer — returned a gpt-5.4 completion; a burst of requests returned
HTTP 429 "Token limit is exceeded"; a request with both api-key and Authorization returned HTTP 400.
terraform destroy for both roots — passed; nothing left behind, account purged.
Fixes from the earlier live runs are included: the connection ignores the isSharedToAll read-back (so a second plan shows no drift), the gateway subnet's NSG rules are standalone resources (so policy-injected rules don't
cause drift), and connection operations retry on transient etag conflicts.
Not checked: a spoke project in a different subscription from the hub (same code path, using absolute resource
IDs).
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Search API-key authentication undermines the advertised Entra-only boundary, while default Storage data-plane reads can cause workstation Terraform applies to fail with HTTP 403.
Adds Terraform counterparts to the Bicep AI Gateway samples, integrating public hub-and-spoke and private-backend deployments into the Foundry golden path.
Changes:
Adds reusable gateway and project-connection modules, with separate public hub and spoke deployment roots.
Adds a self-contained private-network sample with agent infrastructure, private model access, and gateway integration.
Documents deployment, agent creation, permissions, testing, and cleanup.
Paths are relative to infrastructure/infrastructure-setup-terraform/.
Terraform counterparts of the Bicep AI Gateway tier samples, built on the
Microsoft.ApiManagement/aigateways resource type.
- 01-connections/ai-gateway-tier: shared ai-gateway and
ai-gateway-connection modules, a hub/ root (run once) and a connection/
root (run per spoke project).
- 16-private-network-standard-agent-apim-setup-preview/extensions/
ai-gateway-tier-private: self-contained network-secured variant (private
standard-agent foundation + private model account + VNet-integrated
gateway + connection) that reuses the same modules.
- golden-path/README.md: Terraform edition of the Foundry golden path.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- AI Search: disable local (API key) auth so access is Entra ID only,
governed by the project identity's Search RBAC roles.
- Agent Foundry account: disable local auth (Entra ID only), matching the
private hub account.
- Storage: disable the public endpoint (private endpoint only) and set the
azurerm storage.data_plane_available = false feature so plan and apply
don't need data-plane access from outside the VNet.
- Make foundry_allowed_ip_ranges and foundry_public_network_access_enabled
mutually exclusive (validation error instead of an ambiguous setting).
- READMEs: document the hardening and pin the agent-step Python packages to
the tested versions.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The endpoint output can point to the wrong hostname when the custom subdomain differs from the account name, and recreating the gateway can fail to restore backend access because the role assignment does not accommodate a changed principal ID.
- ai-gateway-connection: build project_endpoint from the account's
customSubDomainName instead of its ARM name, which can differ (for example
when a sample adds a random suffix to the subdomain only).
- ai-gateway: include the gateway's principal ID in the Foundry User role
assignment name, so recreating the gateway (new identity) replaces the
grant instead of attempting an in-place update Azure doesn't allow.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Storage and Cosmos DB account keys remain in Terraform state, violating the no-credentials guarantee, while valid mixed-case project IDs can also break endpoint resolution.
AzureRM's Storage credential handling and Cosmos DB read path persist account keys in Terraform state even when shared-key/local authentication is disabled; the Cosmos DB resource at line 25 has the same issue. This contradicts the PR's “no credentials in Terraform state” guarantee, and disabling optional Storage data-plane reads does not prevent these management-plane key reads. Manage both accounts through AzAPI with non-secret exports and update their consumers, or narrow the guarantee and document that state needs credential-level protection.
On the review summary's "Storage and Cosmos DB keys remain in Terraform state": correct, and the PR description's Security section is now worded precisely. The gateway runtime key never enters state (ephemeral read + write-only connection credential). The azurerm provider always records Storage and Cosmos DB account keys in state, as in the other Terraform samples; in this sample both services have key auth disabled (shared_access_key_enabled = false, local_authentication_enabled = false, verified live), so those keys cannot access data. The README's remote-backend guidance (backend "azurerm" {} in versions.tf) covers protecting state. Leaving the providers as-is to stay consistent with the existing Terraform samples.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed to evaluate a credential-refresh gap that can leave gateway connections using deleted keys and cause authentication failures.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
Adds Terraform samples for AI Gateway, the Terraform counterparts of the Bicep AI Gateway tier sample
(
infrastructure-setup-bicep/01-connections/ai-gateway-tier). New files only (36 files in three new folders);no existing files change.
The folders mirror the Bicep AI Gateway samples, and the golden-path README maps the same six steps to
Terraform:
41-standard-agent-setupfor the account and project, these samples for the gateway and modelconnection, and the existing
public-byom-apim/samples/create-agent.pyfor the agent.Why
There were no Terraform samples for AI Gateway. These use the
Microsoft.ApiManagement/aigatewaysresourcetype, because new
Microsoft.ApiManagement/service+AIGatewaySKU resources can no longer be created(migration guide). Resource shapes and the
2025-09-01-previewAPI version follow the official AI Gateway labs.Compared with the earlier
service+AIGatewaySKU form:properties.frontend.defaultHostname(*.ai.gateway.azure.com).Microsoft.App/environments.Authorization: Bearer <key>; a request with bothapi-keyandAuthorizationgets HTTP 400.Design points:
listSecretsand sent as a write-only connectioncredential, so it is never stored in Terraform state (Terraform ≥ 1.11).
connection/root reads the gateway's runtime host and key itself, so there's no key copy-paste, and itworks for a project in another subscription.
terraform destroypurges the soft-deleted Foundry accounts, so names and model quota are released immediately.assign_foundry_user_role = falsesupports organizations where an administrator grants the gatewayidentity's Foundry User role separately (steps in the README).
auth_header_name/auth_header_format); the default isAuthorization: Bearer {api_key}.Security
with an ephemeral
listSecretsand sent to the Foundry connection as a write-only value; example.tfvarsfiles contain only placeholders. All 36 files were scanned for keys, tokens, connection strings, private keys,
emails, public IPs, and environment-specific identifiers: none found.
keys in state (as in the other Terraform samples), but both services have key auth disabled here, so the keys
can't access data. Treat state as sensitive anyway: use a secured remote backend (the
backend "azurerm" {}block in
versions.tf).Cosmos DB, and Storage (shared keys). Access is Entra ID + RBAC only; verified live, the agent account refuses
to list keys.
and are reachable only through private endpoints; the gateway reaches the model over its VNet integration.
AI Gateway's inbound endpoint is public (it doesn't support inbound private endpoints yet) and requires the
runtime key.
foundry_allowed_ip_ranges,foundry_public_network_access_enabled) are off by default,mutually exclusive, and still require Entra ID.
to the tested versions (
azure-ai-projects==2.11.0,azure-identity==1.27.0).Notes for reviewers
the testing-switch precedence) are addressed in 2f4554f; see each thread. The same commit also disables API
keys on the agent's Foundry account and the Storage account's public endpoint.
The two later comments (project endpoint subdomain, role grant on gateway recreate) are addressed in 3bc7d95
and verified live on the public path, including
terraform apply -replaceof the gateway.2025-09-01-preview, as in the official AI Gatewaylabs. The Bicep sample in this repo uses
2026-11-01for the gateway and2026-09-01-previewfor itschildren. Azure Resource Manager still accepts all of them (re-checked 2026-11-27).
locationis an explicit variable, as inthe other Terraform samples (default
eastus2for the hub,swedencentralfor the private sample). Theydon't inherit the resource group's region the way the Bicep samples do since Samples inherit resource group instead of defaulting to eastus2 #1183.
01-connections/(apim/,model-gateway/) and edits01-connections/README.md; this PR doesn't touch that README.Validation
Static checks on the latest
main(2026-11-27) for all three roots (hub/,connection/, privatecode/),with Terraform 1.17.2, azapi 2.14.0, and azurerm 4.81.0:
terraform fmt -recursive -check— passedterraform init -backend=falseandterraform validate— passedterraform plan— passed:hub/13 resources, privatecode/58 resourcesplanwith both testing switches set — fails with the intended validation error; with either one alone — passesMicrosoft.ApiManagement/aigateways@2025-09-01-preview)Live, private VNet sample after the security hardening (2026-11-27, Sweden Central):
terraform apply— passed: 58 resources in one apply, capability hostSucceeded.disableLocalAuth: true(noauthOptions) and public access disabled; Storagepublic access disabled and shared keys off; Cosmos DB local auth off and public access disabled; model (hub)
account keys off and public access disabled; agent account keys off (
listKeysis refused).terraform plan— No changes.POST <gateway>/default/models/openai/v1/chat/completionswithAuthorization: Bearer— returned agpt-5.4completion from the private model.infrastructure-setup-bicep/01-connections/public-byom-apim/samples/create-agent.py --model ai-gateway/gpt-5.4(prompt agent, Responses API, Entra ID only) — answered on the first attempt through the agent data proxy →
gateway → private model.
terraform destroy— removed every resource the sample created and purged both soft-deleted Foundry accounts. Only the resource group delete stopped, on NSGs that a tenant Azure Policy had added (not created by Terraform); after deleting the group, nothing from the test remains.Live, public hub-and-spoke path (2026-11-18, East US 2; unchanged since apart from the README install line):
terraform applyinhub/— passed (13 resources, about 3.5 minutes); a secondterraform planreportsNo changes.
terraform applyinconnection/— passed; a secondplanreports No changes; the gateway key is notpresent in Terraform state.
Authorization: Bearer— returned agpt-5.4completion; a burst of requests returnedHTTP 429 "Token limit is exceeded"; a request with both
api-keyandAuthorizationreturned HTTP 400.create-agent.py --model ai-gateway/gpt-5.4— passed.terraform destroyfor both roots — passed; nothing left behind, account purged.Fixes from the earlier live runs are included: the connection ignores the
isSharedToAllread-back (so a secondplanshows no drift), the gateway subnet's NSG rules are standalone resources (so policy-injected rules don'tcause drift), and connection operations retry on transient etag conflicts.
Not checked: a spoke project in a different subscription from the hub (same code path, using absolute resource
IDs).