Skip to content

About

Azure SQL Database Terraform module — parameterised, azurerm 4.x, CI-validated · AIRCLOUD

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

AIRCLOUD: DevOps, cloud and AI engineering

azure-sql-database-module

Azure SQL Database Terraform module — parameterised, azurerm 4.x, CI-validated.

CI License: MIT Release

Enterprise-grade Azure SQL Database module with comprehensive security, compliance, and performance features.

Features

✅ High Availability - Zone redundancy, geo-redundancy, failover groups, read replicas
✅ Advanced Security - TDE, auditing, threat detection, vulnerability assessment, Azure Policy
✅ Network Security - Private endpoints, VNet integration, firewall rules, service endpoints
✅ Backup & Recovery - Automated backups, retention policies, point-in-time restore, LTR
✅ Performance - Multiple service tiers, auto-scaling, performance insights, query store
✅ Compliance - Azure Policy integration, audit logging, data classification
✅ Identity - Azure AD authentication, managed identities, RBAC
✅ Monitoring - Advanced diagnostics, Log Analytics, Application Insights integration
✅ Testing - Comprehensive Terratest suite with security and performance validation

Usage

Pin the module to a release tag:

module "sql_database" {
  source = "git::https://github.com/AIRCLOUD-PL/azure-sql-database-module.git?ref=v1.0.0"

  # required inputs
  environment         = "..."
  location            = "..."
  resource_group_name = "..."
}

All inputs and outputs are listed in the generated reference below (Inputs, Outputs). Worked examples live in examples/.

Basic Example

module "sql_database" {
  source = "git::https://github.com/AIRCLOUD-PL/azure-sql-database-module.git?ref=v1.0.0"

  sql_server_name           = "sql-prod-westeurope-001"
  location                  = "East US"
  resource_group_name       = "rg-production"
  environment               = "prod"

  administrator_login       = "sqladmin"
  administrator_login_password = "P@ssw0rd123!"

  databases = {
    "appdb" = {
      sku_name    = "GP_Gen5_2"
      max_size_gb = 32
    }
  }

  tags = {
    Environment = "Production"
  }
}

Enterprise Example with Full Security

module "sql_database" {
  source = "git::https://github.com/AIRCLOUD-PL/azure-sql-database-module.git?ref=v1.0.0"

  sql_server_name           = "sql-prod-westeurope-001"
  location                  = "East US"
  resource_group_name       = "rg-production"
  environment               = "prod"

  # Authentication
  administrator_login       = "sqladmin"
  administrator_login_password = "P@ssw0rd123!"

  azuread_administrator = {
    login_username = "sql-admin@domain.com"
    object_id      = "00000000-0000-0000-0000-000000000000"
  }

  # Security Configuration
  public_network_access_enabled = false
  minimum_tls_version          = "1.2"
  enable_advanced_threat_protection = true
  enable_vulnerability_assessment  = true
  enable_extended_auditing_policy  = true

  # Identity
  identity_type = "SystemAssigned"

  # Private Endpoints
  private_endpoints = {
    sql_server = {
      subnet_id = azurerm_subnet.database.id
      private_dns_zone_ids = [
        azurerm_private_dns_zone.sql.id
      ]
    }
  }

  # High Availability Databases
  databases = {
    "appdb" = {
      sku_name        = "BC_Gen5_4"
      max_size_gb     = 100
      zone_redundant  = true
      read_scale      = true
      read_replica_count = 1

      transparent_data_encryption = {
        enabled = true
      }

      short_term_retention_policy = {
        retention_days = 35
        backup_interval_in_hours = 12
      }

      long_term_retention_policy = {
        weekly_retention  = "P12W"
        monthly_retention = "P60M"
        yearly_retention  = "P10Y"
        week_of_year      = 1
      }
    }

    "reporting" = {
      sku_name    = "HS_Gen5_2"
      max_size_gb = 512
      read_replica_count = 1
    }
  }

