Terraform on Azure
From the last article I talked about Introduction to Terraform. From this article I’m going to talk about how to work with Microsoft Azure using your favorite IaaC tool Terraform. In this article I will show you step by step how to set up some azure resources using Terraform. Let’s get started.
Folder Structure
As a beginner, we can start with a simple folder structure. For the resource configuration we can create “main.tf” file. For the variable initialization we can create “variables.tf” file.
Provider
First, we need to select your cloud service provider. I selected my cloud service provider as azure and we have to configure it. I have mentioned an example below.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.15.1"
}
}
}
provider "azurerm" {
features {}
}
Resources
We can easily configure a group of resources using Terraform. The best thing is that we can configure dynamic resources depending on the situation and assign values to the resource and create it. And we can configure the outputs which depend on another resource. I mentioned some example resources that are useful for any common application
Resource group
This is the common way to configure a resource group
# example resource group
resource "azurerm_resource_group" "main" {
name = "test_resource_group"
location = "centralus"
tags = {
environment = "dev"
}}
But we can use that resource dynamically. Now I’m going to show you how to dynamically configure a resource. For that we can initialize some variables for dynamic identifiers.
# dynamic resource group
resource "azurerm_resource_group" "main" {
name = var.resource_group_name # set the name as variable
location = var.resource_group_location
tags = {
environment = var.enviroment
}}
All other resources are also dynamically configured.
App Service plan & App Service
resource "azurerm_app_service_plan" "main" {
name = var.app_service_plan_name
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
# get the resource group output name and location and assigned
sku {
tier = "Standard"
size = "S1"
}}
resource "azurerm_app_service" "main" {
name = var.app_service_name
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
app_service_plan_id = azurerm_app_service_plan.main.id
}
SQL server & SQL Database
resource "azurerm_mssql_server" "main" {
name = var.mssql_server_name
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
version = "12.0"
administrator_login = var.mssql_server_username
administrator_login_password = var.mssql_server_password tags = {
environment = var.environment
}
}
resource "azurerm_mssql_database" "main" {
name = var.mssql_database_name
server_id = azurerm_mssql_server.main.id
license_type = "LicenseIncluded"
tags = {
environment = var.environment
}
}
That way we can dynamically configure our resources. The benefit of this is that we can use these configured resources for any other project. The only thing is to assign respective values for each variable. Then you can configure your resources. Now I’m going to show how to declare the variables.
Variables
If we want to configure some resource dynamically, Then we have to declare the respective variable for each an every dynamic identifier in the variables file.
variable "<<variable_name>>" {
type = "<<variable_type>>" #string number bool
description = "<<description>>" # [optional]
value = "<<value>>" # [optional]
# if value is assigned it will be the default value}
These are some example, variable expressions
These are some example, variable expressionsvariable "resource_group_name" {
type = string
description = "value of resource group name"}
variable "resource_group_location" {
type = string
description = "value of resource group location"}
variable "environment" {
type = string
description = "value of environment"}
Similarly, we need to declare all initialized variables in the main file in the variables file.
Testing
First, you have to login to your Azure account.
If you installed Azure CLI , you can Sign In to your Azure account using
az login
command.
After all this configuration, now we can test our terraform script. For that first we need to initialize our script using the terraform init
command.
You can format your terraform script using
terraform fmt
command
After the terraform init
command now you can hit the terraform plan
Command to determine the desired state of all the resources it declares. But before executing that command we need to give proper values to the respective variable.
If the process is successful, you can see the added resources after entering the values for the variables.
After the planning successful, we can hit the terraform apply
command for the deployment. (Again, you have to assign values for the created variables).
If you get above success message Congratulations, you applied your first dynamic terraform script successfully.
Now you can see your resources in the Azure portal.
Like this you can create your own dynamic terraform scripts with your respective resources. I think now you have much knowledge about how to work with terraform. And I wish to push all the source code to the GitHub and you can refer it as well.
In the next article I hope to talk about Terraform Modules, a very useful and important concept in Terraform. We can use this module dynamically, efficiently and with less errors.
See you in the next article. Stay Safe and good Bye 😊
Happy coding 💻
References:
Azure App Service with terraform
Azure SQL database with terraform