|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create an Azure SQL Database server and database using Terraform' |
| 3 | +description: In this article, you create an Azure SQL Database server and database using Terraform. |
| 4 | +ms.topic: quickstart |
| 5 | +ms.service: sql-database |
| 6 | +ms.subservice: deployment-configuration |
| 7 | +ms.custom: devx-track-terraform, ai-gen-docs |
| 8 | +author: TomArcherMsft |
| 9 | +ms.author: tarcher |
| 10 | +ms.date: 5/03/2023 |
| 11 | +--- |
| 12 | + |
| 13 | +# Quickstart: Create an Azure SQL Database server and database using Terraform |
| 14 | + |
| 15 | +Creating a [single database](single-database-overview.md) is the quickest and simplest option to create a database in Azure SQL Database. This quickstart shows you how to create a single database using Terraform. |
| 16 | + |
| 17 | +[!INCLUDE [Terraform abstract](~/../azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 18 | + |
| 19 | +In this article, you learn how to: |
| 20 | + |
| 21 | +> [!div class="checklist"] |
| 22 | +> * Create a random value for the Azure resource group name using [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet). |
| 23 | +> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group). |
| 24 | +> * Create a random value for the [logical server in Azure](logical-servers.md) using [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet). |
| 25 | +> * Create a random password for the [logical server in Azure](logical-servers.md) using [random_password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password). |
| 26 | +> * Create a [logical server in Azure](logical-servers.md) using [azurerm_mssql_server](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mssql_server). |
| 27 | +> * Create a database in Azure SQL Database using [azurerm_mssql_database](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mssql_database). |
| 28 | +
|
| 29 | +[!INCLUDE [AI attribution](~/../azure/includes/ai-generated-attribution.md)] |
| 30 | + |
| 31 | +## Prerequisites |
| 32 | + |
| 33 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure) |
| 34 | + |
| 35 | +## Implement the Terraform code |
| 36 | + |
| 37 | +> [!NOTE] |
| 38 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-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//101-sql-database/TestRecord.md). |
| 39 | +> |
| 40 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform) |
| 41 | +
|
| 42 | +1. Create a directory in which to test and run the sample Terraform code and make it the current directory. |
| 43 | + |
| 44 | +1. Create a file named `providers.tf` and insert the following code: |
| 45 | + |
| 46 | + [!code-terraform[master](~/../terraform_samples/quickstart/101-sql-database//providers.tf)] |
| 47 | + |
| 48 | +1. Create a file named `main.tf` and insert the following code: |
| 49 | + |
| 50 | + [!code-terraform[master](~/../terraform_samples/quickstart/101-sql-database//main.tf)] |
| 51 | + |
| 52 | +1. Create a file named `variables.tf` and insert the following code: |
| 53 | + |
| 54 | + [!code-terraform[master](~/../terraform_samples/quickstart/101-sql-database//variables.tf)] |
| 55 | + |
| 56 | +1. Create a file named `outputs.tf` and insert the following code: |
| 57 | + |
| 58 | + [!code-terraform[master](~/../terraform_samples/quickstart/101-sql-database//outputs.tf)] |
| 59 | + |
| 60 | +## Initialize Terraform |
| 61 | + |
| 62 | +[!INCLUDE [terraform-init.md](~/../azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 63 | + |
| 64 | +## Create a Terraform execution plan |
| 65 | + |
| 66 | +[!INCLUDE [terraform-plan.md](~/../azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 67 | + |
| 68 | +## Apply a Terraform execution plan |
| 69 | + |
| 70 | +[!INCLUDE [terraform-apply-plan.md](~/../azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 71 | + |
| 72 | +## Verify the results |
| 73 | + |
| 74 | +#### [Azure CLI](#tab/azure-cli) |
| 75 | + |
| 76 | +1. Get the Azure resource group name. |
| 77 | + |
| 78 | + ```console |
| 79 | + resource_group_name=$(terraform output -raw resource_group_name) |
| 80 | + ``` |
| 81 | + |
| 82 | +1. Get the new logical server name. |
| 83 | + |
| 84 | + ```console |
| 85 | + sql_server_name=$(terraform output -raw sql_server_name) |
| 86 | + ``` |
| 87 | + |
| 88 | +1. Run [az sql db list](http://cli/azure/sql/db#az-sql-db-list) to display the names of all the databases in your server. |
| 89 | + |
| 90 | + ```azurecli |
| 91 | + az sql db list \ |
| 92 | + --resource-group $resource_group_name \ |
| 93 | + --server $sql_server_name \ |
| 94 | + --output table |
| 95 | + ``` |
| 96 | + |
| 97 | +#### [Azure PowerShell](#tab/azure-powershell) |
| 98 | + |
| 99 | +1. Get the Azure resource group name. |
| 100 | + |
| 101 | + ```console |
| 102 | + $resource_group_name=$(terraform output -raw resource_group_name) |
| 103 | + ``` |
| 104 | + |
| 105 | +1. Get the new logical server name. |
| 106 | + |
| 107 | + ```console |
| 108 | + $sql_server_name=$(terraform output -raw sql_server_name) |
| 109 | + ``` |
| 110 | + |
| 111 | +1. Run [Get-AzSqlDatabase](/powershell/module/az.sql/get-azsqldatabase) to display the names of all the databases in your server. |
| 112 | + |
| 113 | + ```azurepowershell |
| 114 | + Get-AzSqlDatabase -ResourceGroupName $resource_group_name ` |
| 115 | + -ServerName $sql_server_name ` |
| 116 | + | Format-Table |
| 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 | +> [Create a server-level firewall rule](firewall-create-server-level-portal-quickstart.md) |
0 commit comments