diff --git a/README.md b/README.md new file mode 100644 index 0000000..32b7f74 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ + +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.6.4 | +| [azurerm](#requirement\_azurerm) | 3.94.0 | + +## Providers + +| Name | Version | +|------|---------| +| [azurerm](#provider\_azurerm) | 3.94.0 | + +## Modules + +No modules. + +## Resources + +| Name | Type | +|------|------| +| [azurerm_mssql_database.main](https://registry.terraform.io/providers/hashicorp/azurerm/3.94.0/docs/resources/mssql_database) | resource | +| [azurerm_client_config.current](https://registry.terraform.io/providers/hashicorp/azurerm/3.94.0/docs/data-sources/client_config) | data source | +| [azurerm_resource_group.main](https://registry.terraform.io/providers/hashicorp/azurerm/3.94.0/docs/data-sources/resource_group) | data source | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [databases](#input\_databases) | Map of objects to configure MSSQL Database |
map(object({| `{}` | no | +| [default\_retention\_days](#input\_default\_retention\_days) | Point In Time Restore configuration. Value has to be between 1 and 35. | `number` | `3` | no | +| [default\_tags](#input\_default\_tags) | A mapping of tags to assign to the resource. | `map(any)` |
collation = optional(string, "SQL_Latin1_General_CP1_CI_AS")
sku = optional(string, "GP_S_Gen5_1")
max_size_gb = optional(string, "20")
min_capacity = optional(string, "0.5")
auto_pause_delay_in_minutes = optional(number, 60)
create_mode = optional(string, "Default")
creation_source_database_id = optional(string, null)
storage_account_type = optional(string, "ZRS")
}))
{| no | +| [resource\_group\_location](#input\_resource\_group\_location) | Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created. | `string` | `"West Europe"` | no | +| [resource\_group\_name](#input\_resource\_group\_name) | The name of the resource group in which to create the Azure MSSQL and database component. Changing this forces a new resource to be created. | `string` | n/a | yes | +| [server\_id](#input\_server\_id) | The id of the MS SQL Server on which to create the database. Changing this forces a new resource to be created. | `string` | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| [sql\_database\_id](#output\_sql\_database\_id) | The ID of the MS SQL Database. | +| [sql\_database\_max\_size](#output\_sql\_database\_max\_size) | The max size of the database in gigabytes. | +| [sql\_database\_names](#output\_sql\_database\_names) | The name of the MS SQL Database. | +| [sql\_server\_id](#output\_sql\_server\_id) | The ID of the MS SQL server | +| [storage\_account\_type](#output\_storage\_account\_type) | Storage account type used to store backups for this database | + \ No newline at end of file diff --git a/backend.tf b/backend.tf new file mode 100644 index 0000000..7f32883 --- /dev/null +++ b/backend.tf @@ -0,0 +1,9 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "3.94.0" + } + } + required_version = ">= 1.6.4" +} \ No newline at end of file diff --git a/examples/mssql-database/main.tf b/examples/mssql-database/main.tf new file mode 100644 index 0000000..b8900f5 --- /dev/null +++ b/examples/mssql-database/main.tf @@ -0,0 +1,39 @@ +provider "azurerm" { +features {} +} + +data "azurerm_mssql_server" "main" { + name = "dev-63963882ddd-eus-sql" + resource_group_name = "dev-test-weu-rg" +} + +module "mssql_database" { + source = "Think-Cube/mssql-database/azure" + version = "1.0.0" + resource_group_name = "dev-test-weu-rg" + environment = "dev" + region = "weu" + resource_group_location = "West Europe" + server_id = data.azurerm_mssql_server.main.id + databases = { + database1 = { + server_id = coalesce(var.server_id, data.azurerm_mssql_server.main.id) + collation = "SQL_Latin1_General_CP1_CI_AS" + sku = "GP_S_Gen5_1" + max_size_gb = "20" + min_capacity = "0.5" + auto_pause_delay_in_minutes = 60 + create_mode = "Default" + creation_source_database_id = null + storage_account_type = "ZRS" + } + } + default_tags = { + Administrator = "John Doe" + Department = "IT" + CostCentre = "CC123" + ContactPerson = "Jane Smith" + ManagedByTerraform = "True" +} +depends_on = [ data.azurerm_mssql_server.main ] +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..49fe4f0 --- /dev/null +++ b/main.tf @@ -0,0 +1,5 @@ +data "azurerm_client_config" "current" {} + +data "azurerm_resource_group" "main" { + name = var.resource_group_name +} \ No newline at end of file diff --git a/mssql_database.tf b/mssql_database.tf new file mode 100644 index 0000000..24c2f51 --- /dev/null +++ b/mssql_database.tf @@ -0,0 +1,25 @@ +resource "azurerm_mssql_database" "main" { + for_each = var.databases + + name = each.key + server_id = var.server_id + collation = each.value.collation + sku_name = each.value.sku + max_size_gb = each.value.max_size_gb + min_capacity = each.value.min_capacity + auto_pause_delay_in_minutes = each.value.auto_pause_delay_in_minutes + create_mode = each.value.create_mode + creation_source_database_id = each.value.creation_source_database_id + storage_account_type = each.value.storage_account_type == "ZRS" ? "Zone" : "Geo" + tags = var.default_tags + + short_term_retention_policy { + retention_days = lookup(each.value, "retention_days", var.default_retention_days) + } + + lifecycle { + ignore_changes = [ + sku_name, + ] + } +} \ No newline at end of file diff --git a/output.tf b/output.tf new file mode 100644 index 0000000..baea1d2 --- /dev/null +++ b/output.tf @@ -0,0 +1,29 @@ +output "sql_database_id" { + value = { for k, v in azurerm_mssql_database.main : k => v.id } + description = "The ID of the MS SQL Database." + sensitive = false +} + +output "sql_server_id" { + value = { for k, v in azurerm_mssql_database.main : k => v.server_id } + description = "The ID of the MS SQL server" + sensitive = false +} + +output "sql_database_names" { + value = { for k, v in azurerm_mssql_database.main : k => v.name } + description = "The name of the MS SQL Database." + sensitive = false +} + +output "sql_database_max_size" { + value = { for k, v in azurerm_mssql_database.main : k => v.max_size_gb } + description = "The max size of the database in gigabytes." + sensitive = false +} + +output "storage_account_type" { + value = { for k, v in azurerm_mssql_database.main : k => v.storage_account_type } + description = "Storage account type used to store backups for this database" + sensitive = false +} \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..95f333f --- /dev/null +++ b/variables.tf @@ -0,0 +1,48 @@ +########################### +# Common vars +########################### +variable "default_tags" { + description = "A mapping of tags to assign to the resource." + type = map(any) + default = { + "ManagedByTerraform" = "True" + } +} +########################### +# Resource groups vars +########################### +variable "resource_group_location" { + description = "Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created." + default = "West Europe" + type = string +} +variable "resource_group_name" { + description = "The name of the resource group in which to create the Azure MSSQL and database component. Changing this forces a new resource to be created." + type = string +} +########################### +# MSSQL database groups vars +########################### +variable "databases" { + type = map(object({ + collation = optional(string, "SQL_Latin1_General_CP1_CI_AS") + sku = optional(string, "GP_S_Gen5_1") + max_size_gb = optional(string, "20") + min_capacity = optional(string, "0.5") + auto_pause_delay_in_minutes = optional(number, 60) + create_mode = optional(string, "Default") + creation_source_database_id = optional(string, null) + storage_account_type = optional(string, "ZRS") + })) + description = "Map of objects to configure MSSQL Database" + default = {} +} +variable "default_retention_days" { + type = number + description = "Point In Time Restore configuration. Value has to be between 1 and 35." + default = 3 +} +variable "server_id" { + type = string + description = "The id of the MS SQL Server on which to create the database. Changing this forces a new resource to be created." +} \ No newline at end of file
"ManagedByTerraform": "True"
}