Skip to content

Commit b4b6582

Browse files
Add sticky sessions support to terraform module
Enable sticky sessions for MCP session affinity in multi-replica deployments. MCP sessions are stored in-memory, so requests must be routed to the same replica. Uses Azure Container Apps ARRAffinity cookies for session routing. - Add enable_sticky_sessions variable (default: true) - Add null_resource to configure sticky sessions via az cli - Add outputs for sticky sessions status and verification command Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent c5e0f82 commit b4b6582

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

terraform/main.tf

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,33 @@ resource "null_resource" "configure_github_auth" {
435435
}
436436

437437
# ==============================================================================
438-
# 10. ACR Task for CI/CD (Optional)
438+
# 10. Sticky Sessions for MCP Session Affinity
439+
# ==============================================================================
440+
# MCP sessions are stored in-memory. When running multiple replicas, requests
441+
# must be routed to the same replica to maintain session state. Sticky sessions
442+
# use HTTP cookies (ARRAffinity) to ensure session affinity.
443+
resource "null_resource" "sticky_sessions" {
444+
count = var.enable_sticky_sessions ? 1 : 0
445+
446+
triggers = {
447+
container_app_id = azurerm_container_app.app.id
448+
sticky_sessions = var.enable_sticky_sessions
449+
}
450+
451+
provisioner "local-exec" {
452+
command = <<-EOT
453+
az containerapp ingress sticky-sessions set \
454+
--name ${local.container_app_name} \
455+
--resource-group ${var.resource_group_name} \
456+
--affinity sticky
457+
EOT
458+
}
459+
460+
depends_on = [azurerm_container_app.app]
461+
}
462+
463+
# ==============================================================================
464+
# 11. ACR Task for CI/CD (Optional)
439465
# ==============================================================================
440466
resource "azurerm_container_registry_task" "build" {
441467
count = var.github_repo_url != "" && var.github_pat != "" ? 1 : 0

terraform/outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,17 @@ output "github_callback_url" {
127127
description = "GitHub OAuth callback URL"
128128
value = "https://${azurerm_container_app.app.ingress[0].fqdn}/.auth/login/github/callback"
129129
}
130+
131+
# ------------------------------------------------------------------------------
132+
# Session Affinity
133+
# ------------------------------------------------------------------------------
134+
135+
output "sticky_sessions_enabled" {
136+
description = "Whether sticky sessions are enabled for MCP session affinity"
137+
value = var.enable_sticky_sessions
138+
}
139+
140+
output "sticky_sessions_verify_command" {
141+
description = "Command to verify sticky sessions configuration"
142+
value = "az containerapp show --name ${azurerm_container_app.app.name} --resource-group ${var.resource_group_name} --query \"properties.configuration.ingress.stickySessions\""
143+
}

terraform/variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,13 @@ variable "github_pat" {
164164
default = ""
165165
sensitive = true
166166
}
167+
168+
# ------------------------------------------------------------------------------
169+
# Session Affinity Variables
170+
# ------------------------------------------------------------------------------
171+
172+
variable "enable_sticky_sessions" {
173+
description = "Enable sticky sessions for MCP session affinity. Required for multi-replica deployments with stateful MCP sessions."
174+
type = bool
175+
default = true
176+
}

0 commit comments

Comments
 (0)