  # Firewall Rules (for maintenance)
  firewall_rules = {
    "azure-services" = {
      start_ip_address = "0.0.0.0"
      end_ip_address   = "0.0.0.0"
    }
  }

  # Virtual Network Rules
  virtual_network_rules = {
    "app-subnet" = {
      subnet_id = azurerm_subnet.application.id
    }
  }

  # Diagnostic Settings
  enable_diagnostic_settings = true
  log_analytics_workspace_id = azurerm_log_analytics_workspace.example.id

  diagnostic_settings = {
    logs = [
      {
        category = "SQLSecurityAuditEvents"
        enabled  = true
      },
      {
        category = "SQLInsights"
        enabled  = true
      }
    ]
    metrics = [
      {
        category = "Basic"
        enabled  = true
      }
    ]
  }

  # Resource Protection
  enable_resource_lock = true
  lock_level           = "CanNotDelete"

  tags = {
    Environment = "Production"
    DataClass   = "Confidential"
    Compliance  = "SOX"
    Owner       = "Platform Team"
  }
}

High Availability with Failover Group

module "sql_database_primary" {
  source = "git::https://github.com/AIRCLOUD-PL/azure-sql-database-module.git?ref=v1.0.0"

  sql_server_name           = "sql-prod-primary"
  location                  = "East US"
  resource_group_name       = "rg-production"
  environment               = "prod"

  administrator_login       = "sqladmin"
  administrator_login_password = "P@ssw0rd123!"

  databases = {
    "appdb" = {
      sku_name    = "BC_Gen5_4"
      max_size_gb = 100
      zone_redundant = true
    }
  }
}

module "sql_database_secondary" {
  source = "git::https://github.com/AIRCLOUD-PL/azure-sql-database-module.git?ref=v1.0.0"

  sql_server_name           = "sql-prod-secondary"
  location                  = "West US"
  resource_group_name       = "rg-production"
  environment               = "prod"

  administrator_login       = "sqladmin"
  administrator_login_password = "P@ssw0rd123!"

  databases = {
    "appdb" = {
      sku_name    = "BC_Gen5_4"
      max_size_gb = 100
    }
  }
}

# Failover Group
resource "azurerm_mssql_failover_group" "example" {
  name         = "sql-failover-group"
  server_id    = module.sql_database_primary.sql_server_id
  databases    = [module.sql_database_primary.database_ids["appdb"]]

  partner_server {
    id = module.sql_database_secondary.sql_server_id
  }

  read_write_endpoint_failover_policy {
    mode          = "Automatic"
    grace_minutes = 60
  }

  tags = {
    Environment = "Production"
    Project     = "SQL Database HA"
  }
}

Examples

  • Basic - Simple SQL Database with private endpoint
  • High Availability - Production-ready HA setup with failover groups

Security Features

Data Protection

  • Transparent Data Encryption (TDE) - Always encrypted data at rest
  • Advanced Threat Protection - Real-time security monitoring and alerts
  • Vulnerability Assessment - Automated security scanning with email notifications
  • Extended Auditing - Comprehensive audit logging to storage/Log Analytics

Network Security

  • Private Endpoints - Secure private connectivity without public exposure
  • VNet Integration - Network isolation and service endpoints
  • Firewall Rules - Granular IP-based access control
  • TLS 1.2 Minimum - Secure data in transit encryption

High Availability & Disaster Recovery

  • Zone Redundancy - Cross-availability zone replication
  • Geo-Redundancy - Cross-region failover with automatic failover groups
  • Read Replicas - Read scale-out for performance optimization
  • Automated Backups - Point-in-time restore with configurable retention

Compliance & Governance

  • Azure Policy Integration - Automated compliance enforcement
  • Azure AD Integration - Modern authentication with conditional access
  • Managed Identity - Secure service-to-service authentication
  • Resource Locks - Protection against accidental deletion
  • Diagnostic Settings - Comprehensive monitoring and alerting

Testing

The module includes comprehensive Terratest suites:

# Run all tests
cd test
go test -v

# Run specific test
go test -run TestSQLDatabaseBasic -v

