Step 1 – Install terraform
Step 2 – Copy a followng code into new directory in file called “main.tf”
terraform { | |
required_providers { | |
azurerm = { | |
source = "hashicorp/azurerm" | |
version = "3.12.0"s | |
} | |
} | |
} | |
provider "azurerm" { | |
features {} | |
subscription_id = "" | |
client_id = "" | |
client_secret = "" | |
tenant_id = "" | |
} | |
resource "azurerm_resource_group" "test" { | |
name = "acctestrg" | |
location = "West US 2" | |
} | |
resource "azurerm_virtual_network" "test" { | |
name = "acctvn" | |
address_space = ["10.0.0.0/16"] | |
location = azurerm_resource_group.test.location | |
resource_group_name = azurerm_resource_group.test.name | |
} | |
resource "azurerm_subnet" "test" { | |
name = "acctsub" | |
resource_group_name = azurerm_resource_group.test.name | |
virtual_network_name = azurerm_virtual_network.test.name | |
address_prefixes = ["10.0.2.0/24"] | |
} | |
resource "azurerm_public_ip" "test" { | |
name = "publicIPForLB" | |
location = azurerm_resource_group.test.location | |
resource_group_name = azurerm_resource_group.test.name | |
allocation_method = "Static" | |
} | |
resource "azurerm_lb" "test" { | |
name = "loadBalancer" | |
location = azurerm_resource_group.test.location | |
resource_group_name = azurerm_resource_group.test.name | |
frontend_ip_configuration { | |
name = "publicIPAddress" | |
public_ip_address_id = azurerm_public_ip.test.id | |
} | |
} | |
resource "azurerm_lb_backend_address_pool" "test" { | |
loadbalancer_id = azurerm_lb.test.id | |
name = "BackEndAddressPool" | |
} | |
resource "azurerm_network_interface" "test" { | |
count = 7 | |
name = "acctni${count.index}" | |
location = azurerm_resource_group.test.location | |
resource_group_name = azurerm_resource_group.test.name | |
ip_configuration { | |
name = "testConfiguration" | |
subnet_id = azurerm_subnet.test.id | |
private_ip_address_allocation = "dynamic" | |
} | |
} | |
resource "azurerm_managed_disk" "test" { | |
count = 7 | |
name = "datadisk_existing_${count.index}" | |
location = azurerm_resource_group.test.location | |
resource_group_name = azurerm_resource_group.test.name | |
storage_account_type = "Standard_LRS" | |
create_option = "Empty" | |
disk_size_gb = "100" | |
} | |
resource "azurerm_availability_set" "avset" { | |
name = "avset" | |
location = azurerm_resource_group.test.location | |
resource_group_name = azurerm_resource_group.test.name | |
platform_fault_domain_count = 7 | |
platform_update_domain_count = 7 | |
managed = true | |
} | |
resource "azurerm_virtual_machine" "test" { | |
count = 7 | |
name = "acctvm${count.index}" | |
location = azurerm_resource_group.test.location | |
availability_set_id = azurerm_availability_set.avset.id | |
resource_group_name = azurerm_resource_group.test.name | |
network_interface_ids = [element(azurerm_network_interface.test.*.id, count.index)] | |
vm_size = "Standard_DS1_v2" | |
# Uncomment this line to delete the OS disk automatically when deleting the VM | |
# delete_os_disk_on_termination = true | |
# Uncomment this line to delete the data disks automatically when deleting the VM | |
# delete_data_disks_on_termination = true | |
storage_image_reference { | |
publisher = "Canonical" | |
offer = "UbuntuServer" | |
sku = "16.04-LTS" | |
version = "latest" | |
} | |
storage_os_disk { | |
name = "myosdisk${count.index}" | |
caching = "ReadWrite" | |
create_option = "FromImage" | |
managed_disk_type = "Standard_LRS" | |
} | |
# Optional data disks | |
storage_data_disk { | |
name = "datadisk_new_${count.index}" | |
managed_disk_type = "Standard_LRS" | |
create_option = "Empty" | |
lun = 0 | |
disk_size_gb = "100" | |
} | |
storage_data_disk { | |
name = element(azurerm_managed_disk.test.*.name, count.index) | |
managed_disk_id = element(azurerm_managed_disk.test.*.id, count.index) | |
create_option = "Attach" | |
lun = 1 | |
disk_size_gb = element(azurerm_managed_disk.test.*.disk_size_gb, count.index) | |
} | |
os_profile { | |
computer_name = "hostname" | |
admin_username = "testadmin" | |
admin_password = "Password1234!" | |
} | |
os_profile_linux_config { | |
disable_password_authentication = false | |
} | |
tags = { | |
environment = "staging" | |
} | |
} |
Step 3 – Create IAM user and assign Service Principal Access?
Step 4 – update main.tf with following entry
subscription_id = ""
client_id = ""
client_secret = ""
tenant_id = ""
Step 5 – Go inside a directory and run following command
$ terraform init
$ terraform plan
$ terraform apply
$ terraform destroy
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND
PS D:\Devops Codes> terraform plan
Planning failed. Terraform encountered an error while generating this plan.
╷
│ Error: building account: getting authenticated object ID: listing Service Principals: ServicePrincipalsClient.BaseClient.Get(): clientCredentialsToken: received HTTP status 401 with response: {“error”:”invalid_client”,”error_description”:”AADSTS7000215: Invalid client secret provided. Ensure the secret being sent in the request is the client secret value, not the client secret ID, for a secret added to app ’27cbb9f1-be27-49e8-aee0-7ac8e7032efd’.\r\nTrace ID: 28a73c19-bbc4-45e1-aae0-ead5ac800e00\r\nCorrelation ID: 46b12c2d-aabc-47da-b8fd-ba3498320195\r\nTimestamp: 2023-04-27 05:05:54Z”,”error_codes”:[7000215],”timestamp”:”2023-04-27 05:05:54Z”,”trace_id”:”28a73c19-bbc4-45e1-aae0-ead5ac800e00″,”correlation_id”:”46b12c2d-aabc-47da-b8fd-ba3498320195″,”error_uri”:”https://login.microsoftonline.com/error?code=7000215″}
│
│ with provider[“registry.terraform.io/hashicorp/azurerm”],
│ on terraform.tf line 11, in provider “azurerm”:
│ 11: provider “azurerm” {
│
╵
PS D:\Devops Codes>