In the world of DevOps, Infrastructure as Code (IaC) is a crucial concept. It simplifies managing and setting up IT infrastructure using configuration files that tools like Terraform can interpret. This approach automates infrastructure management through declarative programming.
Introducing Terraform
Terraform, developed by HashiCorp, is an open-source tool for Infrastructure as Code. It enables automated creation of infrastructure across cloud platforms like Azure, AWS, and Google Cloud using specific providers—for example, azurerm
for Azure. Terraform empowers users to manage infrastructure through code, offering precise control and automation.
#TerraformCDK #TypeScript
Lifecycle of Terraform
- init: Initializes the working directory, preparing necessary components.
- plan: Creates an execution plan to reach the desired infrastructure state, highlighting configuration changes. it also detects the changes in the configuration files whether something is changed(added/deleted) in order to achieve the desired state
- apply: Implements the changes defined in the execution plan.
- destroy: Deletes resources marked for removal after the apply phase.
Configuration Overview
Configuration files, typically written in HashiCorp Configuration Language (HCL) or JSON format (tf.json), define infrastructure requirements. These files organize into modules. A module groups related infrastructure elements like resource groups or databases, streamlining management.
Once we understand the commands, it’s important to grasp how to write configuration files. Let’s create a simple example configuration file to provision a web app in Azure.
We use a declarative configuration language called HCL to describe infrastructure requirements. Configuration files typically have a .tf
extension, but they can also be in JSON format with .tf.json
extension.
Configuration files can be organized into modules. So, what exactly is a module? When setting up infrastructure, declaring resources is key. Resources are elements like resource groups, virtual networks, or databases. When these resources are grouped together based on their relationship, they form a module. Think of it like organizing related files in a Windows directory; similarly, modules help organize related resources.
It’s worth noting that HashiCorp provides many predefined reusable modules that can be used in various scenarios.
Every configuration file must include at least a root module, which is where execution begins. This root module may incorporate other child modules depending on the setup.
To get started with Terraform, download it from here. After downloading the .zip
file, place terraform.exe
in your preferred location. Configure its location in your environment paths to access it from anywhere.
Now, let’s delve into the main.tf
configuration file responsible for creating a resource group and web app.
This configuration file is divided into distinct sections, which we’ll explore below.
Getting Started with Terraform
To start using Terraform, begin by downloading it from Terraform’s official site. After downloading, place the terraform.exe
file in a convenient location and configure its path in environment variables for easy access.
Providers, Variables and Resources
Providers: Terraform depends on plugins known as “providers” to communicate with remote systems. For instance, the “azurerm” provider is used for Azure.
################## Providers ########################################
provider "azurerm" {
version = "=2.36.0"
features {}
}
Variables: The Terraform language offers several types of blocks for managing named values:
Input Variables: These act as parameters for Terraform modules, enabling users to customize behavior without modifying the source code.
Output Values: These are akin to return values from Terraform modules.
Local Values: This feature provides a convenient way to assign a brief name to an expression within Terraform configurations.
################## Variables ########################################
variable "resource_group_name" {
type = string
description = "RG name in Azure"
default = "my_terraform_rg"
}
variable "resource_group_location" {
type = string
description = "RG location in Azure"
default = "centralindia"
}
variable "app_service_plan_name" {
type = string
description = "App Service Plan name in Azure"
default = "my-appserviceplan"
}
variable "app_service_name" {
type = string
description = "App Service name in Azure"
default = "terraform-homedemo-010"
}
Resources: Resources form the cornerstone of the Terraform language. Each resource block defines one or more infrastructure components, such as virtual networks and compute instances.
################## Resources ########################################
resource "azurerm_resource_group" "rg" {
name = var.resource_group_name
location = var.resource_group_location
tags = {
environment = "development"
}
}
Creating Azure Resources: Example Configuration
Let’s explore a simple main.tf
configuration file that sets up a resource group and a web app in Azure:
################## Providers ########################################
provider "azurerm" {
version = "=2.36.0"
features {}
}
################## Variables ########################################
variable "resource_group_name" {
type = string
description = "RG name in Azure"
default = "my_terraform_rg"
}
variable "resource_group_location" {
type = string
description = "RG location in Azure"
default = "centralindia"
}
variable "app_service_plan_name" {
type = string
description = "App Service Plan name in Azure"
default = "my-appserviceplan"
}
variable "app_service_name" {
type = string
description = "App Service name in Azure"
default = "terraform-homedemo-010"
}
################## Resources ########################################
resource "azurerm_resource_group" "rg" {
name = var.resource_group_name
location = var.resource_group_location
tags = {
environment = "development"
}
}
resource "azurerm_app_service_plan" "app_plan" {
name = var.app_service_plan_name
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
sku {
tier = "Standard"
size = "S1"
}
tags = {
environment = "development"
}
}
resource "azurerm_app_service" "webapp" {
name = var.app_service_name
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
app_service_plan_id = azurerm_app_service_plan.app_plan.id
site_config {
dotnet_framework_version = "v4.0"
scm_type = "LocalGit"
default_documents = [
"hostingstart.html"
]
}
app_settings = {
"SOME_KEY" = "some-value"
}
tags = {
environment = "development"
}
}
Executing Terraform Commands
Execute the following commands in sequence to deploy the resources:
- terraform version: to check the version of terraform.
terraform init
: Initializes Terraform and installs necessary plugins.terraform plan -out main.tfplan
: Generates an execution plan.terraform apply "main.tfplan"
: Applies the plan to create resources in Azure.
Verify in the Azure Portal that the resources—resource group, service plan, and web app—are successfully created.
Browse this link https://terraform-homedemo-010..azurewebsites.net
Conclusion
With Terraform, creating and managing Azure resources becomes straightforward and efficient. It enhances control over infrastructure provisioning through automation, crucial for integrating into Azure DevOps CI/CD pipelines seamlessly. Embrace Terraform to simplify and empower your infrastructure management workflows.