# Run with parallel execution
go test -parallel 4 -v

Test scenarios include:

  • Basic SQL Server and database creation
  • Security features validation
  • Private endpoint connectivity
  • High availability configurations
  • Backup and retention policies
  • Firewall rules and network security
  • Resource locks and protection
  • Input validation and error handling

Version

Current version: v1.0.0

Contributing

  1. Follow the established patterns for enterprise security
  2. Include comprehensive tests for new features
  3. Update documentation and examples
  4. Ensure backward compatibility

License

MIT

Reference

Requirements

Name Version
terraform >= 1.5.0
azurerm ~> 4.0

Providers

Name Version
azurerm 4.81.0

Modules

No modules.

Resources

Name Type
azurerm_management_lock.sql_databases resource
azurerm_management_lock.sql_server resource
azurerm_monitor_diagnostic_setting.sql_databases resource
azurerm_monitor_diagnostic_setting.sql_server resource
azurerm_mssql_database.databases resource
azurerm_mssql_database_extended_auditing_policy.databases resource
azurerm_mssql_failover_group.main resource
azurerm_mssql_firewall_rule.firewall_rules resource
azurerm_mssql_server.main resource
azurerm_mssql_server_extended_auditing_policy.main resource
azurerm_mssql_server_security_alert_policy.main resource
azurerm_mssql_server_vulnerability_assessment.main resource
azurerm_mssql_virtual_network_rule.vnet_rules resource
azurerm_private_endpoint.sql_server resource
azurerm_resource_group_policy_assignment.sql_auditing resource
azurerm_resource_group_policy_assignment.sql_encryption resource
azurerm_resource_group_policy_assignment.sql_public_access resource
azurerm_resource_group_policy_assignment.sql_threat_detection resource
azurerm_resource_group.main data source

Inputs

