Deployment of Azure resources using IaC technique
Cloud technology is the talk of the town these days and every company wants to leverage the offerings of the cloud either partially or completely. And whenever we talk about cloud, Microsoft Azure always comes into the picture. It is after all one of the leading cloud service providers in the market.
Application developers need to deploy their respective applications to the cloud, similar to how they would do in an on-premise setup. And setting up the infrastructure manually is a tedious process be it in on-premises or cloud.
Let us consider an example. Company A was trying to establish its base in Azure and had 4 different environment requirements viz. development, testing, pre-production, and production. Now the developers had to create the resources in the Azure portal manually for all the 4 regions.
Now the company was developing a Single Page Application (SPA), and they needed two app services and one database to be created in the Azure portal. So, for four environments, a total of 12 applications had to be created manually.
Imagine how cumbersome it can become if the company had more applications to create. It will be quite challenging to manage the applications in the Azure portal. On top of it, there are possibilities of human errors or inconsistencies in creating services.
The question is how can the entire process of creating resources be made simpler? The answer- Deployment of resources through Infrastructure as Code (IaC) technique through Azure Resource Manager template (ARM). Let's begin the post with a basic understanding of what is IaC and what are its benefits before moving onto the practical implementation section.
Table of contents
- What is IaC?
- Benefits of IaC
- What is Azure Resource Manager?
- Practical implementation of an ARM template
What is IaC?
IaC is the process of managing and provisioning of infrastructure (app services, database, virtual machines, etc.,) using machine-readable configuration files.
IaC has been developed to solve the problems of discrepancies in resources between environments. With IaC, users can create the machine-readable model file which is typically saved in JSON format, which can be then used to provision resources in the cloud.
Benefits of IaC
The first and foremost benefit of using this technique is the deployment speed. IaC provides the option of setting up the entire infrastructure by running a script. The same script can be used across environments from development to production. Thus making the entire software lifecycle faster.
Manually provisioning cloud resources across regions results in human errors and discrepancies between environments. IaC eliminates the problem of errors by having a single source of truth file which is used for provisioning resources in the cloud across multiple environments. By this, the same set of configurations are deployed again and again without any discrepancies.
In scenarios when resources have been provisioned incorrectly, it is easier to trace in the IaC approach as we have a single source of truth file. Changes in the file can be traced with the help of versioning the file. This could help us in identifying the issue and the person who is accountable for it.
As IaCs are generic and can be used in any region, it is easier to create new resources based on demand. Say, for example, a team wants to look into a business dedicatedly that is happening in production but can't do their experiments in production as it would be expensive. For such cases, on an Ad-hoc basis, new regions can be created using IaC which is used to push infrastructure and code in a single step.
Reduced Opportunity cost
IaC removes the necessity of having highly paid professionals performing the tasks of provisioning resources in the cloud.
Similar to the code, IaC files are extensible. Based on need, it can be modified at any time.
What is Azure Resource Manager?
In Microsoft's cloud platform, IaCs are enabled using the Azure Resource Manager (ARM). ARM allows users to create templates of the infrastructure in JSON format, which holds the configuration of the project that needs to be provisioned.
There are two ways, in general, to define how IaC works
- Imperative approach,
- Declarative approach
The imperative approach is a sequence of commands that are provided to reach the final step of creating the infrastructure.
The declarative approach is declaring the desired outcome instead of explicitly mentioning the steps involved in it.
ARM templates follow a declarative approach, which allows the users to directly state what needs to be deployed over how it has to be deployed.
Practical implementation of an ARM template
There are multiple ways to use the ARM templates for creating resources in the Azure portal. One of the techniques is through Visual Studio. Let's look into the step-by-step process of how to create ARM templates for web apps using Visual Studio.
Create a new project in a Visual Studio.
Choose Azure Resource Group as a template.
In the next step, provide the project name and click on the ‘Create’ button.
In the next step, choose the category - 'Azure Quickstart (github.vom/azure/azure-quickstart-templates'
In our case, we will be choosing a basic web app. Click ‘OK’ once chosen.
Once you have created the project, it will look like below (as depicted in the screenshot). There are two main files for our needs, one is azuredeploy.json and azuredeploy.parameters.json. Remember, azuredeploy.json is used to hold the resources while azuredeploy.parameters.json is used to hold the user-defined parameters.
Parameters that are defined in azuredeploy.parameters.json can be used in the azuredeploy.json file.
In the above snippet, we have defined a variable named ‘webappName’ and has been used in azuredeploy.json under “webAppName” section’s default value property.
Once all the required details are edited in both the files, right-click the project and select ‘Deploy’ → ‘New’.
A pop-up will appear with the details of the resource and parameter file. This will be used to create the resources in the Azure portal which in turn is linked with Visual Studio.
IaC is a powerful and easy to maintain technique that helps in creating and managing resources in Azure. ARM templates can consistently deliver the required resources in the cloud and also help in provisioning the same to other environments. The same ARM templates can be used to create Virtual Machines, Azure Functions, and Azure SQL databases in Azure.