|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create a private endpoint - Terraform' |
| 3 | +description: In this quickstart, you learn how to create a private endpoint using Terraform. |
| 4 | +ms.topic: quickstart |
| 5 | +ms.date: 02/22/2024 |
| 6 | +ms.custom: devx-track-terraform |
| 7 | +ms.service: virtual-network |
| 8 | +author: asudbring |
| 9 | +ms.author: allensu |
| 10 | +content_well_notification: |
| 11 | + - AI-contribution |
| 12 | +#Customer intent: As someone who has a basic network background but is new to Azure, I want to create a private endpoint by using Terraform. |
| 13 | +--- |
| 14 | + |
| 15 | +# Quickstart: Create a private endpoint by using Terraform |
| 16 | + |
| 17 | +In this quickstart, you use Terraform to create a private endpoint. The private endpoint connects to an Azure SQL Database. The private endpoint is associated with a virtual network and a private Domain Name System (DNS) zone. The private DNS zone resolves the private endpoint IP address. The virtual network contains a virtual machine that you use to test the connection of the private endpoint to the instance of the SQL Database. |
| 18 | + |
| 19 | +The script generates a random password for the SQL server and a random SSH key for the virtual machine. The names of the created resources are output when the script is run. |
| 20 | + |
| 21 | +[!INCLUDE [About Terraform](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +- You need an Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 26 | + |
| 27 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure). |
| 28 | + |
| 29 | +## Implement the Terraform code |
| 30 | + |
| 31 | +> [!NOTE] |
| 32 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/201-private-link-sql-database). You can view the log file containing the [test results from current and previous versions of Terraform](https://github.com/Azure/terraform/tree/master/quickstart/201-private-link-sql-database/TestRecord.md). |
| 33 | +> |
| 34 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform) |
| 35 | +
|
| 36 | +1. Create a directory in which to test and run the sample Terraform code and make it the current directory. |
| 37 | + |
| 38 | +1. Create a file named `main.tf` and insert the following code: |
| 39 | + |
| 40 | + :::code language="Terraform" source="~/terraform_samples/quickstart/201-private-link-sql-database/main.tf"::: |
| 41 | + |
| 42 | +1. Create a file named `outputs.tf` and insert the following code: |
| 43 | + |
| 44 | + :::code language="Terraform" source="~/terraform_samples/quickstart/201-private-link-sql-database/outputs.tf"::: |
| 45 | + |
| 46 | +1. Create a file named `provider.tf` and insert the following code: |
| 47 | + |
| 48 | + :::code language="Terraform" source="~/terraform_samples/quickstart/201-private-link-sql-database/provider.tf"::: |
| 49 | + |
| 50 | +1. Create a file named `ssh.tf` and insert the following code: |
| 51 | + |
| 52 | + :::code language="Terraform" source="~/terraform_samples/quickstart/201-private-link-sql-database/ssh.tf"::: |
| 53 | + |
| 54 | +1. Create a file named `variables.tf` and insert the following code: |
| 55 | + |
| 56 | + :::code language="Terraform" source="~/terraform_samples/quickstart/201-private-link-sql-database/variables.tf"::: |
| 57 | + |
| 58 | + |
| 59 | +## Initialize Terraform |
| 60 | + |
| 61 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 62 | + |
| 63 | +## Create a Terraform execution plan |
| 64 | + |
| 65 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 66 | + |
| 67 | +## Apply a Terraform execution plan |
| 68 | + |
| 69 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 70 | + |
| 71 | +## Verify the results |
| 72 | + |
| 73 | +#### [Azure CLI](#tab/azure-cli) |
| 74 | + |
| 75 | +1. Get the Azure resource group name. |
| 76 | + |
| 77 | + ```console |
| 78 | + resource_group_name=$(terraform output -raw resource_group_name) |
| 79 | + ``` |
| 80 | + |
| 81 | +1. Get the SQL Server name. |
| 82 | + |
| 83 | + ```console |
| 84 | + sql_server=$(terraform output -raw sql_server) |
| 85 | + ``` |
| 86 | + |
| 87 | +1. Run [az sql server show](/cli/azure/sql/server#az-sql-server-show) to display the details about the SQL Server private endpoint. |
| 88 | + |
| 89 | + ```azurecli |
| 90 | + az sql server show \ |
| 91 | + --resource-group $resource_group_name \ |
| 92 | + --name $sql_server --query privateEndpointConnections \ |
| 93 | + --output tsv |
| 94 | + ``` |
| 95 | + |
| 96 | +#### [Azure PowerShell](#tab/azure-powershell) |
| 97 | + |
| 98 | +1. Get the Azure resource group name. |
| 99 | + |
| 100 | + ```console |
| 101 | + $resource_group_name=$(terraform output -raw resource_group_name) |
| 102 | + ``` |
| 103 | + |
| 104 | +1. Get the SQL Server name. |
| 105 | + |
| 106 | + ```console |
| 107 | + $sql_server=$(terraform output -raw sql_server_name) |
| 108 | + ``` |
| 109 | + |
| 110 | +1. Run [Get-AzPrivateEndpoint](/powershell/module/az.network/get-azprivateendpoint) to display the details about the SQL Server private endpoint. |
| 111 | + |
| 112 | + ```azurepowershell |
| 113 | + $sql = @{ |
| 114 | + ResourceGroupName = $resource_group_name |
| 115 | + } |
| 116 | + Get-AzPrivateEndpoint @sql |
| 117 | + ``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## Clean up resources |
| 122 | + |
| 123 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 124 | + |
| 125 | +## Troubleshoot Terraform on Azure |
| 126 | + |
| 127 | +[Troubleshoot common problems when using Terraform on Azure.](/azure/developer/terraform/troubleshoot) |
| 128 | + |
| 129 | +## Next steps |
| 130 | + |
| 131 | +> [!div class="nextstepaction"] |
| 132 | +> [Learn more about using Terraform in Azure](/azure/terraform) |
0 commit comments