Intro

In this post we are going to cover how you can use Terraform to create an Azure SQL Database. If you need help setting up Terraform please see my prior post, Terraform Azure: Setting Up Your Environment.This post assumes you have an Azure account. If you do not, you can create one for free.

We will take a slow approach to accomplishing our task. The goal here is to get familiar with Terraform and its commands by creating some basic resources. Terraform also has some great resources for getting started here.

As you go through the sections of this post you will see screenshots of the code with callouts that I will walk through. You can find all the code for this example on my GitHub repo.

Prerequisites

To create a SQL Database you will need a user to assign admin privileges to. To create a user follow these instructions. If you already have one then you are all set. What we will need from the user is its name and the object id.

Getting Started

  1. Fire up your favorite code editor, in this post I will be using VS Code
  2. Start a new folder for your project, mine is named sql-db
  3. Create new file named main.tf

The Provider

Up first is the Azure provider. You can find all the docs here. The code below will configure your project to pull the Azure provider and configure it.

  1. azurerm is the name of the Terraform Azure provider
  2. We are using version 3.0 or better
  3. This is required to configure the provider

Resource Group

Everything in Azure has to belong to a resource group, so that is the first resource we will create.

  1. Our resource group will be named rg-terraform
  2. I have set the Azure region for this to East US 2. You can set it to whatever region you prefer.

Server

In order to create a database we have to have a server for it to be a member of. 

  1. Our server will be named sql-server-terraform
  2. This is a reference to our resource groups name
  3. This is a reference to our resource groups Azure region
  4. We are setting the server to the latest available version
  5. Here is where we need the name of the admin account we made
  6. Paste in the object id from the admin account
  7. Another item to note here is the azuread_authentication_only property. This is set to true so it will not use SQL Server authentication or mixed mode authentication. To configure mixed mode checkout the example in the Terraform documentation.
  8. Tag the resource with development environment

Database

Finally, we have arrived at the database. 

  1. We will be naming this database db-terraform
  2. This is referencing the id of the server we created
  3. The most important configurable option here is the sku_name. This is the value the pricing of your database is based on. I have it configured to the cheapest possible option. To get a full list of all the available SKUs you can run this command in powershell az sql db list-editions --location “East US 2” --output table.
  4. Tag the resource with development environment

Authenticate

You now have all your resources defined and are ready to deploy them to Azure.

The first thing we need to do is authenticate to Azure so Terraform can connect.

  1. Open a PowerShell terminal in VS Code and run az login. This will open a browser window that asks you to login to your azure account.

Initialize

Next we need to initialize our Terraform project.

  1. Use the terraform init command to initialize your project and download all the providers needed
  2. This message shows Terraform has initialized successfully

Plan

Now we get to see what Terraform is going to do.

  1. Here we are running terraform plan to get a listing of the changes Terraform will make to our Azure environment
  2. The green + indicates an object will be created
  3. Anything marked as (known after apply) is usually an id of some kind that Azure will generate after the object is created.

Apply

After we have reviewed the plan and decide we like it we can deploy the changes.

  1. To deploy our changes we run the terraform apply command
  2. Terraform will output all of the changes it will make and stop to prompt you to make sure you want to make the changes
  3. To confirm the changes enter yes
  4. The output will have a listing of Terraform’s progress
  5. If everything went well you will have a green success message at the bottom

Validate

To confirm everything worked let’s check our Azure account.

  1. Go to resource groups
  2. Open the tg-terraform resource group
  3. You should see the server and database resources we defined

Destroy

So we don’t land ourselves with a large bill, let’s destroy these new resources.

  1. To destroy these resources we run the terraform destroy command
  2. The red – indicates an object will be destroyed
  3. Terraform will output all of the changes it will make and stop to prompt you to make sure you want to make the changes
  4. To confirm the changes enter yes
  5. If everything went well you will have a green success message at the bottom

To confirm everything is gone, head back to your Azure account and refresh the page. You should see all of the new resources are now gone.