Using the Terraform configuration language, you can define various Azure resources such as virtual machines, storage accounts, networking components, databases, and more. Here are a few examples:
Creating an Azure Virtual Machine:
resource “azurerm_virtual_machine” “example” {
name = “example-vm”
location = “West Europe”
resource_group_name = azurerm_resource_group.example.name
vm_size = “Standard_DS2_v2”
storage_image_reference {
publisher = “Canonical”
offer = “UbuntuServer”
sku = “18.04-LTS”
version = “latest”
}
os_disk {
name = “example-osdisk”
caching = “ReadWrite”
storage_account_type = “Standard_LRS”
}
admin_username = “adminuser”
admin_password = “password123”
}
Provisioning an Azure Storage Account:
resource “azurerm_storage_account” “example” {
name = “examplestorage”
resource_group_name = azurerm_resource_group.example.name
location = “West Europe”
account_tier = “Standard”
account_replication_type = “LRS”
tags = {
Environment = “Production”
}
}
Creating an Azure Virtual Network:
resource “azurerm_virtual_network” “example” {
name = “example-vnet”
address_space = [“10.0.0.0/16”]
location = “West Europe”
resource_group_name = azurerm_resource_group.example.name
subnet {
name = “example-subnet”
address_prefix = “10.0.1.0/24”
}
}
In this example, we create an Azure Virtual Network using the azurerm_virtual_network resource. We define the name, address space, location, and associate it with an existing resource group. Additionally, we specify a subnet within the virtual network.
Provisioning an Azure App Service:
resource “azurerm_app_service” “example” {
name = “example-appservice”
location = “West Europe”
resource_group_name = azurerm_resource_group.example.name
app_service_plan_id = azurerm_app_service_plan.example.id
site_config {
app_command_line = “”
dotnet_framework_version = “v5.0”
linux_fx_version = “DOCKER|nginx”
java_version = “11”
}
app_settings = {
“ENVIRONMENT” = “production”
}
}
In this example, we provision an Azure App Service using the azurerm_app_service resource. We specify the name, location, associated resource group, and the ID of the app service plan. We also define the site configuration, including the framework versions, Linux FX version, and Java version. Additionally, we set the app settings using key-value pairs.
Creating an Azure SQL Database:
resource “azurerm_sql_database” “example” {
name = “example-database”
resource_group_name = azurerm_resource_group.example.name
location = “West Europe”
server_name = azurerm_sql_server.example.name
edition = “Standard”
compute_model = “Serverless”
auto_pause_delay = 60
tags = {
Environment = “Production”
}
}
Creating an Azure Cosmos DB Account:
resource “azurerm_cosmosdb_account” “example” {
name = “example-cosmosdb”
location = “West Europe”
resource_group_name = azurerm_resource_group.example.name
offer_type = “Standard”
kind = “GlobalDocumentDB”
consistency_policy {
consistency_level = “Session”
}
}
Here, we create an Azure Cosmos DB account using the azurerm_cosmosdb_account resource. We define the name, location, associated resource group, offer type, and the kind of Cosmos DB account. Additionally, we set the consistency policy to “Session”.
These examples illustrate how Terraform can be used to provision a variety of Azure resources. With Terraform’s declarative syntax, you can easily define and manage complex infrastructure deployments, leveraging the full range of Azure services.