For a student project I need to deploy a VM on Google Cloud throught a powershell script that, whit no interaction, deploys an apache Http opened service.
in my script I already created all the network, subnetwork, api, ports and all needed to the machine to work.
The point is the startup sequence, inside the CreateVM function, the machine is created succesfully whit all settings correctly applied, but when i acess it throught SSH, theres no apache installed and/or running.
function CreateVm {
$vmExists = gcloud compute instances describe $VmName --zone=$Zone --project=$ProjectId --format="value(name)" 2>$null
if (-not $vmExists) {
try {
gcloud compute instances create $VmName `
--zone=$Zone `
--machine-type=e2-medium `
--subnet=$SubnetName `
--tags=$VmName `
--metadata=startup-script='#! /bin/bash
sudo apt-get update -qq
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y apache2 ufw -qq < /dev/null
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 22/tcp
sudo ufw --force enable
sudo systemctl start apache2
sudo systemctl enable apache2' `
--project=$ProjectId `
--quiet
Write-Host "VM $VmName created successfully with Apache2 and firewall configuration." -ForegroundColor Green
} catch {
Write-Host "Failed to create VM $VmName : $($_.Exception.Message)" -ForegroundColor Red
gcloud compute instances delete $VmName --zone=$Zone --quiet --project=$ProjectId
return $false
}
} else {
Write-Host "VM $VmName already exists." -ForegroundColor Yellow
}
return $true
}
I tried to redesign the script and the entire function a couple of times, and doing all the process of the script manually from the GCConsole but nothing seemed to change, only when i access direclty through ssh and i throw then commands by myself works, but in this case thats not valid.
gilipollx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.