Setup Azure SQL Database Using Terraform: A Step-By-Step Guide
Step 1: Set Up Terraform
- Download the Terraform zip from the installation page of the Terraform website.
- Extract and paste the terraform folder to the required location and add the path to runnable in environment variables.
- For MacOs install the terraform using HomeBrew.
Step 2: Set Up Azure CLI
- Download the Azure CLI setup from the official website.
- Run the installer and follow the steps to install.
- For MacOs install the Azure CLI using below HomeBrew Command.
brew update && brew install azure-cli
Step 3: Configure Azure CLI
- Open terminal and run below command.
az login
- A browser window will open for login. Login with your azure credentials. Once it is done you will see output as below.
- As we can see output is printed with details of logged in user and azure details.
Step 4: Create Terraform Code
- Goto your project folder and create main.tf file.
- Add terraform block to code with azure as required provider with latest version. You can find the latest version at hashicorp registry.
- Terraform block should look like below. You can add required version to avoid invalidation.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}
- Now add provider as azurerm like below. Specify other details as required.
provider "azurerm" {
features {}
}
- To create a database first we need to create a SQL server which can be used for creating multiple databases. Add configuration for server .Make sure you give unique name to server.
resource "azurerm_sql_server" "deepslabtestsqlserver" {
name = "deepslabtestsqlserver"
resource_group_name = "DeepsLab"
location = "eastus"
version = "12.0"
administrator_login = "dbmasteradmin"
administrator_login_password = "DBmasterrootadmin@21"
tags = {
"environment"= "test"
}
}
- Now add configuration for service account which is required for accessing Database. Again the name should be unique across Azure.
resource "azurerm_storage_account" "deepslabteststorageac" {
name = "deepslabteststorageac"
resource_group_name = "DeepsLab"
location = "eastus"
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_sql_database" "deepslabtestdb" {
name = "deepslabtestdb"
resource_group_name = "DeepsLab"
location = "eastus"
depends_on = [ azurerm_storage_account.deepslabteststorageac ]
server_name = azurerm_sql_server.deepslabtestsqlserver.name
tags = {
"environment"= "test"
}
}
- We have specified name, resource group and location for SQL Database. The depends_on states that storage account creation is required before provisioning database.
- As you can see we have specified server name where the database should be created.
- The complete code will look like below.
terraform {
required_providers {
azurerm= {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_sql_server" "deepslabtestsqlserver" {
name = "deepslabtestsqlserver"
resource_group_name = "DeepsLab"
location = "eastus"
version = "12.0"
administrator_login = "dbmasteradmin"
administrator_login_password = "DBmasterrootadmin@21"
tags = {
"environment"= "test"
}
}
resource "azurerm_storage_account" "deepslabteststorageac" {
name = "deepslabteststorageac"
resource_group_name = "DeepsLab"
location = "eastus"
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_sql_database" "deepslabtestdb" {
name = "deepslabtestdb"
resource_group_name = "DeepsLab"
location = "eastus"
depends_on = [ azurerm_storage_account.deepslabteststorageac ]
server_name = azurerm_sql_server.deepslabtestsqlserver.name
tags = {
"environment"= "test"
}
}
Step 5: Apply The Terraform Code
- Once the code is ready you can apply it.
- First init the terraform by running below command in project folder where main.tf is present.
terraform init
- Terraform is initialized as above. It will install terraform provider and create lock files for configuration.
- After successful output of terraform apply the changes using below command.
terrraform apply
- Terraform apply will display the details of infrastructure to be provisioned as below .After verifying type “yes” to confirm and apply.
- Terraform will start creating resources. It will print id and creation complete status as below .
- You can also verify deployment by visiting Virtual Networks page of Azure.
- The Storage account has deployed successfully in Azure portal
- The SQL Server has deployed on Azure portal
- The SQL Database has Deployed on Azure Portal
How to Create Azure SQL Data Base using Terraform
Azure SQL database is a managed database service in Azure. It allows the storage of data in an organized and safe manner in Azure Cloud. Azure SQL database is highly scalable and flexible as compared to other databases. In this article let’s see how we can set up Azure SQL Database using Terraform.