Skip to content

Commit 15332c6

Browse files
committed
Initial commit: sql_database Terraform module
0 parents  commit 15332c6

5 files changed

Lines changed: 160 additions & 0 deletions

File tree

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.terraform/
2+
*.tfstate
3+
*.tfstate.backup
4+
.terraform.lock.hcl
5+
*.tfvars
6+
crash.log
7+
override.tf
8+
override.tf.json
9+
*_override.tf
10+
*_override.tf.json

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# SQL Database Module
2+
3+
Creates an Azure SQL Server and Database with a firewall rule allowing Azure services.
4+
5+
## Usage
6+
7+
```hcl
8+
module "sql" {
9+
source = "./modules/sql_database"
10+
11+
server_name = "my-sql-server-123"
12+
database_name = "mydb"
13+
resource_group_name = "se-rg"
14+
location = "eastus"
15+
admin_login = "sqladmin"
16+
admin_password = "P@ssw0rd1234!"
17+
sku_name = "Basic"
18+
19+
tags = {
20+
environment = "workshop"
21+
}
22+
}
23+
```
24+
25+
## Inputs
26+
27+
| Name | Description | Type | Default | Required |
28+
|------|-------------|------|---------|----------|
29+
| server_name | SQL server name (globally unique) | string || yes |
30+
| database_name | Database name | string || yes |
31+
| resource_group_name | Resource group name | string || yes |
32+
| location | Azure region | string || yes |
33+
| admin_login | SQL admin username | string || yes |
34+
| admin_password | SQL admin password | string || yes |
35+
| sku_name | Database SKU | string | `Basic` | no |
36+
| max_size_gb | Max DB size in GB | number | `2` | no |
37+
| tags | Resource tags | map(string) | `{}` | no |
38+
39+
## Outputs
40+
41+
| Name | Description |
42+
|------|-------------|
43+
| server_id | SQL server resource ID |
44+
| server_fqdn | SQL server FQDN |
45+
| database_id | Database resource ID |
46+
| database_name | Database name |
47+
| connection_string | ADO.NET connection string (sensitive) |

main.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
resource "azurerm_mssql_server" "this" {
2+
name = var.server_name
3+
resource_group_name = var.resource_group_name
4+
location = var.location
5+
version = "12.0"
6+
administrator_login = var.admin_login
7+
administrator_login_password = var.admin_password
8+
minimum_tls_version = "1.2"
9+
10+
tags = var.tags
11+
}
12+
13+
resource "azurerm_mssql_database" "this" {
14+
name = var.database_name
15+
server_id = azurerm_mssql_server.this.id
16+
sku_name = var.sku_name
17+
max_size_gb = var.max_size_gb
18+
19+
tags = var.tags
20+
}
21+
22+
resource "azurerm_mssql_firewall_rule" "allow_azure_services" {
23+
name = "AllowAzureServices"
24+
server_id = azurerm_mssql_server.this.id
25+
start_ip_address = "0.0.0.0"
26+
end_ip_address = "0.0.0.0"
27+
}

outputs.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
output "server_id" {
2+
description = "ID of the SQL server"
3+
value = azurerm_mssql_server.this.id
4+
}
5+
6+
output "server_fqdn" {
7+
description = "Fully qualified domain name of the SQL server"
8+
value = azurerm_mssql_server.this.fully_qualified_domain_name
9+
}
10+
11+
output "database_id" {
12+
description = "ID of the SQL database"
13+
value = azurerm_mssql_database.this.id
14+
}
15+
16+
output "database_name" {
17+
description = "Name of the SQL database"
18+
value = azurerm_mssql_database.this.name
19+
}
20+
21+
output "connection_string" {
22+
description = "ADO.NET connection string"
23+
value = "Server=tcp:${azurerm_mssql_server.this.fully_qualified_domain_name},1433;Database=${azurerm_mssql_database.this.name};User ID=${var.admin_login};Password=${var.admin_password};Encrypt=true;Connection Timeout=30;"
24+
sensitive = true
25+
}

variables.tf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
variable "server_name" {
2+
description = "Name of the SQL server (must be globally unique)"
3+
type = string
4+
}
5+
6+
variable "database_name" {
7+
description = "Name of the SQL database"
8+
type = string
9+
default = "workshopdb"
10+
}
11+
12+
variable "resource_group_name" {
13+
description = "Name of the resource group"
14+
type = string
15+
default = "se-rg"
16+
}
17+
18+
variable "location" {
19+
description = "Azure region"
20+
type = string
21+
default = "westus2"
22+
}
23+
24+
variable "admin_login" {
25+
description = "Administrator login for the SQL server"
26+
type = string
27+
}
28+
29+
variable "admin_password" {
30+
description = "Administrator password for the SQL server"
31+
type = string
32+
sensitive = true
33+
}
34+
35+
variable "sku_name" {
36+
description = "SKU name for the database (e.g., Basic, S0, S1, GP_S_Gen5_1)"
37+
type = string
38+
default = "Basic"
39+
}
40+
41+
variable "max_size_gb" {
42+
description = "Maximum size of the database in GB"
43+
type = number
44+
default = 2
45+
}
46+
47+
variable "tags" {
48+
description = "Tags to apply to resources"
49+
type = map(string)
50+
default = {}
51+
}

0 commit comments

Comments
 (0)