Terraform & Azure Initiation
Intro
Terraform is an open-source infrastructure as code (IAC) tool that allows you to define, provision, and manage infrastructure resources in a safe, consistent, and repeatable way. It uses a declarative language to describe the desired state of your infrastructure and then manages the resources necessary to achieve that state.
Here are some key concepts and terminology to understand when getting started with Terraform:
- Provider: A plugin that connects Terraform to a specific cloud provider or technology platform (such as AWS, Azure, or Kubernetes).
- Resource: A declarative block of code that represents a single infrastructure object, such as a virtual machine, network interface, or database.
- State: A snapshot of the current state of the infrastructure that Terraform is managing, stored in a file called
terraform.tfstate
. Terraform uses this file to determine what resources need to be created, updated, or deleted. - Plan: A preview of the changes that Terraform will make to the infrastructure when applied. This allows you to review and verify the changes before actually applying them.
- Apply: The process of actually creating, updating, or deleting resources in the infrastructure based on the desired state defined in your Terraform configuration files.
To use Terraform, you typically follow these steps:
- Install Terraform on your local machine.
- Define your infrastructure resources in Terraform configuration files using the appropriate syntax for your cloud provider and resource types.
- Initialize a new Terraform project in your working directory using the
terraform init
command. - Plan the changes to the infrastructure using the
terraform plan
command. - Apply the changes to the infrastructure using the
terraform apply
command. - Verify that the desired state of your infrastructure has been achieved.
Terraform can also be used to manage the lifecycle of your infrastructure resources, including scaling, updating, and deleting resources as needed. It provides a powerful and flexible way to manage complex cloud environments and automate your infrastructure operations.
Some common Terraform commands include:
terraform init
: This initializes the working directory and downloads any necessary provider plugins.terraform plan
: This generates an execution plan, showing what actions Terraform will take to reach the desired state.terraform apply
: This applies the changes necessary to reach the desired state of the infrastructure.terraform destroy
: This removes all the resources created by Terraform.
Terraform & Microsoft Azure
Terraform has excellent support for Microsoft Azure, one of the leading cloud providers. Using Terraform with Azure allows you to define and manage your Azure resources in a declarative way, making it easier to create, modify, and delete resources as necessary.
To use Terraform with Azure, you first need to install the Azure CLI and log in to your Azure account using the CLI. You can then configure the Azure provider in your Terraform code by specifying the required authentication details.
Here’s an example of how to configure the Azure provider in your Terraform code:
provider "azurerm" {
subscription_id = "<your subscription ID>"
client_id = "<your client ID>"
client_secret = "<your client secret>"
tenant_id = "<your tenant ID>"
}
Once you have configured the provider, you can start defining your Azure resources in Terraform. For example, you can define a virtual network with subnets, a virtual machine, and a load balancer using the Azure provider like this:
resource "azurerm_virtual_network" "example" {
name = "example-vnet"
address_space = ["10.0.0.0/16"]
location = "westus"
}
resource "azurerm_subnet" "example" {
name = "example-subnet"
resource_group_name = "example-rg"
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_public_ip" "example" {
name = "example-pip"
location = "westus"
allocation_method = "Static"
}
resource "azurerm_network_interface" "example" {
name = "example-nic"
location = "westus"
resource_group_name = "example-rg"
ip_configuration {
name = "example-config"
subnet_id = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.example.id
}
}
resource "azurerm_virtual_machine" "example" {
name = "example-vm"
location = "westus"
resource_group_name = "example-rg"
network_interface_ids = [azurerm_network_interface.example.id]
vm_size = "Standard_DS1_v2"
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
storage_os_disk {
name = "example-osdisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "example-vm"
admin_username = "adminuser"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
}
resource "azurerm_lb" "example" {
name = "example-lb"
location = "westus"
resource_group_name = "example-rg"
}
resource "azurerm_lb_backend_address_pool" "example" {
name = "example-backend-pool"
loadbalancer_id = azurerm_lb.example.id
}
resource "azurerm_lb_rule" "example" {
name = "example-rule"
resource_group_name = "example-rg"
loadbalancer_id = azurerm_lb.example.id
Terraform & DevOps
Terraform is a popular tool in the DevOps community due to its ability to define and manage infrastructure as code. It allows DevOps teams to automate the deployment and management of their infrastructure, leading to faster and more reliable deployments.
By using Terraform with a continuous integration and delivery (CI/CD) pipeline, DevOps teams can automatically test and deploy changes to their infrastructure, reducing the risk of manual errors and increasing the speed of deployment.
Some key benefits of using Terraform in a DevOps context include:
- Consistency: With Terraform, you define your infrastructure as code, ensuring that every deployment is consistent and reproducible.
- Collaboration: Terraform code can be shared and reviewed by your team, allowing for collaboration and faster development cycles.
- Automation: Terraform allows you to automate the creation, modification, and deletion of infrastructure resources, reducing the need for manual intervention.
- Scalability: Terraform can easily scale to manage large and complex infrastructures, allowing DevOps teams to manage cloud resources across multiple accounts and regions.
Terraform can be integrated with various CI/CD tools, such as Jenkins, CircleCI, and GitLab CI/CD, allowing you to automate the entire deployment process from source code to production. This integration allows teams to deploy infrastructure changes with greater speed, agility, and reliability.
Some examples
Here’s an example of how to create an Azure Storage Account with Terraform:
resource "azurerm_resource_group" "example" {
name = "example-resource-group"
location = "eastus"
}
resource "azurerm_storage_account" "example" {
name = "examplestorageaccount"
resource_group_name = azurerm_resource_group.example.name
location = "eastus"
account_tier = "Standard"
account_replication_type = "LRS"
}
In this example, we first define an Azure resource group and then create a Storage Account in that resource group.
Here’s an example of how to create an Azure Virtual Network with Terraform:
resource "azurerm_resource_group" "example" {
name = "example-resource-group"
location = "eastus"
}
resource "azurerm_virtual_network" "example" {
name = "example-vnet"
address_space = ["10.0.0.0/16"]
location = "eastus"
resource_group_name = azurerm_resource_group.example.name
}
In this example, we first define an Azure resource group and then create a Virtual Network in that resource group.
Here’s an example of how to create an Azure Key Vault with Terraform:
resource "azurerm_resource_group" "example" {
name = "example-resource-group"
location = "eastus"
}
resource "azurerm_key_vault" "example" {
name = "example-keyvault"
location = "eastus"
resource_group_name = azurerm_resource_group.example.name
enabled_for_disk_encryption = true sku_name = "standard" access_policy {
tenant_id = "<tenant ID>"
object_id = "<object ID>"
secret_permissions = [
"get",
"list",
"set",
"delete",
"backup",
"restore"
]
}
}
In this example, we first define an Azure resource group and then create a Key Vault in that resource group. We also specify an access policy that grants the specified tenant and object IDs the necessary permissions to access secrets in the Key Vault.
Here’s an example of how to create an Azure SQL Database with Terraform:
resource "azurerm_resource_group" "example" {
name = "example-resource-group"
location = "eastus"
}
resource "azurerm_sql_server" "example" {
name = "example-sqlserver"
resource_group_name = azurerm_resource_group.example.name
location = "eastus"
version = "12.0"
administrator_login = "exampleadmin"
administrator_login_password = "Password1234!"
}resource "azurerm_sql_database" "example" {
name = "example-sqldatabase"
resource_group_name = azurerm_resource_group.example.name
location = "eastus"
server_name = azurerm_sql_server.example.name
edition = "Standard"
collation_name = "SQL_Latin1_General_CP1_CI_AS"
max_size_gb = 1
}
In this example, we first define an Azure resource group and then create an SQL Server and SQL Database in that resource group. We specify the necessary parameters for the SQL Server and database, such as the administrator login and password, edition, and location.
Note that these are just examples, and you may need to customize them based on your specific requirements. You can refer to the Terraform documentation here.