17
Nov

Terraform is an open source “Infrastructure as Code” tool, created by HashiCorp. Running a Terraform script will ‘build’ you a SQL Server in a few minutes and destroy it just as fast, so it is perfect for the purpose of standing up a SQL Server to test a theory or some code and then drop this server.

Summary of tasks

  • create main.tf & variables.tf
  • terraform init
  • terraform plan
  • terraform apply
  • test connection to new Windows SQL Server
  • terraform destroy

Quickstart:

We use Azure portal+the Cloud Shell (linux prompt) + Terraform to achieve our goals. This is just one way to do this – you are free to choose your own path in life :-).
https://docs.microsoft.com/en-us/azure/developer/terraform/get-started-cloud-shell

Browse to the Azure portal. and select the Cloud Shell option, see screenshot below

Accessing Cloud Shell

Create and apply a Terraform execution plan.

Create Terraform 2 files

Download the above files.


Lets look at these at a high level. These files contain definitions of what we want to be built and how.

The main.tf file is made of of 2 parts.
Part 1 creates the ‘bones’ of the environment

Resource TypeObjectLogical Name
Resource Groupazurerm_resource_groupexample_rg1
virtual networkazurerm_virtual_networkexample_vn
subnetazurerm_subnetexample_snet
public IPsazurerm_public_ipexample_ip
Network Security Groupazurerm_network_security_groupexample_secgrp

Part 2 creates the Windows Virtual Machine

Resource TypeObjectLogical Name
Virtual NICazurerm_network_interfaceexample_nic
Virtual Machineazurerm_virtual_machineexample_vm

Virtual Machine definition includes

  • storage_image_reference, which specifies the Image file to use when creating the Virtual Machine.
  • storage_os_disk, ususally the C:\ drive in Windows
  • storage_data_disk
  • Assign the admin uid/pwd and also computer name

Creating you first Virtual Machine, including SQL Server

Run the following commands in order. ‘init’ only needs running the first time. Runn all commands from the directory containing your *.tf files

terraform init
terraform plan
terraform apply

should look some thing like this

Connecting to your New VM

Refresh your list of Azure resources in the Azure Portal. Find the virtual machine you just created.

when you open the list you will see the IP address, which we will connect to over RDP in a second.

RDP Connection

Test the results of all your hard work by checking the new SQL Server is available

Well done. You’ve done it.

Destroy it

I know you are proud of all your hard work but once you have marvelled at how clever you are and used the SQL Server to test whatever SQL you created it for, it is time to DESTROY it. This drops all the resources and stops you paying for it!

Once the process is finished, refresh your list of resources in the Azure Portal and confirm that these have now disappeared.

Detailed look at how it works

So how did it know which version of SQL Server to install? the server name? sysadmin account and password?
The details are in the *.tf files.

The maint.tf contains the section storage_image_reference, which is used to define the details of the image files to start up our new Windows VM.

main.tf file

The values for this definition are resolved from the variables.tf file

variables.tf

Leave a Reply

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