Skip to main content

Deploy a MySQL Database with Terraform

Hands-On Lab

 

Photo of Russ Croft

Russ Croft

Azure Training Architect II

Length

00:30:00

Difficulty

Beginner

In this lab, we will deploy a MySQL database instance.

In the previous lab exercise, we deployed a Web application. It's almost unheard of to find a Web application that doesn't have a database backend. MySQL and MariaDB are among the most common types of databases for Web apps being robust and open source.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Deploy a MySQL Database with Terraform

In this lab, we will deploy a MySQL v5.7 database instance with 5GB of storage with a Gen 5, basic tier database SKU. To do this, we will be using the Azure CLI to create storage to deploy the database using Terraform.

Before We Begin

Before we get started, we need to log into the Azure portal using the provided credentials.

Set Up the Azure CLI

In the Azure portal, select the Command Line button at the top of the screen. Open the CLI. Here, select Bash when prompted. We then want to select Show Advanced Settings. Leave both the Resource Group and Storage Account as the provided defaults. In the File share section, enter a name for the account (for this example, we are using Console) and click the Attach Storage button. Once the command prompt completes, we can continue.

Deploy a MySQL Database Instance

Using the following code block, create a .tf file named lab.tf. Replace the "unique storage account name" with a unique name (we'll use"tflab-mysqlserver-1" for our example) and the "Enter Resource Group Name" with the name of the resource group provided by the lab, which should be preceded by three numbers on the Azure portal's main page. For our video example, this title starts with 191.

provider "azurerm" {
    version = 1.38
    }

resource "azurerm_app_service_plan" "svcplan" {
  name                = "Enter App Service Plan name"
  location            = "eastus"
  resource_group_name = "Enter Resource Group Name"

  sku {
    tier = "Standard"
    size = "S1"
  }
}

resource "azurerm_app_service" "appsvc" {
  name                = "Enter Web App Service Name"
  location            = "eastus"
  resource_group_name = "Enter Resource Group Name"
  app_service_plan_id = azurerm_app_service_plan.svcplan.id

  site_config {
    dotnet_framework_version = "v4.0"
    scm_type                 = "LocalGit"
  }
}

Once we've saved the file, go to the Upload/Download button above the Azure CLI and upload the file.

With that uploaded, we need to run the command terraform init. Next, run terraform plan and review the output to confirm that our changes are reflected. The green plus signs indicate the resources that will be added.

Lastly, run terraform apply to confirm these changes, answering yes to the prompt to continue.

To make sure that this worked correctly, go back to the Resource groups page and select Refresh. The database that we created appears on our resource list.

Conclusion

Upon completing the lab, we are now able to use the Azure CLI to create a MySQL Database and the type of code needed to create the database. Congratulations on finishing the lab!