Intro

In this post we are going to deploy a SQL Server VM in Azure. Deploying a VM requires a little more work than the SQL Database we deployed in the last post. We will need to add some networking resources and configure the host VM. 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.

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.

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-vwm
  3. Create new files named main.tf and networking.tf

The Provider & Resource Group

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
  4. Our resource group will be named rg-terraform
  5. I have set the Azure region for this to East US 2. You can set it to whatever region you prefer.

Networking

To stay focused on the details of SQL Server I am not going to deep dive into the networking resources required. See the networking.tf file in the repo to get a copy of all the resources.

One thing I will call out is the network rules. For demonstration purposes I have the source IP set to *. You will want to change this to your IP address, otherwise anyone will be able to connect to your resources.

VM

Let’s break down the VM code.

  1. Giving it a name, rg-terraform
  2. This is a reference to our resource groups name
  3. This is a reference to our resource groups Azure region
  4. Here we get to specify the type and size of the VM. I chose a fairly small VM to keep things cheap in our example. You can find all the available types and sizes on Microsoft’s site, or you can use this command to output a list for you right in PowerShell. az vm list-sizes --location "East US 2" --output table
  5. Enter an admin username and password for your VM. I left a generic set of credentials there for you to use if you want. I would change them if you are doing anything outside of experimenting
  6. This is a reference to our network interface card id. The resource was created in our networking.tf file
  7. Here we configure the cheapest options for our OS disk on the VM
  8. Last, we specify the operating system and version of SQL Server we want. To get the options I wanted I used the CLI commands to narrow it down. The third option SKU specifies the SQL Server license which can have a big difference in cost, choose wisely.
    az vm image list-skus --location "East US 2" --publisher MicrosoftSQLServer --offer "sql2022-ws2022" --output table

Wrapping Up

Deployment of a VM can take a bit longer than the PASS offerings, this is expected. Once Terraform is done deploying the resources they will take a few minutes to finish initializing.

A note on destroying these resources. I had to run destroy 3 times to remove all of the deployed resources. Seems there is a timeout on removing some resources that have a dependency.