From 3f5bce6dd73dd3b0399d68e1582ecb941c13407d Mon Sep 17 00:00:00 2001 From: stackgen-terraform-bot Date: Tue, 9 Jun 2026 07:56:25 +0000 Subject: [PATCH] feat: update . Closes stackgen-demo/azure_tf_sql_database#2 --- README.md | 67 ++++++++++++++++++++++++++-------------------------- main.tf | 7 ++++++ variables.tf | 27 +++++++++++++++++++++ 3 files changed, 67 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 1ce836b..fbe7e76 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/main.tf b/main.tf index 30bba00..ad40016 100644 --- a/main.tf +++ b/main.tf @@ -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 } diff --git a/variables.tf b/variables.tf index b1d236c..497bd20 100644 --- a/variables.tf +++ b/variables.tf @@ -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 +}