Intro

In this post we are going to build on the previous post Terraform Azure: SQL Database. We will cover how you can use Terraform to create multiple Azure SQL Databases with Terraform modules. We are going to turn the original Terraform into a module so we can create as many databases as we need with identical configurations. Another advantage here is maintenance, if a setting needs to be adjusted you can simply adjust the module and it will propagate to each resource using the module configuration.

Our goal is to create identical Azure SQL Databases on each coast of the United States. Using our module we will deploy 1 instance in region East US 2 and another in West US 2.

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.

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

Start off by creating a new project, I named mine sql_db_template. In your project add the following.

  1. Create a file named main.tf
  2. Create a new folder named modules
  3. Inside the modules folder create 2 folders, azure_sql_db and azure_sql_server
  4. Inside the azure_sql_db folder create 2 files, main.tf and variables.tf
  5. Inside the azure_sql_server folder create 3 files, main.tf, outputs.tf, and variables.tf

Creating the SQL Server Module

Our SQL Server Module will be in the azure_sql_server folder. I find it easiest to start with the main.tf file. Starting here we can easily identify what resource properties we want to create variables for.

Next we go into the variables.tf file. Here we define each of the variables we identified in main.tf. This includes the variable type and description.

Last we need to define the outputs we want from the module in the outputs.tf file. The outputs defined in this file are what our module will return after being called. For this module we only need the id of the server returned. This is needed so we can assign database resources to the server.

Creating the SQL Database Module

The SQL Database module will be in the azure_sql_db folder and we will again start with the main.tf file. Here we define the variables for the database resource.

Then we define the variables.

This module doesnt have an outputs file because we dont need to pass any of its information to anything. An outputs file can be added in the future should we need something though.

Using the Modules

Now we are going to dive into the main.tf file in the project folder. We will use the modules we just defined to create identical resources in separate azure regions.

  1. azurerm is the name of the Terraform Azure provider
  2. This is required to configure the provider
  3. Create a Resource Group in East US 2
  4. Create a Resource Group in West US 2

Now that we have our resource groups created we can call the SQL Server module to create a server in each group.

  1. Define the module call
  2. Provide the path to the folder containing the SQL Server module
  3. Give the server a name
  4. Reference to the name of the resource group this server will be in
  5. Reference to the azure region of the resource group this server will be in
  6. Username of the admin user account
  7. ObjectId of the admin user account
  8. The environment to tag this resource with

With the servers established we can now create the SQL Databases.

  1. Define the module call
  2. Provide the path to the folder containing the SQL Database module
  3. Give the database a name
  4. Reference the Id of the server the database will belong to. This is where the output we added to the SQL Server module comes in.
  5. The environment to tag this resource with

Ready for Deployment

With everything we have added to our project we are now ready to deploy our resources. If you run terraform apply you will deploy a SQL Server and SQL Database to East US 2 and West US 2.