Infrastructure as a code – Automate infrastructure deployments in Azure
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.
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.
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.
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.
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.
Login to azure subscription. az login
If you need to change your default subscription.az account set-subscription "subscription-id"
Verify terraform version.terraform -version
Implement the Terraform code.
Create a directory in which to evaluate the Terraform code and make it the current directory.
Download the terraform code files from my GitHub project.git clone https://github.com/mkdevops23/Terraform-code.git
Create a file named providers.tf and insert the downloaded file code.
Create a file named variables.tf and insert the downloaded file code.
Create a file named terraform.tfvars and update each variable value as per your requirement.
Create a file named outputs.tf and insert the downloaded variables file code.
Create a file named main.tf and insert the downloaded variables file code.
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
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 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:
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
Open your Azure DevOps project and go into the Azure Pipelines section.
Select Create Pipeline button.
For the Where is your code? option, select GitHub (YAML).
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.
In the repositories list, select the fork of the repository you created in your GitHub organization.
In the Configure your pipeline step, choose to start from an existing YAML pipeline.
Select Continue to load the Azure YAML pipeline from GitHub.
When the Review your pipeline YAML page displays, select Run to create and manually trigger the pipeline for the first time.
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.
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.
Solution Architect – Azure Infrastructure
Subscribe to our blogs
Subscribe to our blogs
Modernize Your App Development and Automation Capabilities
How to automate infrastructure deployments in the Azure with Infrastructure as code (IaC).