Infrastructure as a code – Automate infrastructure deployments in Azure

Business Scenario:

The following are some reasons why you might want to use IaC (infrastructure as code).

  • In a traditional environment, infrastructure management and configuration were done manually. Each environment has its unique configuration, which is configured manually, and that leads to several problems.
  • Cost as you must hire many professionals to manage and maintain infrastructure.
  • Scaling as a manual configuration of infrastructure tasks is time-consuming, making you struggle to meet spikes on request.
  • Inconsistency because the manual configuration of infrastructure is error-prone. When several people do manual configurations, errors are unavoidable.
  • A major problem is setting up monitoring and performance visibility tools for big infrastructure.

Challenges:

The following are some challenges in above business scenario.

  • The limited scale of deployments.
  • Poor scalability and elasticity of the infrastructure.
  • Extremely low cost-optimization levels.
  • The human error element was universal.
  • Major difficulties with configuration drifts and the management of vast infrastructures
  • Slow provisioning of infrastructure.
  • All these pitfalls combined account for the extremely low agility of a business using traditional infrastructures.

Solution Strategy:

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


Solution Strategy

Basic Prerequisite
  1. Azure Account with an active subscription. You can get a Azure free account
  2. Install Azure CLI on your host.
  3. Code Editor (Visual Studio Code preferably)
  4. Install Git and a GitHub Account.
  5. 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)
  6. Install Terraform on the host.
  7. Azure Service Principal: is an identity used to authenticate to Azure.
  8. Azure Remote Backend for Terraform: we will store our Terraform state file in a remote backend location. We will need a Resource Group, Azure Storage Account, and a Container.
  9. Azure DevOps Account: we need an Azure DevOps account because is a separate service from the Azure cloud.

Azure portal login using Azure CLI from visual studio terminal.
  1. Login to azure subscription.
    az login
  2. If you need to change your default subscription.
    az account set-subscription "subscription-id"
  3. Verify terraform version.
    terraform -version

Implement the Terraform code.
  1. Create a directory in which to evaluate the Terraform code and make it the current directory.
  2. Download the terraform code files from my GitHub project.
    git clone https://github.com/mkdevops23/Terraform-code.git
  3. Terraform Code
  4. Create a file named providers.tf and insert the downloaded file code.
  5. Create a file named variables.tf and insert the downloaded file code.
  6. Create a file named terraform.tfvars and update each variable value as per your requirement.
  7. Terraform Variables
  8. Create a file named outputs.tf and insert the downloaded variables file code.
  9. Create a file named main.tf and insert the downloaded variables file code.

Initialize Terraform
  • Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.
    terraform init

Create a Terraform execution plan.
  • Run terraform plan to create an execution plan.
    terraform plan -out main.tfplan

Apply a Terraform execution plan.
  • Run terraform apply to apply the execution plan to your cloud infrastructure.
    terraform apply main.tfplan
  • Terraform Execution

Verify all infrastructures are deployed.
  • Once deployment is completed on terraform console, navigate to Azure portal, find the resource group, and preview your resources.
  • Terraform Deployed

Terraform state file.

So now, we know that our Terraform code is working perfect. However, when we ran the Terraform Apply, a few new files were created in our local folder:

    Terraform State

Terraform Destroy

We can use the 'Terraform DESTROY' command to remove all the infrastructure from your subscription, so we can look at moving our state file to a centralized area.
terraform plan -destroy -out main.destroy.tfplan
terraform apply main.destroy.tfplan


Import the pipeline into Azure DevOps
  1. Open your Azure DevOps project and go into the Azure Pipelines section.
  2. Select Create Pipeline button.
  3. For the Where is your code? option, select GitHub (YAML).
  4. At this point, you might have to authorize Azure DevOps to access your organization. For more information on this topic, see the article, Build GitHub repositories.
  5. In the repositories list, select the fork of the repository you created in your GitHub organization.
  6. In the Configure your pipeline step, choose to start from an existing YAML pipeline.
  7. When the Select existing YAML pipeline page displays, specify the branch master and enter the path to the YAML pipeline: https://github.com/mkdevops23/Terraform-code/blob/main/Terraform-code-CI.yml
  8. Select Continue to load the Azure YAML pipeline from GitHub.
  9. When the Review your pipeline YAML page displays, select Run to create and manually trigger the pipeline for the first time.
  10. Verify the results.
    You can run the pipeline manually from the Azure DevOps UI. Once you've done that step, access the details in Azure DevOps to ensure that everything ran correctly.
  11. Verify the Results

Outcome & Benefits:

Automating infrastructure with Terraform and DevOps templates help us:

  • Automation of Infrastructure management allows you to create, provision, and alter your resources using template-based configuration files.
  • Automation across several clouds that is platform agnostic – It is likely the only full-featured automation solution that is platform agnostic and can be used to automate on-premises and cloud (Azure, AWS, GCP) systems.
  • Before implementing infrastructure changes, be sure you understand what's going on - Terraform plans may be used for configuration and documentation. This ensures your team understands how your infrastructure is configured and how changes might influence it.
  • Reduce the risk of deployment and configuration errors.
  • Easily deploy many duplicate environments for development, testing, QA, UAT, and production.
  • Reduce costs by provisioning and destroying resources as needed.
Mangesh Kharade

Mangesh Kharade

Solution Architect – Azure Infrastructure

Subscribe to our blogs




Follow Us