|
| 1 | +--- |
| 2 | +title: Create virtual endpoints for read replicas with Terraform |
| 3 | +description: This article describes the virtual endpoints for read replica feature using Terraform for Azure Database for PostgreSQL - Flexible Server. |
| 4 | +author: akashraokm |
| 5 | +ms.author: akashrao |
| 6 | +ms.reviewer: maghan |
| 7 | +ms.date: 08/08/2024 |
| 8 | +ms.service: azure-database-postgresql |
| 9 | +ms.subservice: flexible-server |
| 10 | +ms.topic: how-to |
| 11 | +ai.usage: ai-assisted |
| 12 | +--- |
| 13 | + |
| 14 | +# Create virtual endpoints for read replicas with Terraform |
| 15 | + |
| 16 | +Using Terraform, you can create and manage virtual endpoints for read replicas in Azure Database for PostgreSQL—Flexible Server. Terraform is an open-source infrastructure-as-code tool that allows you to define and provision infrastructure using a high-level configuration language. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +Before you begin, ensure you have the following: |
| 21 | + |
| 22 | +- An Azure account with the necessary permissions. |
| 23 | +- Terraform installed on your local machine. You can download it from the [official Terraform website](https://www.terraform.io/downloads.html). |
| 24 | +- Azure CLI installed and authenticated. Instructions are in the [Azure CLI documentation](/cli/azure/install-azure-cli). |
| 25 | + |
| 26 | +Ensure you have a basic understanding of Terraform syntax and Azure resource provisioning. |
| 27 | + |
| 28 | +Step-by-Step Terraform Configuration: Provide a step-by-step guide on configuring virtual endpoints for read replicas using Terraform. |
| 29 | + |
| 30 | +## Configuring virtual endpoints |
| 31 | + |
| 32 | +Follow these steps to create virtual endpoints for read replicas in Azure Database for PostgreSQL - Flexible Server: |
| 33 | + |
| 34 | +### Initialize the Terraform configuration |
| 35 | + |
| 36 | + Create a `main.tf` file and define the Azure provider. |
| 37 | + |
| 38 | + ```terraform |
| 39 | + provider "azurerm" { |
| 40 | + features {} |
| 41 | + } |
| 42 | +
|
| 43 | + resource "azurerm_resource_group" "example" { |
| 44 | + name = "example-resources" |
| 45 | + location = "East US" |
| 46 | + } |
| 47 | + ``` |
| 48 | + |
| 49 | +### Create the primary Azure Database for PostgreSQL |
| 50 | + |
| 51 | +Define the primary PostgreSQL server resource. |
| 52 | + |
| 53 | +```terraform |
| 54 | +resource "azurerm_postgresql_flexible_server" "primary" { |
| 55 | + name = "primary-server" |
| 56 | + resource_group_name = azurerm_resource_group.example.name |
| 57 | + location = azurerm_resource_group.example.location |
| 58 | + version = "12" |
| 59 | + administrator_login = "adminuser" |
| 60 | + administrator_password = "password" |
| 61 | + sku_name = "Standard_D4s_v3" |
| 62 | +
|
| 63 | + storage_mb = 32768 |
| 64 | + backup_retention_days = 7 |
| 65 | + geo_redundant_backup = "Disabled" |
| 66 | + high_availability { |
| 67 | + mode = "ZoneRedundant" |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Create read replicas |
| 73 | + |
| 74 | +Define the read replicas for the primary server. |
| 75 | + |
| 76 | +```terraform |
| 77 | +resource "azurerm_postgresql_flexible_server_replica" "replica" { |
| 78 | + name = "replica-server" |
| 79 | + resource_group_name = azurerm_resource_group.example.name |
| 80 | + location = azurerm_resource_group.example.location |
| 81 | + source_server_id = azurerm_postgresql_flexible_server.primary.id |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### Configure virtual endpoints |
| 86 | + |
| 87 | +Define the necessary resources to configure virtual endpoints. |
| 88 | + |
| 89 | +```terraform |
| 90 | +resource "azurerm_private_endpoint" "example" { |
| 91 | + name = "example-private-endpoint" |
| 92 | + location = azurerm_resource_group.example.location |
| 93 | + resource_group_name = azurerm_resource_group.example.name |
| 94 | + subnet_id = azurerm_subnet.example.id |
| 95 | +
|
| 96 | + private_service_connection { |
| 97 | + name = "example-privateserviceconnection" |
| 98 | + private_connection_resource_id = azurerm_postgresql_flexible_server.primary.id |
| 99 | + is_manual_connection = false |
| 100 | + subresource_names = ["postgresqlServer"] |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### Apply the configuration |
| 106 | + |
| 107 | +Initialize Terraform and apply the configuration. |
| 108 | + |
| 109 | +`terraform init` |
| 110 | +`terraform apply` |
| 111 | + |
| 112 | +Confirm the apply action when prompted. Terraform provisions the resources and configure the virtual endpoints as specified. |
| 113 | + |
| 114 | + |
| 115 | +For additional info about Virtual endpoints, refer to [create virtual endpoints](how-to-read-replicas-portal.md#create-virtual-endpoints) |
| 116 | + |
| 117 | +## Related content |
| 118 | + |
| 119 | +- [Create virtual endpoints](how-to-read-replicas-portal.md#create-virtual-endpoints) |
| 120 | +- [Read replicas - overview](concepts-read-replicas.md) |
| 121 | +- [Geo-replication](concepts-read-replicas-geo.md) |
| 122 | +- [Promote read replicas](concepts-read-replicas-promote.md) |
| 123 | +- [Create and manage read replicas in the Azure portal](how-to-read-replicas-portal.md) |
| 124 | +- [Cross-region replication with virtual network](concepts-networking.md#replication-across-azure-regions-and-virtual-networks-with-private-networking) |
| 125 | +- [Terraform Azure provider documentation](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs) |
0 commit comments