Search code examples
terraformazure-virtual-machineterraform-provider-azure

How to Start postgres.exe and Change Postgres Account on an Azure Virtual Machine Using Terraform


I am using an Image in Azure Galleries to create an Azure Virtual Machine. The operating system is Windows (Windows Server 2022 Datacenter Azure Edition). The Postgresql program has been pre-installed within the Image. How can I start postgres.exe immediately after deploying the Azure Virtual Machine using Terraform?

My current solution is to use Invoke-AzVMRunCommand, but I am wondering if there are any other methods available (like use azurerm_virtual_machine_extension). Additionally, if I want to change the postgres account using Terraform, how should I proceed?

Appreciated for for all your assistance.


Solution

  • How to Start postgres.exe and Change Postgres Account on an Azure Virtual Machine Using Terraform

    The best way to start any service and install applications inside the VM is to use the azurerm_virtual_machine_extension resource.

    Here is the Postgres service status before executing the terraform script.

    enter image description here

    In your case, you can use the azurerm module as shown below.

    resource "azurerm_windows_virtual_machine" "main" {
    name                  = "demovm"
    
    remaining configuration
    }
    
    resource "azurerm_virtual_machine_extension" "example" {
    name                 = "postgres_Service_start"
    .
    .
    remaining configuration
    
       settings = <<SETTINGS
     {
      "commandToExecute": "powershell -ExecutionPolicy Unrestricted Start-Service -Name postgresql-x64-16"
     }
    SETTINGS
    depends_on = [ azurerm_windows_virtual_machine.main ]
    }
    

    In my case, I used an existing VM for testing, so I referenced it using a data block.

    provider "azurerm" {
      features {}
    }
    data "azurerm_virtual_machine" "example" {
      name                = "windows-Server-2022"
      resource_group_name = "VM-RG"
    }
    resource "azurerm_virtual_machine_extension" "example" {
      name                 = "postgres_Service_start"
      virtual_machine_id   = data.azurerm_virtual_machine.example.id
      publisher            = "Microsoft.Compute"
      type                 = "CustomScriptExtension"
      type_handler_version = "1.8"
      auto_upgrade_minor_version = true
    
      settings = <<SETTINGS
     {
      "commandToExecute": "powershell -ExecutionPolicy Unrestricted Start-Service -Name postgresql-x64-16"
     }
    SETTINGS
    }
    

    Terraform apply

    enter image description here

    Extension has been created in VM successfully, as below.

    enter image description here

    After executing the Terraform code, the PostgreSQL service started successfully.

    enter image description here

    To change a PostgreSQL account, connect to the database and check the existing users. Since it’s not possible to do this with Terraform, you can execute a script using extensions if you find any using extensions. For more details, follow this link.