Sunday 29 August 2021

Enable Boot Diagnostic Via Terraform Part 2

For this posting, it is more like an update as i recently found a better way in github (link here) to enable the boot diagnostic to Azure VM . 

This is code segment that i use previously 


  boot_diagnostics{
          enabled = true
          storage_uri = "Https://${azurerm_storage_account.hub-core-vmdiag.name}
                        .blob.core.windows.net"
}

so i did my own experiment to test that and turn out it is easier to be implemented 
here is code up 

1. AzureRM_virtual_machine resource block
 
 boot_diagnostics {
      enabled = true
      storage_uri = azurerm_storage_account.hub-core-vmdiag.primary_blob_endpoint
    }

2. AzureRM_windows_virtual_machine resource block 

 boot_diagnostics { 
      storage_account_uri = azurerm_storage_account.hub-core-vmdiag.primary_blob_endpoint
    }

that all.. thanks for reading and stay safe

Saturday 28 August 2021

Importing Existing Azure Vnet into Terraform

Previously i have posted a way to import a resource group into terraform. so today it will a continue process to that where it time to import virtual network into terraform . 

This will be a bit challenging as it got some dependencies to another resources like Subnet and NSG. 

So let begin, so always start with creating a empty of your vnet but for this scenario i will suggest to import NSG first as it was bind with subnet. 


resource "azurerm_network_security_group" "nsg-app" {
 
}

Then run the terraform import for this nsg 

 Terraform import azurerm_network_security_group.nsg-app xx/xxx/xxx/NSG-APP

After that, use terraform show to check what are the information needed for NSG block to match with deployment. 


for my case, i just add in the name and few importance information without all the additional rule created in the NSG. 


this step need to be repeat to all   NSG created before touching on Virtual Network and can be skip if no NSG being created or attach to subnet. 

continuing from that, you may start importing the virtual network with the same step and continue with the subnet . i was planning to use one resource block to address vnet and subnet like here 


but seem like create separate block for each subnet will be easier as less information needed for the subnet block. 




repeat for all the subnet available and check if there more changes needed with terraform plan . For me this is enough to import all. 

Thanks for reading and stay safe. 

Saturday 21 August 2021

Import Azure Existing Resource Group to Terraform

 As part of using terraform to manage the architecture, there a time when the environment has existing resources or the resource being deploy via portal instead of terraform 

So, in order to keep all the control in terraform, the resource need to be imported to terraform so it can be manage from there. 

In this case , Azure Resource Group will be imported to terraform, as we all aware, all resource in azure need to be located in resource 


1.Start with create an empty block the resource ;

resource "azurerm_resource_group" "prod-rg" {
    
}

"prod-rg" is just a block name, can be name with any name but i prefer to tally with the resource group name created on Azure. 

2. Get the RG resource id from azure portal 


3. Import command need to parameter which is resource and resource id 

For this case - terraform import azurerm_resource_group.prod-rg /xxx/xxx/resourcegroup/PROD-RG

4. After import is complete, some info need to be added into resource group block. 

4.1 you may run terraform show to see what need to be added 

  

 Not all need to be added 

5. Edit the RG block as follow 

resource "azurerm_resource_group" "prod-rg" {
    name = "PROD-RG"
    location = "southeastasia"
}

6. run terraform plan to check if any more information need to be add, but as for now, that two information is enough


That all i have for now, feel free to leave a feedback in the comment, happy terraforming and stay safe 

Friday 13 August 2021

Windows VM Stuck after Restart

Hello all, 

for this time round, i would like to share some fix i have done due to windows VM is not accessible after applying July update . So when the restart was performed, the VM is not responding to RDP and CPU usage is 0.02% from the Azure Portal for few hours. Turn out some error happen on booting up  the vm and you can see if VM screen like screenshot below if the boot diagnostic is enable


The method of solving this kind of behavior is either restoring from backup of continue work on the affected VM . I will explain more on the solution provided by microsoft support. 

As usual, the solution will need a temporary VM as fixer and the step as follow 

1. Create a Disk Snapshot of affected VM ; name it VM01-snapshot or ss

2. Create a manage disk using VM01-snapshot ; name it VM01-OSdisk-01

3. Create a temporary VM with Hyper V enable ; i name it as HyperV

4. Attach VM01-OSdisk-01 as a data disk to HyperV VM 

5. Do a RDP to HyperV vm and open Command Prompt 

6. Run "dism /image:G:\ /cleanup-image /revertpendingactions" ; change letter G according to  os disk of VM01

7. Once the process completed, go to disk management and "offline" the VM01 Os disk 

8. Create a VM in Hyper V; name TestVM, choose Gen1 because most azure vm is Gen1 and during the disk selection, choose attach the disk later.

9. After the TestVM created, right click and go to setting, On IDE click add hard disk and choose physical drive. This will work as Hyper V support pass through disk in normal hyper v deployment which means it will use physical disk instead of virtual hard disk

    


10. Set a good number for cpu and memory and try to boot the VM. 

11. As the vm is boot successfully and the TestVm can be power off, remove from TestVM setting and remove from HyperV vm data disk in azure portal.

12. Perform  "Swap OS disk" operation with source vm in Azure portal. 

Hope this will help somebody out there , leave a feedback in the comment and  stay safe


Kubecost on AKS Part 02