Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 33 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
# SQL Database Module
# azure_tf_sql_database

Creates an Azure SQL Server and Database with a firewall rule allowing Azure services.
Terraform module that provisions an Azure SQL Server, a SQL Database, and a
firewall rule allowing Azure services.

## Usage
## Features

- Azure SQL Server (TLS 1.2 minimum)
- Azure SQL Database with configurable SKU and size
- Firewall rule allowing Azure services
- **Long-Term Retention (LTR) policy** — retains monthly backups for 13 months by default

## Long-Term Retention Policy

The module configures an LTR policy on the `azurerm_mssql_database` resource.
By default, monthly backups are retained for **13 months** (`P13M`), satisfying
common long-term data retention requirements (e.g., year-over-year reporting).

Durations use ISO 8601 format:

| Variable | Default | Meaning |
|--------------------------|---------|------------------------------------------|
| `ltr_weekly_retention` | `PT0S` | Weekly backup retention (disabled) |
| `ltr_monthly_retention` | `P13M` | Monthly backup retention (13 months) |
| `ltr_yearly_retention` | `PT0S` | Yearly backup retention (disabled) |
| `ltr_week_of_year` | `1` | Week-of-year for yearly backup (1-52) |

To override, e.g. to also keep 5 yearly backups:

```hcl
module "sql" {
source = "./modules/sql_database"

server_name = "my-sql-server-123"
database_name = "mydb"
resource_group_name = "se-rg"
location = "eastus"
admin_login = "sqladmin"
admin_password = "P@ssw0rd1234!"
sku_name = "Basic"

tags = {
environment = "workshop"
}
source = "./azure_tf_sql_database"
# ...
ltr_monthly_retention = "P13M"
ltr_yearly_retention = "P5Y"
ltr_week_of_year = 1
}
```

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|----------|
| server_name | SQL server name (globally unique) | string | — | yes |
| database_name | Database name | string | — | yes |
| resource_group_name | Resource group name | string | — | yes |
| location | Azure region | string | — | yes |
| admin_login | SQL admin username | string | — | yes |
| admin_password | SQL admin password | string | — | yes |
| sku_name | Database SKU | string | `Basic` | no |
| max_size_gb | Max DB size in GB | number | `2` | no |
| tags | Resource tags | map(string) | `{}` | no |
See `variables.tf` for the full list of inputs.

## Outputs

| Name | Description |
|------|-------------|
| server_id | SQL server resource ID |
| server_fqdn | SQL server FQDN |
| database_id | Database resource ID |
| database_name | Database name |
| connection_string | ADO.NET connection string (sensitive) |
See `outputs.tf` for the full list of outputs.
7 changes: 7 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ resource "azurerm_mssql_database" "this" {
sku_name = var.sku_name
max_size_gb = var.max_size_gb

long_term_retention_policy {
weekly_retention = var.ltr_weekly_retention
monthly_retention = var.ltr_monthly_retention
yearly_retention = var.ltr_yearly_retention
week_of_year = var.ltr_week_of_year
}

tags = var.tags
}

Expand Down
27 changes: 27 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,30 @@ variable "tags" {
type = map(string)
default = {}
}

# --- Long-Term Retention (LTR) policy ---
# Default policy retains monthly backups for 13 months to satisfy long-term
# data retention requirements. ISO 8601 duration format (e.g., P13M = 13 months).
variable "ltr_weekly_retention" {
description = "Weekly LTR retention duration in ISO 8601 format (e.g., P1W). Use PT0S to disable."
type = string
default = "PT0S"
}

variable "ltr_monthly_retention" {
description = "Monthly LTR retention duration in ISO 8601 format. Defaults to 13 months (P13M)."
type = string
default = "P13M"
}

variable "ltr_yearly_retention" {
description = "Yearly LTR retention duration in ISO 8601 format (e.g., P5Y). Use PT0S to disable."
type = string
default = "PT0S"
}

variable "ltr_week_of_year" {
description = "Week of the year (1-52) to take the yearly LTR backup. Required when yearly_retention is set; ignored otherwise."
type = number
default = 1
}