Deploy Azure SQL Databases using Terraform.
Date created: December 27, 2020.
What is Terraform? Terraform is an open source “Infrastructure as Code” tool,
created by HashiCorp. A declarative coding tool, Terraform enables to use a
high-level configuration language called HCL (HashiCorp Configuration Language)
to deploy infrastructure to cloud and on-premises environments.
On this article, we show you how to deploy Azure SQL databases to Azure.
Here we assume you have already installed Terraform on your computer. if you
have not, you can visit
this
URL.
Let's start by creating a new directory on the computer disk drive using
PowerShell, and move into that directory with a CD command.
Paste the following Terraform configuration into a file and name it main.tf
inside the folder you just created previously.
# Configure the Azure provider
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 2.26"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "amorillo" {
name = "rgamorillo"
location = "East US"
}
resource "azurerm_storage_account" "amorillo" {
name = "amorillosa"
resource_group_name = azurerm_resource_group.amorillo.name
location = azurerm_resource_group.amorillo.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_mssql_server" "amorillo" {
name = "morillo"
resource_group_name = azurerm_resource_group.amorillo.name
location = azurerm_resource_group.amorillo.location
version = "12.0"
administrator_login = "amorillo"
administrator_login_password = "G>p3_v}'kJyC4}$}"
minimum_tls_version = "1.2"
tags = {
environment = "Production"
}
}
resource "azurerm_sql_database" "amorillo" {
name = "morilloazuresqldatabase"
resource_group_name = azurerm_resource_group.amorillo.name
location = "East US"
server_name = azurerm_mssql_server.amorillo.name
tags = {
environment = "production"
}
}
To deploy the configuration above, we need now to initialize the project, which
downloads a plugin that allows Terraform to interact with Azure.
Finally, we can deploy our infrastructure by running the following command.
This will create a plan with detailed information about how the deployment will
be done. Proceed to examine the plan.
When Terraform asks you to confirm the plan type yes and press ENTER.
After a couple of minutes you can see the deployment has finished.
You can verify the infrastructure has been deployed to Azure by examining the
resource group. In this case, the resource group is named rgamorillo.