Skip to content

Commit d33d108

Browse files
committed
feat(DO-779): Several little improvements and new features
1 parent b0434c0 commit d33d108

File tree

6 files changed

+91
-42
lines changed

6 files changed

+91
-42
lines changed

README.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ The following resources are used by this module:
3838
- [azurerm_mysql_firewall_rule.firewall](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mysql_firewall_rule) (resource)
3939
- [azurerm_mysql_server.server](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mysql_server) (resource)
4040
- [azurerm_mysql_virtual_network_rule.virtualnetworks](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mysql_virtual_network_rule) (resource)
41+
- [azurerm_private_endpoint.mysql-private-endpoint](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/private_endpoint) (resource)
4142

4243
## Required Inputs
4344

@@ -49,6 +50,19 @@ Description: Admin password
4950

5051
Type: `string`
5152

53+
### charset
54+
55+
Description: Charset for the databases, which needs to be a valid PostgreSQL charset
56+
57+
Type: `string`
58+
59+
### collation
60+
61+
Description: Collation for the databases, which needs to be a valid PostgreSQL collation. Note that Microsoft uses
62+
different notation - f.e. en-US instead of en\_US
63+
64+
Type: `string`
65+
5266
### database\_suffixes
5367

5468
Description: List of suffixes for databases to be created
@@ -94,7 +108,8 @@ Default: `"mysqladmin"`
94108
### allowed\_ips
95109

96110
Description: A hash of permissions to access the database server by ip. The hash key is the name suffix and each value
97-
has a start and an end value.
111+
has a start and an end value. For public access set start\_ip\_address to 0.0.0.0 and end\_ip\_address to
112+
255.255.255.255. This variable is not used if public\_access = false.
98113

99114
Type:
100115

@@ -141,27 +156,30 @@ Default: `"8.0"`
141156

142157
### public\_access
143158

144-
Description: Wether to allow public access to the database server
159+
Description: Wether to allow public access to the database server. True will create firewall rules for allowed\_ips and for subnets. False will
160+
create a private endpoint in each given subnet (allowed\_ips will not be used then) - you have to set
161+
enforce\_private\_link\_endpoint\_network\_policies = true on your subnet in this case (see
162+
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet#enforce_private_link_endpoint_network_policies).
145163

146164
Type: `bool`
147165

148166
Default: `false`
149167

150-
### suffix
168+
### subnets
151169

152-
Description: Naming suffix to allow multiple instances of this module
170+
Description: Maps of prefix => subnet id that has access to the server
153171

154-
Type: `string`
172+
Type: `map(string)`
155173

156-
Default: `""`
174+
Default: `{}`
157175

158-
### virtual\_networks
176+
### suffix
159177

160-
Description: Maps of prefix => virtual network id that has access to the server
178+
Description: Naming suffix to allow multiple instances of this module
161179

162-
Type: `map(string)`
180+
Type: `string`
163181

164-
Default: `{}`
182+
Default: `""`
165183

166184
## Outputs
167185

databases.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "azurerm_mysql_database" "db" {
2+
for_each = toset(var.database_suffixes)
3+
name = "${var.project}${var.stage}db${each.value}"
4+
resource_group_name = var.resource_group
5+
server_name = azurerm_mysql_server.server.name
6+
charset = var.charset
7+
collation = var.collation
8+
}

firewall.tf

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "azurerm_mysql_firewall_rule" "firewall" {
2-
for_each = var.allowed_ips
2+
for_each = var.public_access == true ? var.allowed_ips : {}
33
start_ip_address = each.value.start
44
end_ip_address = each.value.end
55
name = "${var.project}${var.stage}dbfw${each.key}"
@@ -8,9 +8,24 @@ resource "azurerm_mysql_firewall_rule" "firewall" {
88
}
99

1010
resource "azurerm_mysql_virtual_network_rule" "virtualnetworks" {
11-
for_each = var.subnets
11+
for_each = var.public_access == true ? var.subnets : {}
1212
name = "${var.project}${var.stage}dbfwnet${each.key}"
1313
resource_group_name = var.resource_group
1414
server_name = azurerm_mysql_server.server.name
1515
subnet_id = each.value
1616
}
17+
18+
resource "azurerm_private_endpoint" "mysql-private-endpoint" {
19+
for_each = var.public_access == false ? var.subnets : {}
20+
name = "${each.value}-mysql-${azurerm_mysql_server.server.id}-endpoint"
21+
location = var.location
22+
resource_group_name = var.resource_group
23+
subnet_id = each.value
24+
25+
private_service_connection {
26+
name = "${each.value}-mysql-${azurerm_mysql_server.server.id}-privateserviceconnection"
27+
private_connection_resource_id = azurerm_mysql_server.server.id
28+
subresource_names = ["mysqlServer"]
29+
is_manual_connection = false
30+
}
31+
}

main.tf

Lines changed: 0 additions & 28 deletions
This file was deleted.

server.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource "azurerm_mysql_server" "server" {
2+
name = "${var.project}${var.stage}dbsrv"
3+
location = var.location
4+
resource_group_name = var.resource_group
5+
administrator_login = var.admin_login
6+
administrator_login_password = var.admin_password
7+
sku_name = var.database_host_sku
8+
storage_mb = var.database_storage
9+
version = var.database_version
10+
backup_retention_days = var.backup_retention_days
11+
public_network_access_enabled = var.public_access
12+
13+
auto_grow_enabled = true
14+
geo_redundant_backup_enabled = false
15+
infrastructure_encryption_enabled = true
16+
ssl_enforcement_enabled = true
17+
}

vars.tf

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ variable "suffix" {
3535
default = ""
3636
}
3737

38+
variable "charset" {
39+
type = string
40+
description = "Charset for the databases, which needs to be a valid PostgreSQL charset"
41+
}
42+
43+
variable "collation" {
44+
type = string
45+
description = <<EOF
46+
Collation for the databases, which needs to be a valid PostgreSQL collation. Note that Microsoft uses
47+
different notation - f.e. en-US instead of en_US
48+
EOF
49+
}
50+
3851
variable "backup_retention_days" {
3952
type = number
4053
description = "Number of days to keep backups"
@@ -69,15 +82,21 @@ variable "database_storage" {
6982
}
7083

7184
variable "public_access" {
72-
description = "Wether to allow public access to the database server"
7385
type = bool
86+
description = <<EOF
87+
Wether to allow public access to the database server. True will create firewall rules for allowed_ips and for subnets. False will
88+
create a private endpoint in each given subnet (allowed_ips will not be used then) - you have to set
89+
enforce_private_link_endpoint_network_policies = true on your subnet in this case (see
90+
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet#enforce_private_link_endpoint_network_policies).
91+
EOF
7492
default = false
7593
}
7694

7795
variable "allowed_ips" {
7896
description = <<EOF
7997
A hash of permissions to access the database server by ip. The hash key is the name suffix and each value
80-
has a start and an end value.
98+
has a start and an end value. For public access set start_ip_address to 0.0.0.0 and end_ip_address to
99+
255.255.255.255. This variable is not used if public_access = false.
81100
EOF
82101
type = map(object({
83102
start = string,

0 commit comments

Comments
 (0)