11 – Azure SQL Database Server Terraform Mini Project — Step-by-Step Guide

In this hands-on tutorial, we will build a complete Azure SQL Server + SQL Database using Terraform, then securely connect to it from our local machine and run real SQL commands — without installing SSMS or Azure Data Studio.

This mini project is perfect if you are learning:

  • Terraform Infrastructure as Code
  • Azure SQL PaaS services
  • Networking security with firewall rules
  • Database connectivity using Azure CLI and sqlcmd

Let’s build everything step by step.

Table of Contents

  1. What We Will Build
  2. Step 1 – Create Resource Group, SQL Server and Database
  3. Step 2 – Add Firewall Rule to Allow Local PC
  4. Step 3 – Test SQL Using CLI (No GUI Needed)
  5. Step 4 – Connect to Database Using sqlcmd
  6. Step 5 – Create Table and Insert Data
  7. What We Learned

What We Will Build

By the end of this demo, we will have:

  • An Azure Resource Group
  • Azure SQL Server
  • Azure SQL Database
  • Firewall rule to allow our PC to connect
  • Real database table with data
  • Full connectivity test using CLI

Step 1 – Create Resource Group, SQL Server and Database

First we define the core infrastructure using Terraform.

Resource Group — rg.tf

resource "azurerm_resource_group" "rg" {
  name     = "rgminipro98989"
  location = "Central US"
}

The resource group is a logical container that will hold our SQL server and database.


SQL Server — sqlserver.tf

resource "azurerm_mssql_server" "sql_server" {
  name                         = "sqlserverminipro876811"
  resource_group_name          = azurerm_resource_group.rg.name
  location                     = azurerm_resource_group.rg.location
  version                      = "12.0"
  administrator_login          = "sqladmin"
  administrator_login_password = "StrongPassword@123"
}

This creates:

  • Azure SQL logical server
  • Admin user and password
  • Hosted in Central US

In real projects, never hardcode passwords — use Azure Key Vault or Terraform variables.


SQL Database — sqldb.tf

resource "azurerm_mssql_database" "sqldb" {
  name      = "sqldbminipro81829"
  server_id = azurerm_mssql_server.sql_server.id
}

This database is created inside the SQL server defined earlier.


Deploy Infrastructure

Run:

terraform init
terraform apply

After apply completes:

  • Open Azure Portal
  • Navigate to your resource group
  • Verify SQL Server and Database exist

Step 2 – Add Firewall Rule to Allow Local PC

By default, Azure SQL blocks all external connections.
We must allow our own IP address.

Firewall Rule — firewallrule.tf

resource "azurerm_mssql_firewall_rule" "firewall_rule" {
  name             = "sqlfirewallruleminipro909122"
  server_id        = azurerm_mssql_server.sql_server.id
  start_ip_address = ""
  end_ip_address   = ""
}

👉 Replace the empty IP values with your public IP.

You can find your IP from:

https://whatismyipaddress.com

Example:

start_ip_address = "203.0.113.10"
end_ip_address   = "203.0.113.10"

Apply again:

terraform apply

Step 3 – Test SQL Using CLI (No GUI Needed)

We will connect using:

  • Azure CLI
  • sqlcmd tool

List SQL Servers

az sql server list -o table

List Databases in Our Server

az sql db list --server sqlserverminipro876811 --resource-group rgminipro98989 -o table

Check Firewall Rules

az sql server firewall-rule list --server sqlserverminipro876811 --resource-group rgminipro98989 -o table

Step 4 – Connect to Database Using sqlcmd

No SSMS required!

Connect

sqlcmd -S sqlserverminipro876811.database.windows.net -U sqladmin -P "StrongPassword@123" -d sqldbminipro81829

IMPORTANT:
Use full DNS name →
sqlserverminipro876811.database.windows.net


Verify Databases

SELECT name FROM sys.databases;
GO

Every SQL command must end with:

GO

Step 5 – Create Table and Insert Data

Create Table

CREATE TABLE employees(
  id INT PRIMARY KEY,
  name VARCHAR(50),
  tech VARCHAR(30)
);
GO

Insert Sample Data

INSERT INTO employees VALUES
(1, 'Alice', 'Terraform'),
(2, 'Bob', 'Azure'),
(3, 'Charlie', 'SQL');
GO

Query Data

SELECT * FROM employees;
GO

🎉 You should see real output from Azure SQL Database!


What We Learned

In this mini project you successfully:

  • Provisioned Azure SQL using Terraform
  • Understood logical SQL server vs database
  • Configured network security via firewall
  • Connected securely from local PC
  • Executed real SQL queries using CLI

This is exactly how cloud engineers deploy database environments in real projects — automated, repeatable, and infrastructure as code.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

TechMilestoneHub

Build Skills, Unlock Milestones

This is a test – edited from front page