How To Create A GCP Compute Engine VM Instance Using PowerShell
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In this blog post, we will discuss how to create a GCP compute VM instance using PowerShell. We will create a new VM instance from scratch including network and firewall rule.
Prerequisites:
- One Windows system with PowerShell.
- PowerShell module for GCP installed and configured with proper access.
You can follow the below blog post to install and configure PowerShell for GCP.
https://cloudaffaire.com/how-to-install-and-configure-powershell-for-gcp/
How To Create A GCP Compute Engine VM Instance Using PowerShell:
Step 1: Create a new custom network.
1 2 3 4 5 6 7 |
##################################################################### ## How To Create A GCP Compute Engine VM Instance Using PowerShell ## ##################################################################### ## Creates a new Google Compute Engine custom network $MyGCPNetwork = New-GceNetwork -Name "my-gcp-network" ` -Project " |
Step 2: Create a new firewall rule for SSH connection in your custom GCP network.
1 2 3 |
## Add a new Firewall rule for SSH connection on your custom network $MyGCPFireWall = New-GceFirewallProtocol tcp -Port 22 | Add-GceFirewall ` -Name "my-gcp-ssh-firewall" -Project " |
Step 3: Get GCP image details for your VM instance. For this demo I am using CentOS 7 image, you can try any other image you like.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
## Get GCP Image Details ## CentOS 7 $MyGCPImages = Get-GceImage -Family "centos-7" $MyGCPImage = $MyGCPImages | Sort-Object CreationTimestamp -Descending | Select-Object * ## Debian 10 ## $MyGCPImages = Get-GceImage -Family "debian-10" ## $MyGCPImage = $MyGCPImages | Sort-Object CreationTimestamp -Descending | Select-Object * ## Redhat 7 ## $MyGCPImages = Get-GceImage -Family "rhel-7" ## $MyGCPImage = $MyGCPImages | Sort-Object CreationTimestamp -Descending | Select-Object * ## Suse Linyx 12 ## $MyGCPImages = Get-GceImage -Family "sles-12" ## $MyGCPImage = $MyGCPImages | Sort-Object CreationTimestamp -Descending | Select-Object * ## Ubuntu 18 ## $MyGCPImages = Get-GceImage -Family "ubuntu-1804-lts" ## $MyGCPImage = $MyGCPImages | Sort-Object CreationTimestamp -Descending | Select-Object * ## Windows 2019 ## $MyGCPImages = Get-GceImage -Family "windows-2019" ## $MyGCPImage = $MyGCPImages | Sort-Object CreationTimestamp -Descending | Select-Object * |
Step 4: Get your GCP VM instance machine type. For this demo, I will be using e2-micro which is free tire eligible.
1 2 3 |
## Get GCP Machine Types $MyGCPImageType = Get-GceMachineType -Project " $MyGCPImageType | Select-Object Name,Description; |
Step 5: Create the new GCP compute engine VM instance.
1 2 3 4 5 6 7 8 9 10 11 |
## Create the GCP compute VM instance $MyGCPInstance = Add-GceInstance -Project " -Zone " -MachineType "e2-micro" -DiskImage $MyGCPImage -Network $MyGCPNetwork.Id; ## Get GCP Instance details Get-GceInstance -Project " -Zone " ## Get GCP Instance disk details Get-GceDisk -Project " |
Step 6: Connect to your new GCP compute engine VM instance using gcloud.
1 2 3 |
## Connect to your GCP Instance gcloud beta compute --project " exit |
Note: I did not find any PowerShell cmdlet to configure ssh keys for GCP compute instance, hence using gcloud instead. If you are aware, kindly write in the comment section and I will update the blog accordingly.
Step 7: Cleanup.
1 2 3 4 5 6 7 8 9 |
## Delete your GCP compute engine instance Remove-GceInstance -Project " -Zone " ## Delete FireWall rule Remove-GceFirewall "my-gcp-ssh-firewall" -Project " ## Delete the custom GCP network Remove-GceNetwork -Name "my-gcp-network" -Project " |
Hope you have enjoyed this blog post. Please refer below GCP documentations for more details
https://cloud.google.com/tools/powershell/docs/create-manage
http://googlecloudplatform.github.io/google-cloud-powershell/#/