Weldon Web

Automating the MCP Gateway: Infrastructure as Code with Terraform

Updated #Azure#Terraform#MCP

Automating the MCP Gateway with Terraform

ClickOps will eventually ruin your APIM instance. If you are configuring Model Context Protocol (MCP) routing rules by hand in the Azure Portal, you are one typo away from breaking your AI agents.

You need Infrastructure as Code, use a modular Terraform approach to separate the shared security policies from the specific routing patterns - It keeps the blast radius small. Here is how you actually deploy it without tearing your hair out over race conditions.

The Terraform Module Structure

Keep your shared policy fragments separate from your APIs. Your token validation, rate limiting, and error handling should live in one core module. Your specific MCP servers—whether you are using the REST-as-MCP pattern or governing a native MCP server—call those shared fragments.

api_management_name   = "my-apim"
resource_group_name   = "my-rg"
tenant_id             = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
mcp_gateway_audience  = "api://my-mcp-gateway"
 

The Named Value Propagation Trap

Terraform hates waiting. But with Azure API Management, you have to force it.

If you deploy your named values and immediately try to bind them to a policy fragment, the deployment will crash. The APIM control plane needs time to propagate those keys internally.

You must introduce a deliberate sleep cycle. If you are deploying to a standard single-region setup, 120 seconds usually does it.

Multi-Region Considerations

Scaling to a multi-region Premium APIM instance changes the math.

That propagation delay takes longer. You need to bump your time_sleep duration up to at least 180 seconds. Yes, it makes your CI/CD pipeline slower. But it stops random deployment failures.

See the code here https://github.com/jackweldonweb/apim-mcp-terraform, check license for usage information.