Name Description Type Default Required
administrator_login Administrator login name string null no
administrator_login_password Administrator login password string null no
auditing_policy_log_monitoring_enabled Enable log monitoring for auditing bool true no
auditing_policy_retention_in_days Retention days for auditing logs number 30 no
auditing_policy_storage_account_access_key Storage account access key for auditing string null no
auditing_policy_storage_account_access_key_is_secondary Use secondary storage account access key bool false no
auditing_policy_storage_endpoint Storage endpoint for auditing policy string null no
azuread_administrator Azure AD administrator configuration
object({
login_username = string
object_id = string
azuread_authentication_only = optional(bool, false)
})
null no
databases Map of databases to create
map(object({
collation = optional(string, "SQL_Latin1_General_CP1_CI_AS")
license_type = optional(string, "LicenseIncluded")
max_size_gb = optional(number, 32)
sku_name = optional(string, "GP_Gen5_2")
zone_redundant = optional(bool, false)
read_scale = optional(bool, false)
read_replica_count = optional(number, 0)
auto_pause_delay_in_minutes = optional(number)
min_capacity = optional(number)
maintenance_configuration_name = optional(string, "SQL_Default")
ledger_enabled = optional(bool, false)

transparent_data_encryption = optional(object({
enabled = bool
}))

short_term_retention_policy = optional(object({
retention_days = number
backup_interval_in_hours = optional(number)
}))

long_term_retention_policy = optional(object({
weekly_retention = optional(string)
monthly_retention = optional(string)
yearly_retention = optional(string)
week_of_year = optional(number)
}))
}))
{} no
diagnostic_settings Diagnostic settings configuration
object({
logs = list(object({
category = string
}))
metrics = list(object({
category = string
enabled = bool
}))
})
{
"logs": [
{
"category": "SQLSecurityAuditEvents"
},
{
"category": "SQLInsights"
},
{
"category": "AutomaticTuning"
},
{
"category": "QueryStoreRuntimeStatistics"
},
{
"category": "QueryStoreWaitStatistics"
},
{
"category": "Errors"
},
{
"category": "DatabaseWaitStatistics"
},
{
"category": "Timeouts"
},
{
"category": "Blocks"
},
{
"category": "Deadlocks"
}
],
"metrics": [
{
"category": "Basic",
"enabled": true
},
{
"category": "InstanceAndAppAdvanced",
"enabled": true
},
{
"category": "WorkloadManagement",
"enabled": true
}
]
}
no
enable_advanced_threat_protection Enable Advanced Threat Protection bool true no
enable_diagnostic_settings Enable diagnostic settings for SQL Server and databases bool true no
enable_extended_auditing_policy Enable extended auditing policy bool true no
enable_policy_assignments Enable Azure Policy assignments for SQL Database security bool true no
enable_resource_lock Enable resource lock for SQL Server and databases bool false no
enable_vulnerability_assessment Enable vulnerability assessment bool true no
environment Environment name (e.g., prod, dev, test) string n/a yes
failover_group Failover group configuration
object({
name = string
partner_server_id = string
read_write_endpoint_failover_policy = object({
mode = string
grace_minutes = optional(number)
})
readonly_endpoint_failover_policy = optional(object({
mode = string
}))
})
null no
firewall_rules Map of firewall rules
map(object({
start_ip_address = string
end_ip_address = string
}))
{} no
identity_ids List of User Assigned Identity IDs list(string) [] no
identity_type Type of Managed Identity string "SystemAssigned" no
location Azure region where resources will be created string n/a yes
lock_level Resource lock level: CanNotDelete or ReadOnly string "CanNotDelete" no
log_analytics_workspace_id Log Analytics workspace ID for diagnostic settings string null no
minimum_tls_version Minimum TLS version string "1.2" no
naming_prefix Prefix for SQL Server naming string "sql" no
primary_user_assigned_identity_id Primary user assigned identity ID string null no
private_endpoints Private endpoint configurations
object({
sql_server = optional(object({
subnet_id = string
private_dns_zone_ids = optional(list(string))
}))
})
{} no
public_network_access_enabled Enable public network access bool false no
resource_group_name Name of the resource group string n/a yes
security_alert_policy_disabled_alerts Disabled alerts for security policy list(string) [] no
security_alert_policy_email_account_admins Email account admins for security alerts bool true no
security_alert_policy_email_addresses Email addresses for security alerts list(string) [] no
security_alert_policy_retention_days Retention days for security alerts number 30 no
sql_server_name Name of the SQL Server. If null, will be auto-generated. string null no
sql_version SQL Server version string "12.0" no
tags Tags to apply to all resources map(string) {} no
virtual_network_rules Map of virtual network rules
map(object({
subnet_id = string
}))
{} no
vulnerability_assessment_email_subscription_admins Email subscription admins for vulnerability scans bool true no
vulnerability_assessment_emails Email addresses for vulnerability scans list(string) [] no
vulnerability_assessment_recurring_scans_enabled Enable recurring vulnerability scans bool true no
vulnerability_assessment_storage_account_access_key Storage account access key for vulnerability assessment string null no
vulnerability_assessment_storage_container_path Storage container path for vulnerability assessment string null no

Outputs

Name Description
administrator_login Administrator login name
database_ids Map of database names to database IDs
databases Map of database names to database objects
failover_group_id Failover group ID
firewall_rules Created firewall rules
identity Managed identity block
identity_principal_id Principal ID of the system-assigned managed identity
private_endpoint_sql_server_id SQL Server private endpoint ID
sql_server_fqdn SQL Server fully qualified domain name
sql_server_id SQL Server resource ID
sql_server_name SQL Server name
virtual_network_rules Created virtual network rules

Part of the AIRCLOUD Azure module catalog

Browse all modules with usage snippets in the module catalog or on github.com/AIRCLOUD-PL. Built and maintained by AIRCLOUD: DevOps, cloud and AI engineering. Need a hand with Azure landing zones or platform engineering? See aircloud.com.pl/consulting.

About

Azure SQL Database Terraform module — parameterised, azurerm 4.x, CI-validated · AIRCLOUD

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages