Open In App

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.

What is Terraform?

Terraform is an infrastructure as a code tool used for provisioning cloud resources with the help of code. It manages the state of cloud infrastructure and allows its management flexibly. It makes updating, destroying and reprovisioning of resources easy and simple.



What is Azure SQL DB?

Azure SQL Database is a managed service provided by Microsoft Azure. It is a relational database service that allows storage of relational data in Azure Cloud. It provides features such as high availability, security, maintenance and scalability. Azure DB can be used for setting up multiple databases with different types of SQL DBMS engines.

Understanding Of Primary Terminologies Related to Azure SQL DB And Terraform

The following are the primary components of Azure Vnet related to Terraform:



Setup Azure SQL Database Using Terraform: A Step-By-Step Guide

Step 1: Set Up Terraform

Step 2: Set Up Azure CLI

brew update && brew install azure-cli

Step 3: Configure Azure CLI

az login

Step 4: Create Terraform Code

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"
}
}
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

terraform init

terrraform apply

Conclusion

We have successfully created Azure SQL Database with the help of terraform in this article. the configuration described can be further modified to make changes to database and SQL server. This is how terraform allows reusable and modifiable configuration of infrastructure.

How to create Azure SQL Database using Terraform – FAQ’s

Why use Terraform for creating Azure SQL Database?

Terraform simplifies and automates the process of provisioning infrastructure, including Azure SQL Database. By defining infrastructure as code, you can easily manage and version your database configuration, making it reproducible and consistent across environments.

What information do I need to provide in my Terraform configuration file?

In your Terraform configuration file, you’ll need to specify details such as the name of the database, resource group, server name, server admin login, and password. You can also configure additional settings like the database edition, pricing tier, and desired performance characteristics.

Can Terraform automatically manage the lifecycle of my Azure SQL Database?

Yes, Terraform can manage the lifecycle of your Azure SQL Database, including provisioning, updating, and destroying resources. This means you can easily make changes to your database configuration and apply them with Terraform, ensuring consistency and reliability.

Can I manage multiple Azure SQL Databases with Terraform?

Yes, Terraform supports managing multiple Azure SQL Databases within the same configuration file. You can define multiple database resources with different configurations and Terraform will manage them accordingly.

Where can I find examples and documentation for creating Azure SQL Database with Terraform?

The Terraform documentation and community resources provide numerous examples and guides for creating Azure SQL Database using Terraform. You can explore the official Terraform documentation, HashiCorp‘s tutorials, and community forums for more information and assistance.


Article Tags :