Skip to content

Commit ad42d64

Browse files
author
neil-yechenwei
committed
Add example for PostgreSQL Flexible Server Database
1 parent 48a25a1 commit ad42d64

File tree

5 files changed

+308
-0
lines changed

5 files changed

+308
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
terraform {
2+
required_version = ">=1.0"
3+
4+
required_providers {
5+
azurerm = {
6+
source = "hashicorp/azurerm"
7+
version = "=2.95.0"
8+
}
9+
}
10+
}
11+
12+
provider "azurerm" {
13+
features {}
14+
}
15+
16+
resource "azurerm_resource_group" "default" {
17+
name = "${var.name}-${var.environment}-rg"
18+
location = var.location
19+
}
20+
21+
resource "azurerm_virtual_network" "default" {
22+
name = "${var.name}-${var.environment}-vnet"
23+
location = azurerm_resource_group.default.location
24+
resource_group_name = azurerm_resource_group.default.name
25+
address_space = ["10.0.0.0/16"]
26+
}
27+
28+
resource "azurerm_network_security_group" "default" {
29+
name = "${var.name}-${var.environment}-nsg"
30+
location = azurerm_resource_group.default.location
31+
resource_group_name = azurerm_resource_group.default.name
32+
33+
security_rule {
34+
name = "test123"
35+
priority = 100
36+
direction = "Inbound"
37+
access = "Allow"
38+
protocol = "Tcp"
39+
source_port_range = "*"
40+
destination_port_range = "*"
41+
source_address_prefix = "*"
42+
destination_address_prefix = "*"
43+
}
44+
}
45+
46+
resource "azurerm_subnet" "default" {
47+
name = "${var.name}-${var.environment}-subnet"
48+
virtual_network_name = azurerm_virtual_network.default.name
49+
resource_group_name = azurerm_resource_group.default.name
50+
address_prefixes = ["10.0.2.0/24"]
51+
service_endpoints = ["Microsoft.Storage"]
52+
53+
delegation {
54+
name = "fs"
55+
56+
service_delegation {
57+
name = "Microsoft.DBforPostgreSQL/flexibleServers"
58+
59+
actions = [
60+
"Microsoft.Network/virtualNetworks/subnets/join/action",
61+
]
62+
}
63+
}
64+
}
65+
66+
resource "azurerm_subnet_network_security_group_association" "default" {
67+
subnet_id = azurerm_subnet.default.id
68+
network_security_group_id = azurerm_network_security_group.default.id
69+
}
70+
71+
resource "azurerm_private_dns_zone" "default" {
72+
name = "${var.name}-${var.environment}-pdz.postgres.database.azure.com"
73+
resource_group_name = azurerm_resource_group.default.name
74+
75+
depends_on = [azurerm_subnet_network_security_group_association.default]
76+
}
77+
78+
resource "azurerm_private_dns_zone_virtual_network_link" "default" {
79+
name = "${var.name}-${var.environment}-pdzvnetlink.com"
80+
private_dns_zone_name = azurerm_private_dns_zone.default.name
81+
virtual_network_id = azurerm_virtual_network.default.id
82+
resource_group_name = azurerm_resource_group.default.name
83+
}
84+
85+
resource "azurerm_postgresql_flexible_server" "default" {
86+
name = "${var.name}-${var.environment}-server"
87+
resource_group_name = azurerm_resource_group.default.name
88+
location = azurerm_resource_group.default.location
89+
version = "13"
90+
delegated_subnet_id = azurerm_subnet.default.id
91+
private_dns_zone_id = azurerm_private_dns_zone.default.id
92+
administrator_login = "adminTerraform"
93+
administrator_password = "QAZwsx123"
94+
zone = "1"
95+
storage_mb = 32768
96+
sku_name = "GP_Standard_D2s_v3"
97+
backup_retention_days = 7
98+
99+
depends_on = [azurerm_private_dns_zone_virtual_network_link.default]
100+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resource "azurerm_postgresql_flexible_server_database" "default" {
2+
name = "${var.name}-${var.environment}-db"
3+
server_id = azurerm_postgresql_flexible_server.default.id
4+
collation = "en_US.UTF8"
5+
charset = "UTF8"
6+
}
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# Azure PostgreSQL Flexible Server Database
2+
3+
This template deploys an [Azure PostgreSQL Flexible Server Database](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_flexible_server_database).
4+
5+
## Resources
6+
7+
| Terraform Resource Type | Description |
8+
| - | - |
9+
| `azurerm_resource_group` | The resource group all resources are deployed into |
10+
| `azurerm_virtual_network` | Manages the Azure Virtual Network including any configured subnets |
11+
| `azurerm_network_security_group` | Manages the Azure Network Security Group that contains a list of network security rules |
12+
| `azurerm_subnet` | Manages the Azure Subnet |
13+
| `azurerm_subnet_network_security_group_association` | Associates an Azure Network Security Group with an Azure Subnet within an Azure Virtual Network |
14+
| `azurerm_private_dns_zone` | Manages Azure Private DNS zones within Azure DNS |
15+
| `azurerm_private_dns_zone_virtual_network_link` | Manages Private DNS zone Virtual Network Links |
16+
| `azurerm_postgresql_flexible_server` | The Azure PostgreSQL Flexible Server that the Azure PostgreSQL Flexible Server Database will run on |
17+
| `azurerm_postgresql_flexible_server_database` | The Azure PostgreSQL Flexible Server Database |
18+
19+
## Variables
20+
21+
| Name | Description |
22+
|-|-|
23+
| `name` | Name of the deployment |
24+
| `environment` | The depolyment environment name (used for postfixing resource names) |
25+
| `location` | The Azure Region to deploy these resources in |
26+
27+
28+
## Example
29+
30+
```bash
31+
>terraform plan
32+
33+
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
34+
+ create
35+
36+
Terraform will perform the following actions:
37+
38+
# azurerm_network_security_group.default will be created
39+
+ resource "azurerm_network_security_group" "default" {
40+
+ id = (known after apply)
41+
+ location = "westeurope"
42+
+ name = "demo-postgresql-fs-dev-nsg"
43+
+ resource_group_name = "demo-postgresql-fs-dev-rg"
44+
+ security_rule = [
45+
+ {
46+
+ access = "Allow"
47+
+ description = ""
48+
+ destination_address_prefix = "*"
49+
+ destination_address_prefixes = []
50+
+ destination_application_security_group_ids = []
51+
+ destination_port_range = "*"
52+
+ destination_port_ranges = []
53+
+ direction = "Inbound"
54+
+ name = "test123"
55+
+ priority = 100
56+
+ protocol = "Tcp"
57+
+ source_address_prefix = "*"
58+
+ source_address_prefixes = []
59+
+ source_application_security_group_ids = []
60+
+ source_port_range = "*"
61+
+ source_port_ranges = []
62+
},
63+
]
64+
}
65+
66+
# azurerm_postgresql_flexible_server.default will be created
67+
+ resource "azurerm_postgresql_flexible_server" "default" {
68+
+ administrator_login = "adminTerraform"
69+
+ administrator_password = (sensitive value)
70+
+ backup_retention_days = 7
71+
+ cmk_enabled = (known after apply)
72+
+ delegated_subnet_id = (known after apply)
73+
+ fqdn = (known after apply)
74+
+ geo_redundant_backup_enabled = false
75+
+ id = (known after apply)
76+
+ location = "westeurope"
77+
+ name = "demo-postgresql-fs-dev-server"
78+
+ private_dns_zone_id = (known after apply)
79+
+ public_network_access_enabled = (known after apply)
80+
+ resource_group_name = "demo-postgresql-fs-dev-rg"
81+
+ sku_name = "GP_Standard_D2s_v3"
82+
+ storage_mb = 32768
83+
+ version = "13"
84+
+ zone = "1"
85+
}
86+
87+
# azurerm_postgresql_flexible_server_database.default will be created
88+
+ resource "azurerm_postgresql_flexible_server_database" "default" {
89+
+ charset = "UTF8"
90+
+ collation = "en_US.UTF8"
91+
+ id = (known after apply)
92+
+ name = "demo-postgresql-fs-dev-db"
93+
+ server_id = (known after apply)
94+
}
95+
96+
# azurerm_private_dns_zone.default will be created
97+
+ resource "azurerm_private_dns_zone" "default" {
98+
+ id = (known after apply)
99+
+ max_number_of_record_sets = (known after apply)
100+
+ max_number_of_virtual_network_links = (known after apply)
101+
+ max_number_of_virtual_network_links_with_registration = (known after apply)
102+
+ name = "demo-postgresql-fs-dev-pdz.postgres.database.azure.com"
103+
+ number_of_record_sets = (known after apply)
104+
+ resource_group_name = "demo-postgresql-fs-dev-rg"
105+
106+
+ soa_record {
107+
+ email = (known after apply)
108+
+ expire_time = (known after apply)
109+
+ fqdn = (known after apply)
110+
+ host_name = (known after apply)
111+
+ minimum_ttl = (known after apply)
112+
+ refresh_time = (known after apply)
113+
+ retry_time = (known after apply)
114+
+ serial_number = (known after apply)
115+
+ tags = (known after apply)
116+
+ ttl = (known after apply)
117+
}
118+
}
119+
120+
# azurerm_private_dns_zone_virtual_network_link.default will be created
121+
+ resource "azurerm_private_dns_zone_virtual_network_link" "default" {
122+
+ id = (known after apply)
123+
+ name = "demo-postgresql-fs-dev-pdzvnetlink.com"
124+
+ private_dns_zone_name = "demo-postgresql-fs-dev-pdz.postgres.database.azure.com"
125+
+ registration_enabled = false
126+
+ resource_group_name = "demo-postgresql-fs-dev-rg"
127+
+ virtual_network_id = (known after apply)
128+
}
129+
130+
# azurerm_resource_group.default will be created
131+
+ resource "azurerm_resource_group" "default" {
132+
+ id = (known after apply)
133+
+ location = "westeurope"
134+
+ name = "demo-postgresql-fs-dev-rg"
135+
}
136+
137+
# azurerm_subnet.default will be created
138+
+ resource "azurerm_subnet" "default" {
139+
+ address_prefix = (known after apply)
140+
+ address_prefixes = [
141+
+ "10.0.2.0/24",
142+
]
143+
+ enforce_private_link_endpoint_network_policies = false
144+
+ enforce_private_link_service_network_policies = false
145+
+ id = (known after apply)
146+
+ name = "demo-postgresql-fs-dev-subnet"
147+
+ resource_group_name = "demo-postgresql-fs-dev-rg"
148+
+ service_endpoints = [
149+
+ "Microsoft.Storage",
150+
]
151+
+ virtual_network_name = "demo-postgresql-fs-dev-vnet"
152+
153+
+ delegation {
154+
+ name = "fs"
155+
156+
+ service_delegation {
157+
+ actions = [
158+
+ "Microsoft.Network/virtualNetworks/subnets/join/action",
159+
]
160+
+ name = "Microsoft.DBforPostgreSQL/flexibleServers"
161+
}
162+
}
163+
}
164+
165+
# azurerm_subnet_network_security_group_association.default will be created
166+
+ resource "azurerm_subnet_network_security_group_association" "default" {
167+
+ id = (known after apply)
168+
+ network_security_group_id = (known after apply)
169+
+ subnet_id = (known after apply)
170+
}
171+
172+
# azurerm_virtual_network.default will be created
173+
+ resource "azurerm_virtual_network" "default" {
174+
+ address_space = [
175+
+ "10.0.0.0/16",
176+
]
177+
+ dns_servers = (known after apply)
178+
+ guid = (known after apply)
179+
+ id = (known after apply)
180+
+ location = "westeurope"
181+
+ name = "demo-postgresql-fs-dev-vnet"
182+
+ resource_group_name = "demo-postgresql-fs-dev-rg"
183+
+ subnet = (known after apply)
184+
+ vm_protection_enabled = false
185+
}
186+
187+
Plan: 9 to add, 0 to change, 0 to destroy.
188+
189+
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
190+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
variable "environment" {
2+
default = "dev"
3+
}
4+
5+
variable "name" {
6+
default = "demo-postgresql-fs"
7+
}
8+
9+
variable "location" {
10+
default = "West Europe"
11+
}

quickstart/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct](https://ope
3131
- [Azure Kubernetes Service with ACR](./201-aks-acr-identity/)
3232
- [Azure virtual machine scale set with jumpbox](./201-vmss-jumpbox)
3333
- [Azure virtual machine scale set with jumpbox from Packer custom image](./201-vmss-packer-jumpbox)
34+
- [Azure PostgreSQL Flexible Server Database](./201-postgresql-fs-db)
3435

3536
#### Advanced
3637
- [Azure Service Fabric](./301-service-fabric/)

0 commit comments

Comments
 (0)