1. You have created Terraform script to deploy resources (VM) in Azure. When you run the azure release pipeline, you receive this error-

Terraform command 'validate' failed with exit code '1'.: No Terraform configuration files found in directory: d:\a\r1\a

 

Solution-

This happens when the command “Terraform plan” is executed from a directory where the Terraform scripts are not present. Make sure the “Terraform init” and “Terraform plan” commands are run from the directory where the scripts are available. Open the Release pipeline and make changes to “Configuration Directory” parameter. It will by default show $(System.DefaultWorkingDirectory) but you have to set it to the folder where Terraform files are available. Like in my setup they were in “$(System.DefaultWorkingDirectory)/_Linux Build Agent ARM template/drop”. Check the screen shots-

2.  If there is dependency on one resource on another, like Network interface (NIC) should be ready before VM can be created. You may get this error –

Terraform command 'apply' failed with exit code '1'.:  network.InterfacesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidResourceReference" Message="Resource /subscriptions/fa628409-59a2-43ed-afde-7b509d932bbd/resourceGroups/Testpoc/providers/Microsoft.Network/virtualNetworks/vnet01qaodhsub/subnets/vnet01-qaodhsub referenced by resource /subscriptions/fa628409-59a2-43ed-afde-7b509d932bbd/resourceGroups/Testpoc/providers/Microsoft.Network/networkInterfaces/qatodhsub123-nic was not found. Please make sure that the referenced resource exists, and that both resources are in the same region." Details=[]

 

Solution:

You have to add “depends_on” argument to create explicit dependency. Say, you have Network Interface (NIC) resource like below-

resource "azurerm_network_interface" "myterraformnic" {
    name                        = "${var.vmName}-nic"
    location                    = "${var.location}"
    resource_group_name         = "${var.resource_group_name}"
    
    ip_configuration {
        name                          = "myNicConfiguration"
        subnet_id                     = "${data.azurerm_subnet.azsubnet.id}"
        private_ip_address_allocation = "Dynamic"
        public_ip_address_id          = "${azurerm_public_ip.myterraformpublicip.id}"
    }

    tags = {
        environment = "Terraform Demo"
    }
}

 

To make sure that NIC is already created before VM resource is created, the VM resource will look like below

resource "azurerm_virtual_machine" "myterraformvm" {
    name                  = "${var.vmName}"
    location              = "${var.location}"
    resource_group_name   = "${var.resource_group_name}"
    network_interface_ids = [azurerm_network_interface.myterraformnic.id]
    vm_size               = "${var.vmSize}"
    depends_on            = [azurerm_network_interface.myterraformnic]

    storage_os_disk {
        name              = "${var.vmName}-disk"
        create_option     = "FromImage"
        
    }

    storage_image_reference {
        publisher = "${var.imagePublisher}"
        offer     = "${var.imageOffer}"
        sku       = "${var.ubuntuOSVersion}"
        version   = "latest"
    }

    os_profile {
        computer_name  = "${var.vmName}"
        admin_username = "${var.vmAdminUserName}"
        admin_password = "${var.vmAdminPassword}"
    }

    os_profile_linux_config {
        disable_password_authentication = false
    }

    tags = {
        environment = "Terraform Demo"
    }
}

 

3. You mage get the below error-

Terraform command 'apply' failed with exit code '1'.:  Error: either a `os_profile_linux_config` or a `os_profile_windows_config` must be specified.

 

Solution:

You have to add at least the below to the VM resource script-

   os_profile_linux_config {
    disable_password_authentication = false
  }

 

If you want to enable it then as ssh authentication like below-

 os_profile_linux_config {
        disable_password_authentication = true
        ssh_keys {
            path     = "/home/azureuser/.ssh/authorized_keys"
            key_data = "ssh-rsa AAAAB3Nz{snip}hwhqT9h"
        }
    }