I'm having issues with an Azure Container Instance (ACI) timing out when trying to connect to an Azure SQL database through a Private Endpoint. My setup, all provisioned with Terraform, is as follows:
Database
resource "azurerm_sql_server" "azure_sql_server" {
name = "azure-sql-server"
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
location = data.azurerm_resource_group.arc_resource_group.location
administrator_login = var.administrator_login
administrator_login_password = var.administrator_login_password
version = "12.0"
}
resource "azurerm_sql_database" "azure_sql_database" {
name = "database"
server_name = azurerm_sql_server.arc_azure_sql_server.name
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
location = data.azurerm_resource_group.arc_resource_group.location
}
Container Instance
resource "azurerm_container_group" "nginx_container" {
name = "nginx-container-group"
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
location = data.azurerm_resource_group.arc_resource_group.location
ip_address_type = "Private"
os_type = "Linux"
subnet_ids = [azurerm_subnet.subnet_b.id]
container {
name = "nginx-container"
image = "${var.acr_repository}.azurecr.io/docker_image:latest"
cpu = "0.5"
memory = "1.5"
environment_variables = {
"DB_HOST" = format("%s.database.windows.net", azurerm_sql_server.arc_azure_sql_server.name)
}
ports {
port = 80
protocol = "TCP"
}
}
}
Virtual network
data "azurerm_virtual_network" "existing_vnet" {
name = "vnet-uks-dev"
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
}
resource "azurerm_subnet" "subnet_a" {
name = "sql-sn"
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
virtual_network_name = data.azurerm_virtual_network.existing_vnet.name
address_prefixes = ["10.2.1.0/24"]
}
resource "azurerm_subnet" "subnet_b" {
name = "aci-sn"
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
virtual_network_name = data.azurerm_virtual_network.existing_vnet.name
address_prefixes = ["10.2.2.0/24"]
service_endpoints = ["Microsoft.Sql"]
delegation {
name = "aci"
service_delegation {
name = "Microsoft.ContainerInstance/containerGroups"
actions = ["Microsoft.Network/virtualNetworks/subnets/join/action"]
}
}
}
resource "azurerm_subnet" "subnet_c" {
name = "private-link-sn"
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
virtual_network_name = data.azurerm_virtual_network.existing_vnet.name
address_prefixes = ["10.2.3.0/24"]
}
DNS and private endpoint
resource "azurerm_private_dns_zone" "sql_private_dns_zone" {
name = "privatelink.database.windows.net"
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
}
resource "azurerm_private_dns_zone_virtual_network_link" "vnet_link" {
name = "vnet-link-to-private-dns"
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
private_dns_zone_name = azurerm_private_dns_zone.sql_private_dns_zone.name
virtual_network_id = data.azurerm_virtual_network.existing_vnet.id
}
resource "azurerm_sql_virtual_network_rule" "allow_aci_subnet" {
name = "AllowACISubnet"
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
server_name = azurerm_sql_server.arc_azure_sql_server.name
subnet_id = azurerm_subnet.subnet_b.id
}
resource "azurerm_private_endpoint" "database_private_endpoint" {
name = "pe-database"
location = data.azurerm_resource_group.arc_resource_group.location
resource_group_name = data.azurerm_resource_group.arc_resource_group.name
subnet_id = azurerm_subnet.subnet_c.id
private_service_connection {
name = "psc-sql"
private_connection_resource_id = azurerm_sql_server.arc_azure_sql_server.id
subresource_names = ["sqlServer"]
}
private_dns_zone_group {
name = "default"
private_dns_zone_ids = [azurerm_private_dns_zone.sql_private_dns_zone.id]
}
}
Given this setup, my application within the ACI is experiencing timeouts when attempting to write to the Azure SQL database. What could be causing this, and how can I successfully establish the connection between the ACI and SQL database over the private endpoint?
I have constructed this with 3 subnets as private endpoints and container instances must have dedicated subnets and will not accommodate additional resources.
I have confirmed the database connection strings are correct. I have confirmed the server is deploying with azure-sql-server.database.windows.net as its server name.
