Chisom Jude
Chisom Jude

Chisom Jude

Deploying 3 Tier Application to Azure App Service via Terraform

#Azure #IAC #Terraform #DevOps #Cloud #Appservice

Chisom Jude's photo
Chisom Jude
·Jul 7, 2022·

4 min read

Deploying 3 Tier Application to Azure App Service via Terraform

Table of contents

This article explains how to provision and deploy a three-tier application to Azure using Terraform. The steps below show how I deployed a simple PHP + MSSQL application to Azure App Service using Terraform.

What is Terraform

According to to Terraform documentation, Terraform is an open-source infrastructure as a code software tool that enables you to safely and predictably create, change, and improve infrastructure. In other words, terraform is used for automating infrastructure provisioning.

Using terraform, your resources in the cloud are managed and maintained as code, you can manage different versions, collaborate, backup, and maintain consistency. These infrastructures could mean servers, databases, firewalls, and more. For more detail see terraform.io

Basic Requirements

In this project, I will be working with Microsoft Azure as our cloud provider, hence we will need to set up all requirements on our local machine. Below are lists of all that will be required in the project.

  • Azure Account with an active subscription. You can get a Azure free account
  • Code Editor ( Visual Studio Code preferably)
  • Git Installed and a Github Account (or basically git URL to clone the required application)
  • MSSQL tool to manage your DB ( Azure Data Studio app, this might not be necessary if your application has a backend that manages your database)
  • And of course, we will need to install Terraform on the host

As previously mentioned I will be working with azure, hence the need to have Azure CLI installed on your host.

Before we proceed

Perhaps you have more than one Azure subscription on your account, Confirm your default subscription using, as terraform will pick the default subscription for creating resources

az login
az account list - output=table

If you need to change your default subscription, then run this

az account set - subscription "subscription-id"

Confirm Terraform is installed

terraform -version

What application are you hosting

It is important to test the application you intend to host at this point. If you will be cloning an application, test this on your local machine and confirm there are no errors.

git-push-normal.gif

Once you tested locally and this works fine, push this to your Github and keep the GitHub URL safe. you will need this later. For this project, I used my simple INEC Location Search App

Let's dive into Terraform

Now we have our application ready, our host also ready, with all dependencies installed. What next? We are set to automate our deployment using terraform. Let's Dive in

going.gif

Create your Terraform files

I have 5 major files, this help for easy understanding

  1. main.tf : This file contains all resources I will be deploying to Azure. This includes resource group, App service plan, App service, SQL Server, SQL Database, Storage account and more
  2. variables.tf : Here I declared all variables' names used
  3. terraform.tfvars : Contains default values for my variable
  4. output.tf : This help fetch your deployment outputs to console
  5. providers.tf : This contains major providers
resource "azurerm_resource_group" "rg" {
  name     = var.resource_group_name
  location = var.resource_group_location

  tags = {
    environment = "production"
  }
}

resource "azurerm_service_plan" "plan" {
  name                = var.app_service_plan_name
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  sku_name            = "B1"
  os_type             = "Linux"
}

resource "azurerm_linux_web_app" "app" {
  name                = "webappname"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_service_plan.plan.location
  service_plan_id     = azurerm_service_plan.plan.id

  site_config {

  }

  app_settings = {
    #"SOME_KEY" = "some-value"
  }

  connection_string {
    name  = "Database"
    type  = "SQLAzure"
    value = "Server=tcp:azurerm_mssql_server.sql.fully_qualified_domain_name Database=azurerm_mssql_database.db.name;User ID=azurerm_mssql_server.sql.administrator_login;Password=azurerm_mssql_server.sql.administrator_login_password;Trusted_Connection=False;Encrypt=True;"
  }
}
#connect the web files on github
resource "azurerm_app_service_source_control" "example" {
  app_id   = azurerm_linux_web_app.app.id
  repo_url           = "github URL here"
  branch             = "branch to deploy"

  depends_on = [
    azurerm_linux_web_app.app
  ]
}

resource "azurerm_mssql_server" "sql" {
  name                         = var.sql_server_name
  resource_group_name          = azurerm_resource_group.rg.name
  location                     = azurerm_resource_group.rg.location
  version                      = "12.0"
  administrator_login          = var.sql_admin_login
  administrator_login_password = var.sql_admin_password
}

resource "azurerm_mssql_database" "db" {
  name           = "yourDBname"
  server_id      = azurerm_mssql_server.sql.id
  collation      = "SQL_Latin1_General_CP1_CI_AS"
  license_type   = "LicenseIncluded"
  sku_name       = "S0"
  zone_redundant = false
}

Check out this project files on my Github

Terraform + Azure AppService + MSSQL

Run your Terraform Script

  • First you run the terraform init to initialize the folder and allow terraform to download all necessary plugin
  • You can run the terraform plan to check access all the resources to be deployed or terraform validate to confirm all syntax are declared correctly.
  • When you are ready to go, you hit the `terraform apply to deploy your resource to azure

Confirm all resources are deployed Azure

Once deployment is completed on terraform console, navigate to Azure portal, find the resource group and preview your resources. resources.PNG

Navigate to your Web App and confirm your app is deployed without errors. azure app service app.PNG

It's beautiful to know that all changes from local, pushed to GitHub, will automatically be deployed to Azure via github actions.

Check Deployment center >Logs azure deployment ci-cd-a.gif

Thank you for reading. Please like and share your thoughts with me on the comment box.

PS: Gifs are extracted from google search

 
Share this