Jul 14 2022 07:56 AM - edited Jul 14 2022 07:58 AM
Hi All,
I am deploying a windows VM in Azure using terraform and want to install some software while deployment so I thought about PS script extension.
Things are working fine for me however ENV variables is not getting added though If I will run this script in the system after deployment then everything is right.
Can anyone suggest me why only ENV variable is not getting set.
you can read the script here,
https://raw.githubusercontent.com/INGourav/Azure-Resources/master/azbastionsetup01.ps1
<#
Script to install chocolatey on the windows system and install some apps that is needed for Azure
Author : - Gourav Kumar
Reach Me : - email address removed for privacy reasons
Version : - 1.0.1
#>
Set-ExecutionPolicy Bypass -Scope Process -Force;
New-Item -Path 'C:\Temp\terraform_1.2.4_windows_amd64' -ItemType Directory
# setting env vars
$path = (Get-Item -Path Env:\Path).Value
Start-Sleep -Seconds 5;
$newpath = $path + 'C:\Temp\terraform_1.2.4_windows_amd64'
Start-Sleep -Seconds 5;
Set-Item -Path Env:\Path -Value $newpath
# Installation of Terraform on the machine
Invoke-WebRequest 'https://releases.hashicorp.com/terraform/1.2.4/terraform_1.2.4_windows_amd64.zip' -OutFile C:\temp\terraform_1.2.4_windows_amd64.zip -Verbose
Start-Sleep -Seconds 5;
Expand-Archive C:\temp\terraform_1.2.4_windows_amd64.zip C:\temp\terraform_1.2.4_windows_amd64 -Verbose -Force
Start-Sleep -Seconds 5;
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) -Verbose
Start-Sleep -Seconds 5;
# Installation of apps (Pycharm, vscode, git, and drwaio)
choco install pycharm -y --force;
Start-Sleep -Seconds 5;
choco install vscode -y --force;
Start-Sleep -Seconds 5;
choco install git -y --force;
Start-Sleep -Seconds 5;
choco install drawio -y --force;
Start-Sleep -Seconds 5;
The code that I am using to deploy resources is as follows,
provider "azurerm" {
features {}
}
data "azurerm_subnet" "vmsnet" {
name = "vm_subnet"
virtual_network_name = "az_test"
resource_group_name = "poc"
}
resource "azurerm_resource_group" "vmrg" {
name = "tfvmtestrg"
location = "uk south"
}
resource "azurerm_network_interface" "az_vm_nic" {
location = "uk south"
name = "tfvmtestnic"
resource_group_name = azurerm_resource_group.vmrg.name
ip_configuration {
name = "internal"
private_ip_address_allocation = "Dynamic"
subnet_id = data.azurerm_subnet.vmsnet.id
}
}
resource "azurerm_windows_virtual_machine" "az_vm_win" {
admin_password = "tfvmtestrgrtetghcgh"
admin_username = "tfvmtestrg"
location = "uk south"
name = "tfvmtestvm"
network_interface_ids = [azurerm_network_interface.az_vm_nic.id]
resource_group_name = azurerm_resource_group.vmrg.name
size = "Standard_D2s_v3"
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2022-Datacenter"
version = "latest"
}
}
resource "azurerm_virtual_machine_extension" "vmext" {
name = azurerm_windows_virtual_machine.az_vm_win.name
virtual_machine_id = azurerm_windows_virtual_machine.az_vm_win.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.10"
### THIS PART IS ALL NEEDED, INCLUDING THE WEIRD SETTINGS BIT.
settings = <<SETTINGS
{
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File azbastionsetup01.ps1",
"fileUris": ["https://raw.githubusercontent.com/INGourav/Azure-Resources/master/azbastionsetup01.ps1"]
}
SETTINGS
}